advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 2.9/5 | Rate this item | 16 users have rated this item.
Expertise: Advanced
Language: Java
September 20, 2005
View the Bytecode Contained in a Java Class
If you look in the bin directory of a regular Java2 SDK installation, you'll find a 'javap' application (javap.exe), which you can use as a disassembler by adding the -c command-line option. This allows you to view the bytecode inside a Java class. A Java class consists of assembly-code-like instructions to the Java virtual machine. For example, consider the following code:

class HelloWorldClass
{
public static void main (String args[])
{
System.out.println ("Hello, world!!!");
}
}
Executing javap -c HelloWorldClass gives the following output:

Compiled from "HelloWorldClass.java"
class HelloWorldClass extends java.lang.Object{
HelloWorldClass();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String Hello, world!!!
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}
Viewing the bytecode of a particular class can give you a lot of info about its core internals. For example, consider the following common string concatenation method:

public String strCat (String one, String two)
{
return (one + two);
}
Disassembling this results in the following bytecode:

public java.lang.String strCat(java.lang.String,java.lang.String);
Code:
0: new #7; //class StringBuffer
3: dup
4: invokespecial #8; //Method java/lang/StringBuffer."<init>":()V
7: aload_1
8: invokevirtual #9; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)
Ljava/lang/StringBuffer;
11: aload_2
12: invokevirtual #9; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)
Ljava/lang/StringBuffer;
15: invokevirtual #10; //Method java/lang/StringBuffer.toString:()Ljava/lang/String;
18: areturn
The preceding code proves that strings are immutable; therefore you should always perform string concatenation using a StringBuffer rather than simply concatenating strings.
Jospeh Fernandez
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement