Posts

Initialize variable with Method

1. Static ====================================== public class TestClass3 {     static int x = me();     static {         x = x + 10;         System.out.println("Static Block : "+x);     }     static int me(){         System.out.println("Staic Instance");         return 10;     }     public static void main(String[] args){         System.out.println("Main Method");     } } ******************OUTPUT***************** Staic Instance Static Block : 20 Main Method ====================================== 2. Non-Static ====================================== public class TestClass4 {     int i = me();     private int me() {         return 20; ...

Java : String

Top 35 Java String Interview Questions And Answers : 1) Is  String  a keyword in java? No.  String  is not a keyword in java.  String  is a final class in  java.lang  package which is used to represent the set of characters in java. 2) Is  String  a primitive type or derived type? String  is a derived type. 3) In how many ways you can create string objects in java? There are two ways to create string objects in java. One is using  new  operator and another one is using string  literals . The objects created using new operator are stored in the heap memory and objects created using string literals are stored in string constant pool. String s1 = new String("abc"); //Creating string object using new operator String s2 = "abc"; //Creating string object using string literal 4) What is string constant pool? String objects are most used data objects in Java. Hence, java has a s...