| Author |
bytecode differences, string + and type of char[]
|
Juan Tanamera
Greenhorn
Joined: May 05, 2005
Posts: 4
|
|
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 *ab* while javac byte code when run with java produces *[C@4abc9* 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 whereas 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. Juan
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
|
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.
|
[Jess in Action][AskingGoodQuestions]
|
 |
 |
|
|
subject: bytecode differences, string + and type of char[]
|
|
|