Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Java
Expertise: Advanced
Sep 20, 2005



Application Security Testing: An Integral Part of DevOps

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{
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
   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);
   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;)
   11:  aload_2
   12:  invokevirtual   #9; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)
   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 and Steve Simon
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date