Pages

Showing posts with label career in java. Show all posts
Showing posts with label career in java. Show all posts

Thursday, December 1, 2011

Differences between Java and C++.

Introduction

This lesson introduces you to Java programming by presenting some of the similarities and differences between Java and C++.

Similarities and Differences


1.      Java does not support typedefs, defines, or a preprocessor. Without a preprocessor, there are no provisions for including header files.

2.      Since Java does not have a preprocessor there is no concept of #define macros or manifest constants. However, the declaration of named constants is supported in Java through use of the final keyword.

3.      Java does not support enums but, as mentioned above, does support named constants.

4.      Java supports classes, but does not support structures or unions.

5.      All stand-alone C++ programs require a function named main and can have numerous other functions, including both stand-alone functions and functions, which are members of a class. There are no stand-alone functions in Java. Instead, there are only functions that are members of a class, usually called methods. Global functions and global data are not allowed in Java.

6.      All classes in Java ultimately inherit from the Object class. This is significantly different from C++ where it is possible to create inheritance trees that are completely unrelated to one another.

7.      All function or method definitions in Java are contained within the class definition. To a C++ programmer, they may look like inline function definitions, but they aren't. Java doesn't allow the programmer to request that a function be made inline, at least not directly.

8.      Both C++ and Java support class (static) methods or functions that can be called without the requirement to instantiate an object of the class.

9.      The interface keyword in Java is used to create the equivalence of an abstract base class containing only method declarations and constants. No variable data members or method definitions are allowed. (True abstract base classes can also be created in Java.) The interface concept is not supported by C++.

10.  Java does not support multiple inheritance. To some extent, the interface feature provides the desirable features of multiple inheritance to a Java program without some of the underlying problems.

11.  While Java does not support multiple inheritance, single inheritance in Java is similar to C++, but the manner in which you implement inheritance differs significantly, especially with respect to the use of constructors in the inheritance chain.

12.  In addition to the access specifiers applied to individual members of a class, C++ allows you to provide an additional access specifier when inheriting from a class. This latter concept is not supported by Java.

13.  Java does not support the goto statement (but goto is a reserved word). However, it does support labeled break and continue statements, a feature not supported by C++. In certain restricted situations, labeled break and continue statements can be used where a goto statement might otherwise be used.

14.  Java does not support operator overloading.

15.  Java does not support automatic type conversions (except where guaranteed safe).

16.  Unlike C++, Java has a String type, and objects of this type are immutable (cannot be modified). Quoted strings are automatically converted into String objects. Java also has a StringBuffer type. Objects of this type can be modified, and a variety of string manipulation methods are provided.

17.  Unlike C++, Java provides true arrays as first-class objects. There is a length member, which tells you how big the array is. An exception is thrown if you attempt to access an array out of bounds. All arrays are instantiated in dynamic memory and assignment of one array to another is allowed. However, when you make such an assignment, you simply have two references to the same array. Changing the value of an element in the array using one of the references changes the value insofar as both references are concerned.

18.  Unlike C++, having two "pointers" or references to the same object in dynamic memory is not necessarily a problem (but it can result in somewhat confusing results). In Java, dynamic memory is reclaimed automatically, but is not reclaimed until all references to that memory become NULL or cease to exist. Therefore, unlike in C++, the allocated dynamic memory cannot become invalid for as long as it is being referenced by any reference variable.

19.  Java does not support pointers (at least it does not allow you to modify the address contained in a pointer or to perform pointer arithmetic). Much of the need for pointers was eliminated by providing types for arrays and strings. For example, the oft-used C++ declaration char* ptr needed to point to the first character in a C++ null-terminated "string" is not required in Java, because a string is a true object in Java.

20.  A class definition in Java looks similar to a class definition in C++, but there is no closing semicolon. Also forward reference declarations that are sometimes required in C++ are not required in Java.

21.  The scope resolution operator (::) required in C++ is not used in Java. The dot is used to construct all fully-qualified references. Also, since there are no pointers, the pointer operator (->) used in C++ is not required in Java.

22.  In C++, static data members and functions are called using the name of the class and the name of the static member connected by the scope resolution operator. In Java, the dot is used for this purpose.

23.  Like C++, Java has primitive types such as int, float, etc. Unlike C++, the size of each primitive type is the same regardless of the platform. There is no unsigned integer type in Java. Type checking and type requirements are much tighter in Java than in C++.

24.  Unlike C++, Java provides a true boolean type.

25.  Conditional expressions in Java must evaluate to boolean rather than to integer, as is the case in C++. Statements such as if(x+y)... are not allowed in Java because the conditional expression doesn't evaluate to a boolean.

26.  The char type in C++ is an 8-bit type that maps to the ASCII (or extended ASCII) character set. The char type in Java is a 16-bit type and uses the Unicode character set (the Unicode values from 0 through 127 match the ASCII character set). For information on the Unicode character set see http://www.stonehand.com/unicode.html.

27.  Unlike C++, the >> operator in Java is a "signed" right bit shift, inserting the sign bit into the vacated bit position. Java adds an operator that inserts zeros into the vacated bit positions.

28.  C++ allows the instantiation of variables or objects of all types either at compile time in static memory or at run time using dynamic memory. However, Java requires all variables of primitive types to be instantiated at compile time, and requires all objects to be instantiated in dynamic memory at runtime. Wrapper classes are provided for all primitive types except byte and short to allow them to be instantiated as objects in dynamic memory at runtime if needed.

29.  C++ requires that classes and functions be declared before they are used. This is not necessary in Java.

30.  The "namespace" issues prevalent in C++ are handled in Java by including everything in a class, and collecting classes into packages.

31.  C++ requires that you re-declare static data members outside the class. This is not required in Java.

32.  In C++, unless you specifically initialize variables of primitive types, they will contain garbage. Although local variables of primitive types can be initialized in the declaration, primitive data members of a class cannot be initialized in the class definition in C++.

33.  In Java, you can initialize primitive data members in the class definition. You can also initialize them in the constructor. If you fail to initialize them, they will be initialized to zero (or equivalent) automatically.

34.  Like C++, Java supports constructors that may be overloaded. As in C++, if you fail to provide a constructor, a default constructor will be provided for you. If you provide a constructor, the default constructor is not provided automatically.

35.  All objects in Java are passed by reference, eliminating the need for the copy constructor used in C++.

36.  (In reality, all parameters are passed by value in Java. However, passing a copy of a reference variable makes it possible for code in the receiving method to access the object referred to by the variable, and possibly to modify the contents of that object. However, code in the receiving method cannot cause the original reference variable to refer to a different object.)

37.  There are no destructors in Java. Unused memory is returned to the operating system by way of a garbage collector, which runs in a different thread from the main program. This leads to a whole host of subtle and extremely important differences between Java and C++.

38.  Like C++, Java allows you to overload functions. However, default arguments are not supported by Java.

39.  Unlike C++, Java does not support templates. Thus, there are no generic functions or classes.

40.  Unlike C++, several "data structure" classes are contained in the "standard" version of Java. More specifically, they are contained in the standard class library that is distributed with the Java Development Kit (JDK). For example, the standard version of Java provides the containers Vector and Hashtable that can be used to contain any object through recognition that any object is an object of type Object. However, to use these containers, you must perform the appropriate upcasting and downcasting, which may lead to efficiency problems.

41.  Multithreading is a standard feature of the Java language.

42.  Although Java uses the same keywords as C++ for access control: private, public, and protected, the interpretation of these keywords is significantly different between Java and C++.

43.  There is no virtual keyword in Java. All non-static methods always use dynamic binding, so the virtual keyword isn't needed for the same purpose that it is used in C++.

44.  Java provides the final keyword that can be used to specify that a method cannot be overridden and that it can be statically bound. (The compiler may elect to make it inline in this case.)

45.  The detailed implementation of the exception handling system in Java is significantly different from that in C++.

46.  Unlike C++, Java does not support operator overloading. However, the (+) and (+=) operators are automatically overloaded to concatenate strings, and to convert other types to string in the process.

47.  As in C++, Java applications can call functions written in another language. This is commonly referred to as native methods. However, applets cannot call native methods.

48.  Unlike C++, Java has built-in support for program documentation. Specially written comments can be automatically stripped out using a separate program named javadoc to produce program documentation.

49.  Generally Java is more robust than C++ due to the following:
  1. Object handles (references) are automatically initialized to null.
  2. Handles are checked before accessing, and exceptions are thrown in the event of problems.
  3. You cannot access an array out of bounds.
  4. Memory leaks are prevented by automatic garbage collection.


Sunday, November 27, 2011

Java Interview Questions

Java interview questions and answers

  1. What is garbage collection? What is the process that is responsible for doing that in java? Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this process
  2. What kind of thread is the Garbage collector thread? It is a daemon thread.
  3. What is a daemon thread? - These are the threads which can run without user intervention. The JVM can exit when there are daemon thread by killing them abruptly.
  4. How will you invoke any external process in Java? - Runtime.getRuntime().exec(….)
  5. What is the finalize method do? - Before the invalid objects get garbage collected, the JVM give the user a chance to clean up some resources before it got garbage collected.
  6. What is mutable object and immutable object? - If a object value is changeable then we can call it as Mutable object. (Ex., StringBuffer, …) If you are not allowed to change the value of an object, it is immutable object. (Ex., String, Integer, Float, …)
  7. What is the basic difference between string and stringbuffer object? - String is an immutable object. StringBuffer is a mutable object.
  8. What is the purpose of Void class? - The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void.
  9. What is reflection? - Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions.
  10. What is the base class for Error and Exception? - Throwable
  11. What is the byte range? -128 to 127
  12. What is the implementation of destroy method in java.. is it native or java code? - This method is not implemented.
  13. What is a package? - To group set of classes into a single unit is known as packaging. Packages provides wide namespace ability.
  14. What are the approaches that you will follow for making a program very efficient? - By avoiding too much of static methods avoiding the excessive and unnecessary use of synchronized methods Selection of related classes based on the application (meaning synchronized classes for multiuser and non-synchronized classes for single user) Usage of appropriate design patterns Using cache methodologies for remote invocations Avoiding creation of variables within a loop and lot more.
  15. What is a DatabaseMetaData? - Comprehensive information about the database as a whole.
  16. What is Locale? - A Locale object represents a specific geographical, political, or cultural region
  17. How will you load a specific locale? - Using ResourceBundle.getBundle(…);
  18. What is JIT and its use? - Really, just a very fast compiler… In this incarnation, pretty much a one-pass compiler — no offline computations. So you can’t look at the whole method, rank the expressions according to which ones are re-used the most, and then generate code. In theory terms, it’s an on-line problem.
  19. Is JVM a compiler or an interpreter? - Interpreter
  20. When you think about optimization, what is the best way to findout the time/memory consuming process? - Using profiler
  21. What is the purpose of assert keyword used in JDK1.4.x? - In order to validate certain expressions. It effectively replaces the if block and automatically throws the AssertionError on failure. This keyword should be used for the critical arguments. Meaning, without that the method does nothing.
  22. How will you get the platform dependent values like line separator, path separator, etc., ? - Using Sytem.getProperty(…) (line.separator, path.separator, …)
  23. What is skeleton and stub? what is the purpose of those? - Stub is a client side representation of the server, which takes care of communicating with the remote server. Skeleton is the server side representation. But that is no more in use… it is deprecated long before in JDK.
  24. What is the final keyword denotes? - final keyword denotes that it is the final implementation for that method or variable or class. You can’t override that method/variable/class any more.
  25. What is the significance of ListIterator? - You can iterate back and forth.
  26. What is the major difference between LinkedList and ArrayList? - LinkedList are meant for sequential accessing. ArrayList are meant for random accessing.
  27. What is nested class? - If all the methods of a inner class is static then it is a nested class.
  28. What is inner class? - If the methods of the inner class can only be accessed via the instance of the inner class, then it is called inner class.
  29. What is composition? - Holding the reference of the other class within some other class is known as composition.
  30. What is aggregation? - It is a special type of composition. If you expose all the methods of a composite class and route the method call to the composite method through its reference, then it is called aggregation.
  31. What are the methods in Object? - clone, equals, wait, finalize, getClass, hashCode, notify, notifyAll, toString
  32. Can you instantiate the Math class? - You can’t instantiate the math class. All the methods in this class are static. And the constructor is not public.
  33. What is singleton? - It is one of the design pattern. This falls in the creational pattern of the design pattern. There will be only one instance for that entire JVM. You can achieve this by having the private constructor in the class. For eg., public class Singleton { private static final Singleton s = new Singleton(); private Singleton() { } public static Singleton getInstance() { return s; } // all non static methods … }
  34. What is DriverManager? - The basic service to manage set of JDBC drivers.
  35. What is Class.forName() does and how it is useful? - It loads the class into the ClassLoader. It returns the Class. Using that you can get the instance ( “class-instance”.newInstance() ).
  36. Inq adds a question: Expain the reason for each keyword of
    public static void main(String args[])

Friday, November 25, 2011

Comparing .Net and Java

Java or .NET?
Over the past several year, we’ve seen the Java platform and the .NET platform continue to converge and the gap between them in terms of features, libararies, and languages continue to shrink. Not only that, but both platforms have to a certain extent been ported from the official version and can run on systems not envisioned by the original designer. Carefully designed ASP.NET applications can run on almost as many platforms as Java with the Mono-based CLR (Common Language Runtime), and its JVM (Java Virtual Machine) counterpart can now run within .NET (or Mono) with the help of IKVM.NET. However, despite all this cross-pollination and convergence between the two platforms there still remain distinct advantages for choosing a certain platform for your next enterprise, web-based, desktop, or mobile application. Compiling a comprehensive list or chart of what makes one platform ideal for a scenario is impossible; however, there are these simple guidelines that can help you start down that path. We’ve broken down the high-level options into six categories, and we’ll discuss the options based on each one next.

Cloud Computing

Although this can usually be considered a subset of the next category (Web 2.0 Applications), cloud computing introduces several new constraints and a paradigm shift in the way development is approached. Microsoft is well aware of these implications and has already invested heavily in technologies that will align .NET more closely with the ideology behind cloud computing. However, the momentum, at least for the moment, is heavily in the favor of the entrenched technologies which are mainly built on top of open source software and commodity hardware. Amazon’s EC2, Google’s App Engine, and Salesforce.com’s Force.com are all built on technologies like Java, Python, and C++ and favor solutions that run on open source operating systems.
As we mentioned, however, Microsoft is not just sitting back and watching this huge market slip away from them, and is working feverishly to bring Azure, its entry into the cloud computing arena, to market. For the time being, however, the winner in this area between the two contestants is Java.

Web 2.0 Applications

As mentioned above, this category shares many of the features of the previous one so we will just mention that the topics of open source virtualization and operating systems still favor Java. There are, however, additional advantages that Java has in this realm, mainly in the form of open source libraries and frameworks that aid in the development of these applications. For example, the Java eco-system has hundreds of web application frameworks, content management systems, ecommerce systems, social network frameworks and so on, with a large amount of them being high quality as well as open source. Two exceptional Web 2.0 frameworks that are used to build successful Web 2.0 applications include JRuby on Rails and Grails.
Also, traditional ASP.NET applications had only one architectural option for build web apps, the classic Page controller pattern. Most interaction between the browser and the server happens through an HTTP POST action and session and page information is stored in a ViewState object that persists on every page. This architectural style creates a heavier load on the server and is more difficult to scale then the share-nothing architecture found in many Web 2.0 applications.
Microsoft has been working on alternative styles of web applications and is close to releasing an MVC architecture style for ASP.NET applications which uses the Front controller pattern found in most Java applications. Java, however, still has the advantage in this category.

Rich Internet Applications

Since the first release of Java in the mid-90s, the Java applet has been hailed as the ultimate platform for applications that can be written-once and run on any platform, easily distributed using a web-browser, and capable of high-quality graphics and animations. However, Sun stumbled early on with it’s browser plugin for the applet, and Java applets became synonymous with slow, buggy, and unwieldy applications. The promise behind them faded and although several really good applications using applets have been released the Java applet platform still hasn’t caught on. Flash became what Java should have been, and developers quickly switched their attention to that environment.
As the important of Rich Internet Applications started becoming apparent, Microsoft decided to take the technology it was building behind XAML and WPF (presentation frameworks for Windows and .NET) and build a Flash/Java killer that would provide all the benefits of Flash on the client side with all the benefits of .NET on the development end. Giving developers the powerful capabilities of Visual Studio and .NET libraries and a high-end graphics and animation framework quickly made Silverlight an important contender in the RIA arena. Although Sun is pouring a large amount of resources in its RIA player, JavaFX, it still remains to be seen whether it will have the success that Silverlight is enjoying, making .NET the winner in this category.

Enterprise Applications

We’ve been noticing a clear trend in many of the enterprise’s IT departments, and several surveys and studies have been confirming this. More enterprises are consolidation on the Microsoft platform, or at least clearly making it an equal partner where it was not a considered option before. From large enterprises to SMBs, IT departments are jumping at the opportunity to consolidate on a single platform for their mail, directory, ERP, CRM, document sharing, and so on. Exchange, SharePoint, Microsoft’s CRM and ERP offerings are becoming semi-standards in many companies and this starts to filter down to the development teams as they start using Visual Studio and .NET for integration and development projects. At the same time, Visual Studio has become an excellent IDE with some of the best support for XML/SOAP/WebServices that is the main glue available for both server to server as well as server to client communications.
Creating desktop applications in Visual Studio is also one of its strong spots, and with the (partially) controlled environment found in the enterprise it’s not a problem to quickly deploy the necessary .NET framework to all clients. With all these advantages, .NET is usually the safe bet for most enterprises and is the winner in this category.

Desktop Applications

As we just mentioned, Visual Studio is a top-notch IDE for building GUIs (graphical user interfaces) and desktop applications, especially with the data binding and component building support in Windows Forms and Windows Presentation Framework. However, there is a an important drawback, these applications (with the exception of those targeting Mono) will only work on Windows machines with the .NET framework pre-installed. For some developers this may be an acceptable assumption, especially if we’re targeting the enterprise as we mentioned above. However, for general purpose applications Java is a better option, considering that it usually comes pre-installed with most operating systems and OEMs, and can be easily installed on virtually any platform if it is not. In the future, Microsoft may extend Silverlight’s functionality with offline support and desktop integration but Java is a better option for now.

Mobile Applications

With the notable exception of Apple’s iPhone, the Java Mobile platform is available on almost every modern mobile device, and despite its fragmented and sometimes inconsistent implementation on the various platforms it’s still considered the one common tool for developers to take advantage of. If you want an application that runs on the most mobile platforms, Java is your platform to get there.