Browsing the tag Java
Consider the following Java program:
Read More »Like C++, Java also supports copy constructor. But, unlike C++, Java doesn’t create a default copy constructor if you don’t write your own.
Read More »The purpose of inheritance is same in C++ and Java. Inheritance is used in both languages for reusing code and/or creating is-a relationship. There are following differences in the way both languages provide support for inheritance.
Read More »Directly accessing Grandparent’s member in Java: Predict the output of following Java program.
Read More »Predict the output of following Java programs.
Read More »Difficulty Level: Rookie Predict the output of following Java Programs.
Read More »Access specifiers for classes or interfaces in Java: In Java, methods and data members of a class/interface can have one of the following four access specifiers. The access specifiers are listed according to their restrictiveness order.
Read More »Static blocks in Java Unlike C++, Java supports a special block, called static block (also called static clause) which can be used for static initializations of a class.
Read More »In C++ and Java, functions can not be overloaded if they differ only in the return type.
Read More »In Java, all methods in an interface are public even if we do not specify public with method names. Also, data fields are public static final even if we do not mention it with fields names. Therefore, data fields must be initialized.
Read More »In Java, it is compiler error to give more restrictive access to a derived class function which overrides a base class function.
Read More »In Java, constructor of base class with no argument gets automatically called in derived class constructor. For example, output of following program is:
Read More »Unlike C++, arrays are first class objects in Java. For example, in the following program, size of array is accessed using length which is a member of arr[] object.
Read More »Unlike C/C++, Java does not have goto statement, but java supports label. The only place where a label is useful in Java is right before nested loop statements. We can specify label name with break to break out a specific outer loop. Similarly, label name can be specified with continue.
Read More »Both languages use try, catch and throw keywords for exception handling, and meaning of try, catch and free blocks is also same in both languages. Following are the differences between Java and C++ exception handling.
Read More »Shadowing of static functions in Java: In Java, if name of a derived class static function is same as base class static function then the derived class static function shadows (or conceals) the base class static function. For example, the following Java code prints “A.fun()”
Read More »Exception Handling – catching base and derived classes as exceptions: If both base and derived classes are caught as exceptions then catch block of derived class must appear before the base class.
Read More »Assigning values to static final variables in Java: In Java, non-static final variables can be assigned a value either in constructor or with the declaration. But, static final variables cannot be assigned value in constructor; they must be assigned a value with their declaration.
Read More »Like C++, Java automatically creates default constructor if there is no default or parameterized constructor written by user, and (like C++) the default constructor automatically calls parent default constructor. But unlike C++, default constructor in Java initializes member data variable to default values
Read More »In Java, when final keyword is used with a variable of primitive data types (int, float, .. etc), value of the variable cannot be changed.
Read More »Unlike C/C++, static local variables are not allowed in Java. For example, following Java program fails in compilation with error “Static local variables are not allowed”
Read More »In Java, all objects are dynamically allocated. When we only declare a variable of a class type, Java just creates a reference. To allocate memory to an object, we must use new().
Read More »In Java, parameters are always passed by value. For example, following program prints i = 10, j = 20.
Read More »Default virtual behavior of methods is opposite in C++ and Java:
Read More »