Professional Documents
Culture Documents
There are mainly three reasons to eliminate the pointer concept from Java.
They are as follows:
1. The concept of Pointer leads to confusion for a programmer.
2. Pointer may break a program easily. For example, when we add two
pointers, the program can crash immediately.
3. The use of Pointer feature can break security because harmful programs
like virus and other hacking programs can be developed using pointer.
Because of the above reasons, the pointer feature has been completely
eliminated from Java technology.
Advantages of Java Bytecode
The main difference between the byte code and machine code is that byte
code can be run only on JVM whereas machine code is a set of instructions in
machine language that can be directly run by the CPU.
Difference between .java and .class
Java source code file (with a .java extension) are compiled into bytecode (with
a .class extension), which is then interpreted and executed by a Java
processor called JVM.
Key points:
1. A bytecode in Java is a set of byte-long instructions that Java compiler
produces and Java interpreter (JVM) executes.
2. When Java compiler compiles .java file, it generates a series of bytecode
(machine independent code)and stores them in a .class file.
3. JVM then interprets and executes the byte code stored in the .class file and
converts them into machine code.
4. The byte code remains the same on different platforms such as Windows,
Linux, and Mac OS.
Interpreter vs Compiler: How is Interpreter different
from a compiler in Java?
2. Types of language:
● C is a structural and procedural programming language.
● Java is a purely object-oriented programming language because
everything in java is an object. All the program code and data
remain within classes and objects.
3. Language level:
● C is a middle-level programming language because it combines
both machine-level and high-level languages.
● Java is a high-level programming language because the
translation of program source code takes place into machine
language using compiler or interpreter.
4. Approach:
● C language follows the top-down approach to design the
application program.
● Java language follows the bottom-up approach to design the
application program.
5. Translation:
● C is considered a compiled language because it converts the code
into machine language so that it could be understood by the
machine or system.
● Java is an interpreted language because the source code is first
converted into bytecode by the compiler and then that bytecode is
then executed by the JVM (Java Virtual Machine).
6. Platform Dependency:
● C is platform-dependent or system-dependent.
● Java is platform-independent. Program source code can be easily
moved from one computer system to another, anywhere, and
anytime.
7. Keywords:
● There are 32 keywords in the C language.
● Java has 50 keywords.
8. OOPs Concepts:
● C does not follow OOPs concepts mechanism.
● Java follows OOPs concepts mechanism.
9. File Extension:
● Source code file is saved with the extension .c.
● In Java, source code file is saved with the extension .java.
11. Security:
● C is not secure.
● Java is a fully secured language.
12. Overloading:
● C does not support overloading functionality.
● Java supports method overloading concept that helps in code
readability.
13. Pointers:
● Pointer concept is supported by the C programming language.
● Java does not support pointers.
15. Robust:
● C is not robust.
● Java is robust.
17. Inheritance:
● There is no inheritance concept present in the C language.
● Java supports inheritance that helps in code reusability.
18. Threading:
● There is no concept of threading in C language.
● Java programming language supports the concept of threading.
20. Preprocessors:
● C language supports Preprocessors.
● Java does not support Preprocessors.
26. Applications:
● C programming language is mainly used to develop system
applications and firmware.
● Whereas Java programming language is mainly used to develop
enterprise applications and web-based applications.
a) C++ uses compiler only whereas Java uses both compiler and interpreter.
b) C++ supports both operator overloading and method overloading whereas
Java only supports method overloading, not operator overloading.
c) C++ has manual object management with the help of new and delete
keywords while Java has automatic built-in garbage collection.
d) C++ supports structures and unions whereas Java doesn’t support
structures and unions.
e) C++ supports pointers while Java doesn’t support pointers.
Q1) Which one is better C++ or Java?
Ans: Perhaps, it is difficult to say which is better. Both C++ and Java have
their own features, merits, and demerits.
C++ is a highly evolved language and is mostly good for system programming
with advanced features like templates and containers. In fact, C++ can do
anything in systems development from enterprise to gaming. We cannot do it
with Java.
Java is the most widely used to develop desktop applications, gaming, web
servers, and client-side web applications. It is also known as a native
language of the Android operating system that operates on Android phones
and tablets.
It’s also easier to find a Java developer than a “harder” language such as C+
+. Thus, the choice between C++ vs Java totally depends on which kind of
application we are developing.
So, before going to choose between better: C++ and Java, First, evaluate
beforehand the pros and cons of both the languages and then decide which is
the best programming language to develop your application should be.
There are the following differences between class and object in java. They are
as follows:
1. A class is a user-defined data type whereas an object is an instance of
class data type.
2. A class generates objects whereas an object gives life to a class.
3. Classes do not occupy memory location but objects occupy memory
location.
4. Classes cannot be manipulated due to not available in the memory location
but objects can be manipulated.
There are several ways to create an object of class in Java. They are as
follows.
1. Using the new keyword
2. Using Class.forName
3. Using Clone.
4. Using Object Deserialization.
5. Using ClassLoader.
Difference between Primitive and Non-primitive Data
types in Java
The difference between while loop and do-while loop in Java is as follows:
1. In the case of while loop, first test condition is verified and then executes
the statements inside the while block.
In the case of do-while loop, first statements inside do block are executed and
then the test condition is verified.
2. If the test condition is false for first time, then block of statement executed
zero times.
If the test condition is false for the first time, then the block of statement
executed at least once time.
3. In the while syntax, while statement is at the top.
In the do-while syntax, while statement is at the bottom.
Key points:
1. While importing another package, package declaration must be the first
statement and followed by package import.
2. A class can have only one package statement but it can be more than one
import package statement.
3. import can be written multiple times after the package statement and before
the class statement.
4. You must declare the package with root folder name (No subfolder name)
and the last file name must be class name with a semicolon.
5. When you import, it does mean that memory is allocated. It just gives the
path to reach the file.
Why cannot Static method directly access non-static
members?
We cannot directly access non-static members from a static method. That is,
we cannot directly call instance members within a static area because there is
a logical concept behind such restriction.
Static members such as static variables, methods, or blocks are linked with
the class. Therefore, they get memory only once when the class is loaded in
the memory.
But non-static members like instance variables, method, or block get memory
after the object creation of the class.
Therefore, If we call any non-static members from the static area, it means
that when the class is loaded in the memory, the static members also loaded
and it will look for non-static members which are not in existence because we
have not created an instance till now. Hence there is ambiguity.
That’s why we cannot call the non-static member from the static area. A non-
static method can access static members in Java. we will discuss in the static
tutorial.
Since static method cannot access directly the non-static members but if we
create the object of the class within the static method, In this case, we can
access non-static members within the static method.
Why public static void main in Java?
Basically, the public static void main(String [ ] args) acts as an entry point to
start the execution of Java application program. That’s why we use/write
public static void main in java program.
Yes, a class can have any number of main() methods but the execution
always starts from public static void main(String[ ] args) only. Let’s take an
example program where we will declare more than one method.
Can we execute a program without main() method in
Java?
Yes, we can execute a program without main() method in Java in the previous
version of JDK. One of the ways is a static block. But from onwards JDK 1.7
and above, is not possible.
Yes, we can overload the main() method but we cannot override it. We can
declare any number of main() method in a class, but the method signature
must be different. Let’s make a program where we will overload the main
method.
Key Points:
1. public static void main(String args[ ]) is a line at which the program will start
executing.
2. main() method in java is always called by JVM (Java Virtual Machine)
before any objects are created.
3. It does not return any value.
4. Since Java is case-sensitive, Main is different from main. If you type Main
instead of main, java compiler would still compile your program but java will
report an error because it would not find main() method.
5. If a program does not contain the main method in a class, Java compiler
will compile it but cannot run it.
6. Java main() method can be overloaded but cannot override it.
Difference between Argument and Parameter in Java
Key Points:
1. Java passes the arguments both primitives and the copy of object reference
by value.
2. Java never passes the object itself.
3. In realtime project, we pass class objects as parameters to method call in
java.
Can we have two methods having same name with different
parameters?
Yes, we can have two methods having the same name with different
parameters in a class. This technique is called method overloading in java.
Declaring a method inside another method is called inner method in java but
in the entire Java, the inner method concept is not allowed.
Note: Java supports the inner class concept but does not support the inner
method concept.
Characteristics of Constructor
6. It is called and executed only once per object. This means that when an
object of a class is created, constructor is called. When we create second
object then the constructor is again called during the second time.
Where,
School ➝ Name of the class.
sc ➝ Object reference variable which stores the address of the object in the stack
memory.
School() ➝ Constructor of class.
new ➝ It is a special keyword that allocates the memory to store objects whose type
is specified by a constructor. After allocation of memory, it calls constructor to
initialize objects, which are stored in the heap ( a region of memory for storing
objects).
When the constructor ends, a new keyword returns memory addresses to the
object so that it can be accessed from anywhere in the application.
Basically, there are two types of constructors in java. They are as:
1. Default Constructor (No-argument constructor)
2. Parameterized Constructor (Argument constructor)
Difference between Constructor and Method in Java
Constructor vs Method
S Constructor Method
N
2. It has no return type even void also. It has both void and return
type.
3. If we don’t provide any constructor in Method is not provided by the
the class, Java Compiler provides a compiler in any case.
default constructor for that class.
Key Points:
1. The task of a constructor in Java is to initialize instance variables of an
object.
2. A constructor of class is automatically executed by JVM when an object is
instantiated.
3. When an object of class is instantiated, constructor is called before any
methods.
4. If you declare your own constructor, you have to decide what values should
be given to instance variables.
5. this and super keyword must be the first line in the constructor.
6. Constructor overloading is possible but overriding is not possible.
7. It cannot be inherited in the subclass.
8. A constructor can also call another constructor of the same class using
‘this’ and for argument constructor use ‘this(para_list).
9. A Constructor is not a keyword in Java.
10. Java constructor is the third possibility to initialize instance variables in a
class.
1. Can a constructor be private in Java?
2. What is the use of private constructor?
3. How to access or class private constructor?
4. Can we call private constructor?
The below table summarizes the visibility of various access modifiers in java.
1. Both instance block and constructor will be executed automatically for every
object creation, but instance block will be executed before a constructor.
2. A constructor can take arguments but instance block cannot take any
argument. Hence, we cannot replace the constructor concept with IIB.
3. If we want to perform any activity for every object creation, we have to
define that activity inside the instance block.
Yes, static members (static variables) can be accessed from both instance
and static area (i.e. instance and static methods) directly using the class name
or without the class name. But outside the class, we can call only using class
name.
We can access a static variable in Java by using class name from another
class.
We can change the value of the static variable in java by using a constructor
and static block but not inside a static method.
Class variable makes program memory efficient. That is, it saves memory. All
static variables are stored in PermGen space of the heap memory.
Key Points:
1. Static variable (also known as class variable) in Java is simply like another
variable but it is common to all instances of the class.
2. It can be accessed and modified by any other objects of class.
3. A static variable can be directly accessed by class name within static and
non-static methods of the class.
4. It can be used with public and final modifiers to make constant value.
Why Instance variable is not available to Static method?
When we declare a static method in Java, JVM first executes the static
method, and then it creates objects of the class. Since objects are not
available at the time of calling the static method.
Therefore, instance variables are also not available to a static method. Due to
which a static method cannot access an instance variable in the class.
There are mainly five differences between static method and instance method.
They are as follows:
1. A static method is also known as class method whereas, the instance
method is also known as non-static method.
2. The only static variable can be accessed inside static method whereas,
static and instance variables both can be accessed inside the instance
method.
3. We do not need to create an object of the class for accessing static method
whereas, in the case of an instance method, we need to create an object for
access.
4. Class method cannot be overridden whereas, an instance method can be
overridden.
5. Memory is allocated only once at the time of class loading whereas, in the
case of the instance method, memory is allocated multiple times whenever the
method is calling.
Look at the difference between method overriding and method hiding in the
below figure.
This is one of the most tricky java questions where the interviewer will try to
confuse you. May be interviewer also does not know whether they can
override or overload static methods in Java. Therefore, you do not confuse
and give an exact answer.
Let’s understand from three different cases with example programs where we
cannot override static methods in Java.
Case 1:
Explanation:
In this example program, the m1() method is declared as static in the parent
class but not in child class. We are trying to override a static method with a
non-static method (instance method).
So, is it a valid case? No, it is not a valid case for overriding because static
method means class-level method whereas, instance method means object-
level method.
Case 2:
Explanation:
So, this case is also not valid for overriding. Therefore, we cannot override an
instance method with a static method.
Case 3:
Output:
m1-AB
m1-ABC
m1-AB
Explanation:
In this example program, both parent class and child class methods are static.
We did not get any compile-time error. It seems overriding concept applicable
for static methods but it is not overriding, it is method hiding. How?
1. obj1.m1(); will call m1() method of class AB because the reference variable
obj1 is the type of AB. Therefore, the output is “m1-AB”.
2. obj2.m1(); will call m1() method of class ABC because the reference
variable obj2 is the type of ABC. Therefore, the output is “m1-ABC”.
3. obj3.m1(); will call m1() method of class AB because the reference variable
obj3 is the type of AB. Therefore, the output is “m1-AB”.
Key Points:
When we execute a particular class, JVM performs two actions at the runtime.
They are as:
1. JVM loads the corresponding dot class file (byte code) into memory.
2. During the dot class file loading into memory, static block is executed. After
loading the dot class file, JVM calls the main method to start execution.
Therefore, static block is executed before the main method.
In other words, we can also say that static block always gets executed first in
Java because it is stored in the memory at the time of class loading and
before the object creation.
Dot class file is loaded into the memory only one time. So, only one time static
block will be executed. Instance block’s execution depends upon the object
creation.
If we create 10 objects, 10 times instance blocks will be executed but the
execution of static block depends upon the class loading. Since the class is
loaded only one time So, static block will be executed only one time.
It is possible to execute static block without a main method inside the class up
to Java 1.5 version but Java 1.6 version onwards, the main method is
mandatory to execute a static block inside the class.
During the dot class file loading, first static blocks are executed and then JVM
calls main method.
The top five differences between instance block and static block in java are as
follows:
1. Static block is also known as a static initialization block whereas instance
block is also known as instance initialization block or non-static block.
2. They execute before the instance block whereas instance block executes
after the static blocks.
3. Only static variables can be accessed inside the static block whereas both
static and non-static variables can be accessed inside the instance block.
4. Static blocks execute when the class is loaded into the memory whereas
instance blocks execute only when instance of the class is created.
5. ‘this’ keyword cannot be used in the static block whereas this keyword can
be used in the instance block.
Key Points:
1. A static block in java can not be declared inside any method.
2. JVM executes a static block on the highest priority basis when the dot class
file is loaded into the memory.
3. If the keyword “static” is missed before block then the block is called non-
static block (instance block) that is executed when class is instantiated.
Key Points:
1. A constructor cannot be final.
2. A block cannot be final.
3. A local final variable must be initialized at the time of declaration.
4. We cannot change the value of a final variable after initialization.
5. A final method cannot be overridden.
6. A final class cannot be extended(inherited).
7. We can create the object for a final class but cannot extend it.
8. If the method parameters are declared as final, the value of these
parameters cannot be changed.
Nested class is a class that is defined inside another class. There are two
types of nested class: static and non-static. Non-static nested class is the
inner class.
Advantages of Inner class
There are basically five advantages of inner class in java. They are as follows:
1. Inner class provides a way to group the classes and interfaces in one place
that logically belongs together.
2. It can directly access all the variables and methods of the outer class
including private.
3. It is used to develop more readable and maintainable code.
4. It needs less code to write.
5. It increases encapsulation.
Restriction on Anonymous Inner class
An anonymous inner class also has the same restriction as to the local inner
class with respect to its members.
1. Anonymous inner class cannot be declared as public, private,
protected, or static.
2. It cannot access local variables of its enclosing scope that are not
declared as final or effectively final.
3. Inside anonymous inner classes, we cannot define any static
variables, methods, or static blocks except for static final constant.
4. We cannot create more than one object of the anonymous inner
class in Java.
5. Since an anonymous inner class has no name. Therefore, we
cannot declare a constructor for it within the class body.
Difference between Normal/Regular class and
Anonymous Inner class
The difference between a normal class and an anonymous inner class in java
is as follows:
1. A normal Java class can implement any number of interfaces
simultaneously but an anonymous inner class can implement only one
interface at a time.
2. A normal Java class can extend a class and can implement any number of
interfaces simultaneously but an anonymous inner class can extend a class or
can implement an interface but not both simultaneously.
3. In a normal Java class, we can define any number of constructors but in an
anonymous inner class, we cannot write any constructor explicitly (because
the name of the class and name of the constructor must be the same but
anonymous inner class not having any name).
Can we declare Main method within Static nested
class?
Yes, we can declare the main method inside a static nested class. A static
nested class can have both static and non-static members including the main
method.
1. Can you declare a class inside a class?
2. Can you declare a class inside an interface?
3. Can you declare an interface inside a class?
4. Can you declare an interface inside an interface?
Answer: If the interviewed person asks the above kind of questions then you
tell a simple answer like this. Anything inside anything is possible with respect
to class and interface. All four combinations are valid.
There are the following advantages of using a static nested class in Java.
They are as follows:
1. A static nested class can directly access the static members of its outer
class including private static members.
2. It acts as a direct member of its top-level class, not a member of the
package in which it is declared.
3. Without an existing outer class object, there may be a chance of an existing
static nested class. To access static members of the static nested class, there
is no need for an outer class object.
Key points:
1. A class declared inside another class is known as nested classes in java.
2. There are two types of nested classes available in Java: static and non-
static.
3. The member class declared with a static modifier is known as static nested
class.
4. The non-static member class is known as inner class or non-static nested
class.
5. A static nested class is not an inner class in java because it is considered
as top-level class.
6. It can be declared public, protected, package-level, or private to restrict its
accessibility.
7. An instance of a static nested class is created the same way as you create
an instance of a top-level class using the new keyword.