• Post Reply Bookmark Topic Watch Topic
  • New Topic

bytecode differences, string + and type of char[]  RSS feed

Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to understand byte code generated by bcj and javac with regards to string concatenation.

For the following lines
char ar[] = {'a','b'};
System.out.println("*"+ar+'*'); //second * is a char

bcj byte code when run with java produces

while javac byte code when run with java produces

Looking at the byte code, the key lines seem to be

bcj :
17 new #3 <Class java.lang.StringBuffer>
20 dup
21 ldc #4 <String "*">
23 invokespecial #5 <Method java.lang.StringBuffer(java.lang.String)>
26 aload_1 // that's the char array reference
27 invokevirtual #6 <Method java.lang.StringBuffer append(char[])>
30 bipush 42 // that's the '*'
32 invokevirtual #7 <Method java.lang.StringBuffer append(char)>
35 invokestatic #8 <Method java.lang.String valueOf(java.lang.Object)>
38 invokestatic #8 <Method java.lang.String valueOf(java.lang.Object)>
41 invokevirtual #9 <Method void println(java.lang.String)>

I don't get all of this, but the key bit seems to be that this byte code appends the char array using append(char []), which appends each character

javac does something like this:
21 invokespecial #4 <Method java.lang.StringBuffer()>
24 ldc #5 <String "*">
26 invokevirtual #6 <Method java.lang.StringBuffer append(java.lang.String)>
29 aload_1 // that's the char array reference
30 invokevirtual #7 <Method java.lang.StringBuffer append(java.lang.Object)>
33 bipush 42 // that's the '*'
35 invokevirtual #8 <Method java.lang.StringBuffer append(char)>
38 invokevirtual #9 <Method java.lang.String toString()>
41 invokevirtual #10 <Method void println(java.lang.String)>

And the key bit here seems to me that we append the char array using append(Object), whose toString produces the object's address.

Now, reading http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html
"the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead. "

So, which is right, append(char []), or append(Object) or is this open to interpretation. Or is the question 'what is the type of char[]'? Also interesting to note bcj does valueOf(Object) while javac does toString()

Further, setting
char ar[] = null;
compiles with both bcj and javac, but crashes with a null pointer exception for bcj byte code, and produces *null* for javac code.

author and iconoclast
Posts: 24217
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bcj is wrong, plain and simple. This error has been discussed here before, not that long ago, actually. I think it was in the advanced forum last time. As you quoted from the spec, the result should be as if toString() were called on the array, and that's not what bcj does.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!