03 Month
03 Hours
Yes java is platform independent we can write code in one platform and run any platform present.
No java is not purely object oriented, it supports primitive data types also int, short, char where as in object oriented every thing will be happen through object only.
| JDK | JRE | JVM |
|---|---|---|
| JDK is platform dependent | JRE is platform dependent | JVM is platform independent |
| JRE + Development tool | JVM + libraries to run the application | JVM only runtime environment for executing the JAVA byte code |
| JDK is primary used for code execution and has primary functionality of development | JRE is majorly responsible for creating environment for the code execution | JVM on the other hand specifies all the implements and responsible to provide implementation to JRE |
Class Loader is an abstract class. it belongsto Java.lang package.
It defined as process of loading the class file from secondary storage into JVM memory.
Static is class level keyword which always loaded with class loading time do not wait for object creation and initialization.it is used with variable, method and block.
Static int I =10;
Static String str=”name”;
Public static void Method() {}
//code
Main() {}
method()// direct access no need to define along with object reference
Final is a restriction keyword which restrict the access of data to the user
Super keyword inherit the property from parent class to child class using super keyword
Main() method is static by nature because it is loaded once class loaded as it is class level and not need object initialization to call the method ,which will save the memory and execute the code faster.
String is immutable by nature, it stores the value in string pool, when new value store in string pool it will check the object refence, if the value is same then assign the same object reference. if string is non immutable so value will be changing every time which cause the problem for security aspect and also class loading.
| String | StringBuilder | StringBuffer |
|---|---|---|
| String is Immutable | StringBuilder is mutable | StringBuffer is mutable |
| String stores its value in String Pool which is part of heap memory | StringBuilder stores in heap memory | StringBuffer stores its value in heap memory |
| String is non threadsafe and synchronized | StringBuilder is good for single threaded application | StringBuffer is good for multithreaded application |
| String is slower in comparision to StringBuilder | StringBuilder is faster in execution | StringBuffer is slower in execution in comparision to StringBuilder |
.equals method compare the value or content of String and “==” compare the the object reference of the string object.
String s1 = "abc"; String s2 = "abc"; String s3 = new String("abc"); if (s1 == s2) { return true; } else if (s1.equals(s2)) { return true; } else if (s1 == s3) { return false; } else if (s1.equals(s3)) { return true; }
| Constructor | Method |
|---|---|
| Constructor can not have return type | Method must have return type |
| Constructor used by JVM to initialize the object | Method used for specific purpose |
| Constructor name must be same as class name | Method name may or may not be same as class name |
| If no constructor created JVM create default constructor with no parameter | If method is not created JVM not create default constructor |
Constructor chaining is calling one constructor from other constructor.
Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class.We can use this private constructor in the Singleton Design Pattern
| Access Modifier |
Within class |
Within package |
Outside package |
Outside package |
|---|---|---|---|---|
| Private | Y | N | N | N |
| Default | Y | Y | N | N |
| Protected | Y | Y | Y | N |
| Public | Y | Y | Y | Y |
Java support primitive datatypes. The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.
| Primitive Type | Wrapper class |
|---|---|
| boolean | Boolean |
| char | Character |
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| Volatile | Transient |
|---|---|
| Volatile keyword is used to make class thread safe like synchronization | Transient keyword is used to prevent variable from serialization |
| Volatile keyword used during Multithreading | Transient Keyword is used during Synchronization |
| implements changes directly in main method | exclude variable during synchronization |
| Volatile are not initialized with a default value | Transient variables are initialized with a default value |
| used with a static variable | used with the static keyword |
| used with the final keyword | used with the final keyword |
Serialization in Java is a mechanism of writing the state of an object into a byte-stream. For serializing the object, we call the writeObject() method of ObjectOutputStream class, and for deserialization we call the readObject() method of ObjectInputStream class. Serializable is a marker interface (has no data member and method). It is used to “mark” Java classes so that the objects of these classes may get a certain capability. The Cloneable and Remote are also marker interfaces
To make any immutable we have to perform following steps:
The object cloning is a way to create exact copy of an object. The clone() method of Object class is used to clone an object.
The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don’t implement Cloneable interface, clone() method generates CloneNotSupportedException.
Example :
class Student18 implements Cloneable { int rollno; String name; Student18(int rollno, String name) { this.rollno = rollno; this.name = name; } public Object clone() throws CloneNotSupportedException { return super.clone(); } public static void main(String args[]) { try { Student18 s1 = new Student18(101, "amit"); Student18 s2 = (Student18) s1.clone(); System.out.println(s1.rollno + " " + s1.name); System.out.println(s2.rollno + " " + s2.name); } catch (CloneNotSupportedException c) {} } }
Marker interface is an interface that does not contain methods, fields, and constants is known as marker interface.
Marker interface is used by JVM and compiler have additional information about an object. The Serializable and Cloneable interfaces are the example of marker interface. In short, it indicates a signal or command to the JVM.
public interface Serializable
{
//marker interface
}
This section we will cover all topic wise java question
Visit