What are the main concepts of OOPs in Java? (Important)
Object-Oriented Programming or OOPs is a programming style that is associated with concepts like:
-
Inheritance: Inheritance is a process where one class acquires the properties of another.
-
Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit.
-
Abstraction: Abstraction is the methodology of hiding the implementation details from the user and only providing the functionality to the users.
-
Polymorphism: Polymorphism is the ability of a variable, function or object to take multiple forms.
What is Object Oriented Programming?
Object-oriented programming or popularly known as OOPs is a programming model or approach where the programs are organized around objects rather than logic and functions. In other words, OOP mainly focuses on the objects that are required to be manipulated instead of logic. This approach is ideal for the programs large and complex codes and needs to be actively updated or maintained.
What is an object in Java and how is it created?
An object is a real-world entity that has a state and behaviour. An object has three characteristics:
-
State
-
Behaviour
-
Identity
An object is created using the ‘new’ keyword. For example:
ClassName obj = new ClassName();
What is JIT compiler in Java?
JIT stands for Just-In-Time compiler in Java. It is a program that helps in converting the Java bytecode into instructions that are sent directly to the processor. By default, the JIT compiler is enabled in Java and is activated whenever a Java method is invoked. The JIT compiler then compiles the bytecode of the invoked method into native machine code, compiling it “just in time” to execute. Once the method has been compiled, the JVM summons the compiled code of that method directly rather than interpreting it. This is why it is often responsible for the performance optimization of Java applications at the run time.
Why pointers are not used in Java?
Java doesn’t use pointers because they are unsafe and increases the complexity of the program. Since, Java is known for its simplicity of code, adding the concept of pointers will be contradicting. Moreover, since JVM is responsible for implicit memory allocation, thus in order to avoid direct access to memory by the user, pointers are discouraged in Java.
What is a package in Java? List down various advantages of packages.
Packages in Java, are the collection of related classes and interfaces which are bundled together. By using packages, developers can easily modularize the code and optimize its reuse. Also, the code within the packages can be imported by other classes and reused. Below I have listed down a few of its advantages:
-
Packages help in avoiding name clashes.
-
They provide easier access control on the code.
-
Packages can also contain hidden classes which are not visible to the outer classes and only used within the package.
-
Creates a proper hierarchical structure which makes it easier to locate the related classes.
Differentiate between static and non-static methods in Java.
Static Method:
-
The static keyword must be used before the method name.
-
It is called using the class. (className.methodName)
-
They can’t access any non-static instance variables or methods.
Non-Static Method:
-
No need to use the static keyword before the method name.
-
It is can be called like any general method.
-
It can access any static method and any static variable without creating an instance of the class.
Difference Between String , StringBuilder And StringBuffer Classes.
String:
String is immutable ( once created can not be changed ) object. The object created as a String is stored in the Constant String Pool.
Every immutable object in Java is thread safe, that implies String is also thread safe. A string can not be used by two threads simultaneously.
String once assigned can not be changed.
StringBuffer:
StringBuffer is mutable means one can change the value of the object. The object created through StringBuffer is stored in the heap. StringBuffer has the same methods as the StringBuilder, but each method in StringBuffer is synchronized that is StringBuffer is thread safe.
Due to this, it does not allow two threads to simultaneously access the same method. Each method can be accessed by one thread at a time. But being thread safe has disadvantages too as the performance of the StringBuffer hits due to thread safe property. Thus StringBuilder is faster than the StringBuffer when calling the same methods of each class.
String Buffer can be converted to the string by using toString() method.
StringBuffer demo1 = new StringBuffer("Hello"); // The above object stored in heap and its value can be changed .
demo1=new StringBuffer("Bye"); // Above statement is right as it modifies the value which is allowed in the StringBuffer
StringBuilder:
StringBuilder is same as the StringBuffer , that is it stores the object in heap and it can also be modified . The main difference between the StringBuffer and StringBuilder is that StringBuilder is also not thread safe.
StringBuilder is fast as it is not thread safe .
StringBuilder demo2= new StringBuilder("Hello"); // The above object too is stored in the heap and its value can be modified
demo2=new StringBuilder("Bye"); // Above statement is right as it modifies the value which is allowed in the StringBuilder
What is a classloader in Java?
The Java ClassLoader is a subset of JVM (Java Virtual Machine) that is responsible for loading the class files. Whenever a Java program is executed it is first loaded by the classloader. Java provides three built-in classloaders:
-
Bootstrap ClassLoader
-
Extension ClassLoader
-
System/Application ClassLoader
Why Java Strings are immutable in nature?
In Java, string objects are immutable in nature which simply means once the String object is created its state cannot be modified. Whenever you try to update the value of that object instead of updating the values of that particular object, Java creates a new string object. Java String objects are immutable as String objects are generally cached in the String pool. Since String literals are usually shared between multiple clients, action from one client might affect the rest. It enhances security, caching, synchronization, and performance of the application.
.png)