开发者

Vector.add('Y') valid in java 1.4?

In the following code

Vector v = new Vector();
v.add('c');

v.add('c') is allowed in 1.5 and I thought it's may be because of autoboxing feature of 1.5.

But I was surprised to see that in 1.4 v.add('c') a character literal is allowed as an object type method parameter of the add method.

Does this mean character literal considered as an object?


before posting question here I crosschecked on my Intellij IDE by doing "Integer i = 1" and IDE showed red error symbols over the line. but IDE failed to do so on v.add('Y') statement. It didn't show any error. Anyways its clear now. v.add('Y') doesn't compile in 1.4 or older version. Thanks for your inputs. I believe sometimes its better to go to shell and check th开发者_C百科ere. hmmm...


I'm not sure how you compiled it, but it shouldn't be allowed. Are you sure you have compiled it in 1.4?

Here's what I'm getting

$ javac -target 1.4 -source 1.4 X.java
X.java:4: cannot find symbol
symbol  : method add(char)
location: class java.util.Vector
        v.add('Y');
         ^
1 error


It doesn't compile with source compatibility set to 1.4:

javac -source 1.4 VectorTest.java
VectorTest.java:6: cannot find symbol
symbol  : method add(char)
location: class java.util.Vector
        v.add('x');
     ^
1 error


@beny23 and @ataylor have correctly pointed out you didn't really compile in Java 1.4 or use the correct flags to do the equivalent in a later version of Java.

I'd just like to note that the code wouldn't run on a real Java 1.4 JVM, no matter what you did to compile it. Autoboxing a char is actually just "syntactic sugar" for a call to Character.valueOf(char). The problem is that that method does not exist the Java 1.4 version of Character. According to the javadoc, the method was added in Java 1.5.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜