问题描述
我是JAVA的新手,目前正在学习Oracle教程泛型部分.我认为那里有一个错误,我想确保我没有记错.感谢您的反馈.
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.
我在下面看到了以下解释 https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html
I saw the following explanation athttps://docs.oracle.com/javase/tutorial/java/generics/restrictions.html
请注意,Java编译器自动将8装箱为Integer.valueOf(8),将'a'装箱为Character('a'):
Note that the Java compiler autoboxes 8 to Integer.valueOf(8) and 'a' to Character('a'):
对<整数,字符> p =新对(<>)(Integer.valueOf(8),新字符('a'));
Pair < Integer, Character > p = new Pair<>(Integer.valueOf(8), new Character('a'));
我认为这是一个错误,"a"实际上会自动装箱到 Character.valueOf('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("=============");
}
}
输出为:
通用方法用法:值:一个真的第一==第二是真的
generic method usage:values: a atruefirst == second is true
常规方法用法:值:一个真的第一==第二是真的
regular method usage:values: a atruefirst == second is true
通用类用法:值:一个真的第一==第二是真的
generic class usage:values: a atruefirst == second is true
所以,我认为这表明'a'被自动装箱到Character.valueOf.
So, I think this demonstrates that 'a' is autoboxed to Character.valueOf.
我错过了什么吗?这是检查的正确方法吗?
Am I missing something? Is this the right way to check it?
谢谢.
Eliyahu
推荐答案
是的,自动装箱是使用 valueOf
到处进行的,以利用所有缓存.例如,使用 Character
:
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);
}
这是文档疏忽.
这篇关于Oracle Java教程-有关字符自动装箱Java注释的可能错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!