生成几个String的问题

public class Test extends java.lang.Object{
public Test();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   new     #2; //class java/lang/String
   3:   dup
   4:   ldc     #3; //String 34
   6:   invokespecial   #4; //Method java/lang/String."<init>":(Ljava/lang/Strin
g;)V
   9:   astore_1
   10:  return

}

 

这是javap(Disassembles class files)该程序后的:

public class Test {
	public static void main(String args[]) {
		String a = new String("3" + "4");
	}
}

 

可见优化后只生成一个String 34,加new String()就共两个对象生成

这些好知道,但ldc之类的啥意思呢?

 

4:	ldc	#3; //String 34
This line uses the opcode ldc, which loads a constant onto the operand stack. At this point, we're going to load whatever constant is in index #3 of our constant table. That constant is our String, "34".

 

http://www.javaranch.com/journal/200408/ScjpTipLine-javap.html

 

你可能感兴趣的:(java,html)