Java Basics Interview Perspective

Basic concepts:

1. Interface

2. Java String Concepts 

3. Difference between StringBuffer & StringBuilder

4. Immutable Concept

-------------------------------------------------------------------------------

 1. Interface : The Interface = 100% ABSTRACTION method

  • Methods are : public & abstract (BY DEFAULT)
  • Variable : public , static & final (BY DEFAULT)

And class implements multiple INTERFACE and MUST NEED TO define (implement) methods inside the class otherwise we get "COMPILE TIME ERROR



2. SUPER Keyword (Super() )

The use of the SUPER keyword is to invoke the constructor of the parent class. 
So as in the down example, the Object created for the SubClass itself will FIRST invoke parent class NO ARG CONSTRUCTOR "SuperConst()" and then Child Constructor.


public class SuperConst {
SuperConst(){
System.out.println("The parent class constructor with No Args");
}
SuperConst(String str){
System.out.println("The parent class constructor with  Args "+str);
}

}

public class SubClassConst extends SuperConst {

SubClassConst(){
System.out.println(" This is sub class const with No Args");
}
SubClassConst(String str){
System.out.println(" This is sub class const with Args: "+str);
}
public static void main(String [] args) {
SubClassConst sb = new SubClassConst("hello");
}
}

ITS Return:



If we use super() in subclass : So will compiler will not invoke parent class Automatically 



Its Returns: The With Arguments Constructor / No arg constructor 



2. Java STRING CONCEPT memory:

HEAP Memory: When JVM performs an action during RUN TIME then-new Object will be created.
SCP Memory: When JVM performs an action during COMPILE TIME / using only String Literal then SCP memory will be used



String s1 = "ANKUR";  

String s2 = s1.toUpperCase();   or s1.toString(); //Since no action is performing it is taken care during compile time so JVM check SCP first if no object present then creates only on SCP.

String s3 = s1.toLowerCase(); //Since there is action taking place by JVM during RUNTIME then create memory new object on HEAP.

Sop(s1= = s2); //True  
Sop(s1= = s3); //False





package RevProg;

public class Strngtest {

public static void main(String [] args) {
// (Create One objects at HEAP & One Object at SCP) 
String s1 = new String("HelloWorld");  
// (Create One objects at HEAP & SINCE the same Object at SCP already from S1 so will not create again on SCP)
String s2 = new String("HelloWorld");  
System.out.println(s1==s2);  // false   
//(since both are pointing to two diff objects  at HEAP)
String s3 = s2;  // Both will point at same object at HEAP
System.out.println(s2==s3); // True
System.out.println(s2);  // Return from HEAP only
// Check first is it available on SCP if yes then no new Object at Heap and Will point only at same SCP (of old S1)
String s4 = "HelloWorld";  
System.out.println(s4==s3);
String s5 = s4;  // Storing into another variable
System.out.println(s4==s5); // True

System.out.println(s2==s5); // False
String s6 = "Hello";   // this will object on SCP
String s7 = s6 + "World"; // this will creat object on SCP ("World") and HelloWorld object on Heap with refrence s7
System.out.println(s7==s4);  // False
String s8 = "Hello"+"World";// Concat will taken care by JVM and complie on compile time and No need to create new Object on HEAP if that already present on SCP.
System.out.println(s8==s4); //True
final String s9 = "Hello"; // Final value will taken care by JVM and complie on compile time and No need to create new Object on HEAP if that already present on SCP.
String s10 = s9 + "World";
System.out.println(s10==s4); //True
}
}

3. Difference Betweeen StringBuffer and StringBuilder:



4. Immutability:

Strings are Immutable and other than that Wrapper Class are also Immutable : Integer/Float/Double/Byte/Long/Boolean/Character










Comments

Popular posts from this blog

The self healing automation framework - Healenium Updated Dec2023

Heleanium - Web without using Docker