问题
I'm new to JAVA, currently learning Oracle tutorial generics section. I think there is a mistake there, and I want to make sure I'm not wrong. I'll appreciate your feedback.
I saw the following explanation at https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html
Pair < Integer, Character > p = new Pair<>(8, 'a');
Note that the Java compiler autoboxes 8 to Integer.valueOf(8) and 'a' to Character('a'):
Pair < Integer, Character > p = new Pair<>(Integer.valueOf(8), new Character('a'));
I think this is a mistake and 'a' is actually auto-boxed to Character.valueOf('a').
I wrote the following program to check it. as I thought maybe generics have special boxing, I tried generic method, regular method and generic class:
class TestCharacter<T>{
public <T> void check( T first, T second){
System.out.println("values: " + first + " " + second);
System.out.println(first.equals(second));
System.out.println( "first == second is " + (first == second) );
System.out.println( "first == second is " + (first == Character.valueOf('a')) );
}
}
public class TestAutoBoxingIssue{
public static <T> void check1( T first, T second){
System.out.println("values: " + first + " " + second);
System.out.println(first.equals(second));
System.out.println( "first == second is " + (first == second) );
System.out.println( "first == second is " + (first == Character.valueOf('a')) );
}
public static void check2( Character first, Character second){
System.out.println("values: " + first + " " + second);
System.out.println(first.equals(second));
System.out.println( "first == second is " + (first == second) );
System.out.println( "first == second is " + (first == Character.valueOf('a')) );
}
public static void main(String[] args){
char first = 'a';
char second = 'a';
System.out.println("generic method usage: ");
check1( first, second );
System.out.println("=============");
System.out.println("regular method usage: ");
check2( first, second );
System.out.println("=============");
TestCharacter<Character> t = new TestCharacter<>();
System.out.println("generic class usage: ");
t.check(first, second );
System.out.println("=============");
}
}
output is:
generic method usage: values: a a true first == second is true
first == second is true
regular method usage: values: a a true first == second is true
first == second is true
generic class usage: values: a a true first == second is true
first == second is true
So, I think this demonstrates that 'a' is autoboxed to Character.valueOf.
Am I missing something? Is this the right way to check it?
Thanks.
Eliyahu
回答1:
Yes, autoboxing is done with valueOf
everywhere to take advantage of any caches. For example with Character
:
public static Character valueOf(char c) {
if (c <= 127) { // must cache
return CharacterCache.cache[(int)c];
}
return new Character(c);
}
It's a documentation oversight.
来源:https://stackoverflow.com/questions/51634863/oracle-java-tutorial-possible-error-regarding-character-autoboxing-java-commen