Java

Java:String Pooling

Posted on Updated on

  • String Pool in java is a pool of Strings stored in Java Heap Memory. We know that String is special class in java.
  • String pooling concept can be better understood with the below diagram.

String-Pool-Java1

  • Hence when u do not use the new keyword the object is referenced to the same memory location.
Advertisements

Java: Difference between path & Classpath

Posted on Updated on

Difference between path and classpath

path classpath
  • path variable is used to set path for all java software tools like javac.exe, java.exe, javadoc.exe, and so on.
  • Path is an environment variable which is used by the operating system to find the executables i.e setting up environment for operating system
  • Classpath is an environment variable which is used by the Java compiler to find the path, of classes.ie in J2EE we give the path of jar files.
  • classpath variable is used to set path for java classes which are used in our application.

difference between path and classpath

Java: finalize() method

Posted on

  • The java.lang.Object.finalize() is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
  • This method is defined in Object class as:  protected void finalize(){}
  • If an object is created without keyword new we can use finalize method to perform cleanup processing.

Java: toString() method

Posted on

  • toString() method is used to retrieve object information in string format.
  • If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

Let us understand with an example:

class Student{  

 int rollno;  

 String name;  

 String city; 

 Student(int rollno, String name, String city){  

 this.rollno=rollno;  

 this.name=name;  

 this.city=city;  

 }  

 public static void main(String args[]){  

   Student s1=new Student(101,“Raj”,“lucknow”);  

   Student s2=new Student(102,“Vijay”,“ghaziabad”);  

   System.out.println(s1);//compiler writes here s1.toString()  

   System.out.println(s2);//compiler writes here s2.toString()  

 }  

}  

Output:

Student@1fee6fc
Student@1eed786

As you can see in the above example, printing s1 and s2 prints the hashcode values of the objects but I want to print the values of these objects. Since java compiler internally calls toString() method, overriding this method will return the specified values. Let’s understand it with the example given below:

class Student{  

 int rollno;  

 String name;  

 String city;  

  Student(int rollno, String name, String city){  

 this.rollno=rollno;  

 this.name=name;  

 this.city=city;  

 }  

   public String toString(){//overriding the toString() method  

  return rollno+” “+name+” “+city;  

 }  

 public static void main(String args[]){  

   Student s1=new Student(101,“Raj”,“lucknow”);  

   Student s2=new Student(102,“Vijay”,“ghaziabad”);  

   System.out.println(s1);//compiler writes here s1.toString()  

   System.out.println(s2);//compiler writes here s2.toString()  

 }  

}

Output:
101 Raj lucknow
102 Vijay ghaziabad

Java : Does a Try block be followed by catch block

Posted on

  • Each Try block must be followed by either catch or finally.
  • Let us see what happens when you use try block alone

public class Testtry{
public static void main(String args[]){
try{
System.out.println(“code…”);
}
}
}

Output:

error: ‘try’ without ‘catch’, ‘finally’ or resource declarations
try{
^
1 error

  • Thus try must be followed by either catch or finally.

 

exceptionobject

 

 

Java:Can Finally block be used without Catch.

Posted on

The answer is YES.

  • The finally block always executes when the try block exits. So you can use finally without catch but you must use try.

Let us understand this with an example

class TestFinallyBlockWithoutCatch{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}

finally{

System.out.println(“finally block is always executed”);

}
System.out.println(“rest of the code…”);
}
}

Output:

Exception in thread “main” java.lang.ArithmeticException: / by zero
at TestFinallyBlock1.main(TestFinallyBlock1.java:4)

finally block is always executed
finally

 

Java:Difference between JDK, JRE and JVM

Posted on Updated on

JAVA DEVELOPMENT KIT:
  • The Java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc) and other tools needed in Java development.

JAVA RUNTIME ENVIRONMENT

  • JRE stands for “Java Runtime Environment” and may also be written as “Java RTE.” The Java Runtime Environment provides the minimum requirements for executing a Java application; it consists of the Java Virtual Machine (JVM), core classes, and supporting files.

JAVA VIRTUAL MACHINE

  • specification where working of Java Virtual Machine is specified. But implementation provider is independent to choose the algorithm. Its implementation has been provided by Sun and other companies.
  • An implementation is a computer program that meets the requirements of the JVM specification
  • Runtime Instance Whenever you write java command on the command prompt to run the java class, an instance of JVM is created.
  • The JVM doesn’t understand Java source code, that’s why you compile your *.java files to obtain *.classfiles that contain the bytecodes understood by the JVM. It’s also the entity that allows Java to be a “portable language” (write once, run anywhere).

difference