Well as we know that there is lots of discussion done when it comes to memory allocation for the String class Objects.As we have the concept of "constant pool" and "non-constant pool",the allocation of memory is quit different and really gets confusing some times.
Even in the scenarios like using String class and StringBuffer class,we all know that its better to use the StringBuffer object compared to that of String class as we can append some string despite of concatinating it,we do not actually know why its better but we implement it that way only.
We can obtain the best possible solution to these problems if we can see the bytecode generated after translation of the .java file to .class file.
Well java is not behind even in this aspect of it.Sun has provided a great tool called "javap" command.
Now what javap command does is,it takes the .class file name and prints the information about the class:access specifers,method signature,classes used with fully qualified package...
Also if you use option c with this command we get a different representation....
what it does??well it does not decompile the bytecode but it disassembles the code and gives the bytecode instruction specified by the java virtual mechine.....
Well take a look at this example....String class objects for constant and non constant pool.....
package com.lara;
public class Example
{
public static void main(String[] args)
{
}
void usingStringConstantPool()
{
String s = "java";
s = s+"geek";
System.out.println(s);
}
void usingStringNonConstantPool()
{
String s1 = new String("java");
s1 = s1+"geek";
System.out.println(s1);
}
}
now see this,what happens next...
E:\classFiles\com\lara>javap -c Example
Compiled from "Example.java"
public class com.lara.Example extends java.lang.Object{
public com.lara.Example();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: return
public static void main(java.lang.String[]);
Code:
0: return
void usingStringConstantPool();
Code:
0: ldc #2; //String java
2: astore_1
3: new #3; //class java/lang/StringBuilder
6: dup
7: invokespecial #4; //Method java/lang/StringBuilder."":()V
10: aload_1
11: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
14: ldc #6; //String geek
16: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
19: invokevirtual #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
22: astore_1
23: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream;
26: aload_1
27: invokevirtual #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
30: return
void usingStringNonConstantPool();
Code:
0: new #10; //class java/lang/String
3: dup
4: ldc #2; //String java
6: invokespecial #11; //Method java/lang/String."":(Ljava/lang/String;)V
9: astore_1
10: new #3; //class java/lang/StringBuilder
13: dup
14: invokespecial #4; //Method java/lang/StringBuilder."":()V
17: aload_1
18: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
21: ldc #6; //String geek
23: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
26: invokevirtual #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
29: astore_1
30: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream;
33: aload_1
34: invokevirtual #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
37: return
}
now the stuff that you see is more of the JVM specified instructions,so we won't go to that part but we can really make out from the out put that there is new operator being used by the second function twice hence 2 objects are created whereas in first case there is only one object created....
and that's perfectly fine with the theory about these....
But what a command,we can really have own proof of what exactly happens.....
There are other options also available..you can go through the API....one more interesting option is l which prints the local variable and out line table...
No comments:
Post a Comment