This post lists the Java Programming FAQ (Frequently Asked Questions and answers) for freshers for next job interview or Java Programming exam.
James Gosling is the father of the Java language. It was developed as a programming language in 1996 by the Sun Microsystem.
The main target of developing the Java language was "WRITE ONE, RUN ANYWHERE". It means the code you are writing is machine-independent. If you are writing any program that particular program will run on every operating system. There is no need to write a program again for another operating system. In the case of C and C++, the codes are machine-dependent. So to make your program run on every platform you have to write an individual program for every platform.
Features of Java language as follow:
For more details, you can link on the below
https://www.tutorialslink.com/Articles/What-is-Java-and-Its-Features/1493
The Java programming model is way different from the traditional programming model. The java programming model is designed to make the language machine-independent.
Java programming Model:
Source Code ----> Bytecode ----> JVM
Source code: It is a basic code you write in java IDE.
Bytecode: It is a machine-independent code. This bytecode file helps to run the code on any operating system.
JVM: It converts the bytecode for the machine in which it is running.
In the traditional approach, the source code is directly converted into the native code. That's why it is machine-dependent.
Reserved words are pre-defined words in the language. These words have an already defined meaning in the language. The programmer can not these words a name of the variable, identifier, or function name. If a programmer tries to do that a program will through an error.
In general, the packages mean putting a similar thing into one pack. Similarly, package in java means a grouping of related types of classes, interfaces, and other packages. Package help in providing access protection and namespace management.
Conversion means converting the data type into another data type. There are two types of conversion:
byte ----> short, int, long, float.
int ----> long, float, double
float ----> double
short ---->byte
int -----> short, byte
double ----> float
Access modifiers are hinder the access of classes, interfaces, and variables. There are four types of access modifier in Java:
It refers to the instance of the current object from any method or constructor. It is particularly used to differentiate between a class variable and a local variable. We can provide the same name in the method or function parameters mentioned in class variables.
In simple words, they are class-level members. There is no need to create an object of static class. Static methods are not allowed to access the class-level variables. Only static methods can access the static variables. We can also initialize the value to the static variables in the static blocks.
Typecasting means assigning an object of the superclass to an inherited class or vice-versa. There are two types of typecasting:
Inheritance means inheriting the properties of one class to another class. The class whose property is inherited is known as parent class and in which property is inherited is known as child class. The types of inheritance Java supports are:
One more inheritance exists in the OOPs i.e Multiple Inheritance but Java does not support multiple inheritances.
JDK | JVM | JRE |
JDK stands for Java Development Kit. | JVM stands for Java Virtual Machine. | JRE stands for Java Runtime Environment. |
JDK is used to compile and execute the Java package. | It provides the specification for the runtime environment to run the bytecode of the java program. | It provides the runtime environment to run the bytecode. |
It consists of JRE+development tools. | It a superset that contains specification, Implementation, and runtime instances. | It is the virtual implementation of the JDK. |
Constructor is the method of the class that has the same name as that of the class. It is specially used to initialize the object. There are two types of the constructor:
No, we can inherit the constructor of one class to another class. Constructor is the part of the class whose name is the same as that of the class.
Java Constructor | Java Method |
It is used to initialize the instance of the object. | It is used to expose the behavior of an object. |
It does not have any return type. | It does have a return type. |
The default constructor is available to initialize the value. | There is no default method is available. |
The constructor's name is the same name as that of the class. | You can or cannot have the same name as that of the class |
Local Variable is declared inside the methods, constructor, and inside any other blocks. The scope of these variables is only inside the block where they are declared. You can not access these variables outside that particular block.
Instance variables are declared inside the class, but not inside the method. These variables are bounded to its object itself. Each object is creating its individual copy. It won't affect any other variable if we are making changes in one instance.
The final keyword has a very special meaning in java. It has a different meaning in a different concept:
final variable: The value of the variable can not be modified once it is assigned. When a final keyword is used with the class variable then only the constructor can assign value to the final variable.
final method: You cannot override the method if it is declared with the final keyword.
final class: The class cannot be extended to any other class but it can extend other classes.
The Multiple inheritance increase the complexity in the Java language. In multiple inheritance, the properties of two classes are inherited in one child class. For example, there are three classes A, B, and C. If A and B have the same methods and you are creating an object of the child class, then there will be a problem when you are calling methods of class A and class B.
Java shows a compile-time error if two classes are inherited. It does not matter whether the methods are different or the same it will give a compile-time error.
Pointers make the language a bit complex and unsafe too. But the java language is known for the simplicity. JVM looks after the implicit memory location. It will avoid direct access to the memory location by the user is more encouraged by Java.
break | Continue |
It is used with conditional(if, if-else, switch) and loop statements(for, while, do-while). | It is used only with the loop statements. |
Once the statement is executed it terminated the moment and come out of the block. | Once the statement is executed it will jump to the next iteration. |
It terminates the innermost loop immediately. | It skips the next set of statements after continue and jump to the next iteration. |
When a method is static, there is no need to create an object to call the method. It will be called automatically. That's why the main method of Java is static. If we create a main method without static, then JVM needs to create an object to call the method. This procedure will acquire extra memory space.
Java does not support a copy constructor like C++. Moreover, there are some concepts that work like copy constructor in java. We can copy the value of one object to another object. The ways you can do:
Example: We have a class of employees.
employees(int i, String name)
{
this.i =i;
this.name=name;
}
Example: employee(employee e)
{
i = e.i;
name = e.name;
}
this() | super() |
It belongs to the current instance of the class. | It belongs to the current instance of the parent class. |
It calls the default constructor of the same class | It calls the default constructor of the parent class. |
It accesses the method of the current class. | It accesses the method of the parent class. |
It points to the instance of the current class. | It points to the instance of the base class. |
One constructor can call another constructor of the class with respect to the current class object. Constructor chaining can be performed inside the same with the help of this keyword.
Example:
public class student
{
int id;
String name;
public student(int id)
{
this.id=id;
}
public student(int id, String name)
{
this(id);
this.name=name;
}
public static void main(String[] args)
{
student s = new student(3, " Ram");
System.out.print("The student id" + id + "and name is" + name);
}
}
Method overloading is a type of polymorphism. Polymorphism allows you to create methods with the same name. There is two way through which you can implement the method overloading:
Method overriding comes under run time polymorphism. There are some conditions in method overriding:
If these conditions are satisfied then run-time will occur polymorphism.
String | String Builder | String Buffer |
String values are stored in a constant string pool. | String Builder is stored in the stack. | String Buffer is also stored in the stack. |
The values can not be changes | We can change the value | We can change the value. |
It is not thread-safe so it is not synchronized. | It is thread-safe so it is synchronized. | |
It is faster in working. | It is slower in working. |
It is used to achieve abstraction. The template of the Interface has only method declaration. It does not have method implementation. Few points are needed to remember about the interface:
Abstract classes are used to achieve abstraction. It is declared by using the "abstract" keyword. It can have both abstract and non-abstract methods. Abstract classes have declaration and implementation of non-abstract methods but the implementation of abstract methods is inside the class where it is extended.
ArrayList is present in java.util package and also the part of the collection framework.
Features of ArrayList:
Abstract Class | Interface |
It has a default constructor. | It does not have any default constructor |
It has both abstract and non-abstract methods. | It has only an abstract method |
It contains instance variables. | It contains only constants |
It is extended in classes. | It is implemented in classes and methods defined in interfaces are implemented in the class. |
A group of objects of classes and interfaces is called collections. It comes under java.util package. It is designed to store objects and manipulate the design to store objects.
Operations that you can perform on collections:
Ordered: The values stored in the collection are the way in which we inserted the values. We can iterate the collections in any specific order.
Sorted: It can be done internally and externally. It is a mechanism that can sort the collections in any particular order.
Set believes in solitary. It does not allow duplicate values. The existing value inserted again will display an error. There are three types of sets that exist in Java:
Encapsulation means combining the properties of class and methods in a single unit. It helps the programmer to follow the modular approach for software development. Each object has its own property and methods and works independently of other objects. It also serves the purpose of data hiding.
A class that has one instance is called the singleton class. All its variables and methods belong to that one instance of the class only. The concept is useful where there is a need to limit the instance of the class.
For achieving abstraction both abstract and the interface is used. The abstract class helps in partial abstraction and interface achieves full abstraction. Let us study the difference between the two in the table given below.
Abstract class |
Interface |
A class that is abstract can have both abstract and non-abstract methods. |
The interface on the other hand can have only abstract methods. However, after Java 8, it can have default and static methods also. |
It does not support multiple inheritances. |
It supports multiple inheritances. |
Abstract class contains final, non-final, static and non-static variables. |
The interface can contain only static and final variables. |
An abstract class can provide an implementation of the interface. |
It cannot implement the abstract class. |
If we want to declare a class as abstract we need to use the abstract keyword. |
To declare interface we use the interface keyword. |
It can extend another Java class and multiple java interfaces can be implemented. |
It can only extend another Java interface. |
The “extends” keyword is used to extend an abstract class. |
“implements “keyword is used. |
Class members can be private, protected etc. |
Class members are by default public. |
Example Public abstract class painting{ Public abstract void draw();} |
Example Public interface Creation{ Void draw();} |