Wednesday, May 14, 2014

What is the output for the below piece of Code? #3

Leave a Comment

Question:


class Simple{

 public static void main(String args[]){

   String s=50+30+"Sachin"+40+40;

   System.out.println(s);//80Sachin4040

 }

}


Output:

80Sachin4040
Read More...

What is the output for the below piece of Code? #2

Leave a Comment

Question:


class Simple{

 public static void main(String args[]){

   String s=50+30+"Sachin"+40+40;

   System.out.println(s);//80Sachin4040

 }

}

Output:

80Sachin4040
Read More...

What is the output for the below piece of Code? #1

Leave a Comment

Question :


public class StringTest {
 

    public static void main(String args[])

    {

        String s = "sachin";

        s.concat("tendulkar");

        System.out.println(s);

    }

}


Output:

sachin

Because String is immutable in Java.


Question :


public class StringTest {

  

    public static void main(String args[])

    {

        String s = "sachin";

        s=s.concat("tendulkar");

        System.out.println(s);

    }

}


Output:

sachintendulkar

Here a new string "sachintendulkar" is created and 's' is made as a reference to this new string.
String object "sachin" still exist in the string constant pool.



Read More...

Tuesday, May 13, 2014

static blank final variable

Leave a Comment
A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.
Read More...

What is blank or uninitialized final variable?

Leave a Comment
A final variable that is not initialized at the time of declaration is known as blank final variable.
If you want to create a variable that is initialized at the time of creating object and once initialized may not be changed, it is useful. For example PAN CARD number of an employee.
It can be initialized only in constructor.
Read More...

Can we override static method?

Leave a Comment
NO
Read More...

Can we overload main() method?

Leave a Comment
YES
Read More...

What if the static modifier is removed from the signature of the main method?

Leave a Comment
Program compiles. But at runtime throws an error "NoSuchMethodError".
Read More...

Restrictions for static method

Leave a Comment
There are two main restrictions for the static method. They are:
  1. The static method can not use non static data member or call non-static method directly.
  2. this and super cannot be used in static context.
Read More...

If no arguments are passed on the Commandline...

Leave a Comment
String array of main method will be empty, NOT NULL!
Read More...

Is Empty .java file name a valid source file name?

Leave a Comment
YES.

  • Save a java file as ".java
  • Compile it as "javac .java"
  • Run it as "java className" [where className is the name of your java class within the file .java]

Read More...

Memory Areas allocated by JVM

Leave a Comment
  1. Class (Method) Area - Stores class structures such as runtime constant pool, method data, method code etc.
  2. Heap - Runtime memory area where objects are allocated.
  3. Stack - Holds local variables, method call/retun data.
  4. Program Counter Register - Stores address of JVM instruction currently being executed.
  5. Native Method Stack - Stores all native methods used in the application.
Read More...