Name: tb29552 Date: 08/13/97
company - Baan Labs / Baan Company N.V. , email - ###@###.###
/*
According to the Java Language Specification
(First printing, August 1996) section 15.17.1.1,
the implicit conversion from a character array
to a String necessary for the + operator
"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."
However, as demonstrated below, in JDK 1.1.3 the
conversion is not done through toString but by
interpreting a char[] as a String of characters.
Note that the bug also shows up when the char[] is
the first and the String is the second argument to
the + operator.
This bug is probably also present in JDK versions
prior to 1.1.3, but I have not checked.
*/
public class CharArrayBug
{
public static void
main(String[] args)
{
// Initialization
String s = "polymorph";
char[] ca = {'i', 's', 'm'};
// This concatenation goes wrong
String concat = s + ca;
// Show it
String concatA = s + ca.toString();
String concatB = s + new String(ca);
if (concat.equals(concatA))
System.out.println("Correct!");
else if (concat.equals(concatB))
System.out.println("Bug! (Expected in JDK 1.1.3.)");
else
System.out.println("Unexpected bug!!");
}
}
company - Baan Labs / Baan Company N.V. , email - ###@###.###
======================================================================
- relates to
-
JDK-4250269 concatenation of char[] to String (using +) : new behavior with 1.2.x javac
-
- Closed
-