这两者之间有什么区别。我知道Boxing正在将原始值转换为引用。什么在扩大。还应该做什么序列第一次拳击应该完成或加宽?
答案 0 :(得分:9)
扩展正在用更宽的类型转换另一个变量 可以使用原始类型或引用类型进行扩展。
例如:
String
- > Object
int
- > long
正如JLS所述:
一个拳击转换(§5.1.7) [是]可选地后跟一个加宽的引用转换
答案 1 :(得分:9)
答案 2 :(得分:4)
扩展是将byte
分配给int
。即你正在扩大数据类型。
顺序必须为boxing
,然后widening
。
你不能加宽然后框(int不能很长)。
您 CAN 框然后加宽(int可以通过整数成为对象)
注意:突出显示的字词来自Sun Certified Java Programmer SCJP 6 - Kathy Sierra
答案 3 :(得分:2)
答案 4 :(得分:1)
扩展是将数据类型扩展为更广泛的类型。 Boxing是将原始数据类型包装到容器对象中,以便它可以在泛型中使用,主要是集合。 例如:
public class Widening{
public static void main(String[] args) throws Exception {
int test = 20;
myOverloadedFunction(test);
}
//static void myOverloadedFunction(long parameter) {
//System.out.println("I am primitive long");
//}
static void myOverloadedFunction(Integer parameter) {
System.out.println("i am wrapper class Integer");
}
}
输出:
i am wrapper class Integer
(int包含在Integer容器中)
现在让我们取消注释另一个重载方法,然后看看:
public class Widening{
public static void main(String[] args) throws Exception {
int test = 20;
myOverloadedFunction(test);
}
static void myOverloadedFunction(long parameter) {
System.out.println("I am primitive long");
}
static void myOverloadedFunction(Integer parameter) {
System.out.println("i am wrapper class Integer");
}
}
输出:I am primitive long
编译器优先级正在扩大自动装箱。
答案 5 :(得分:0)
扩展将基元或非基元转换为更宽的类型(即可以容纳更多字节的类型)。
示例:
short -> int
String -> Object
但是,int -> Integer
并没有扩大;这是拳击。加宽比拳击具有更高的优先级。加宽和拳击也不能一起完成,即
int -> Long // cannot be done - both widening and boxing
int -> long // can be done - only widening
答案 6 :(得分:0)
我认为订单非常吸引人。我做了下面的游乐场,看看每个可能的组合。这是我的职责:
static void doSomeThing(short i) {
System.out.println("short");
}
static void doSomeThing(short... i) {
System.out.println("short...");
}
static void doSomeThing(Short i) {
System.out.println("SHORT");
}
static void doSomeThing(Short... i) {
System.out.println("SHORT...");
}
static void doSomeThing(long i) {
System.out.println("long");
}
static void doSomeThing(long... i) {
System.out.println("long...");
}
static void doSomeThing(Long i) {
System.out.println("LONG");
}
static void doSomeThing(Long... i) {
System.out.println("LONG...");
}
static void doSomeThing(int i) {
System.out.println("int");
}
static void doSomeThing(int... i) {
System.out.println("int...");
}
static void doSomeThing(Integer i) {
System.out.println("INTEGER");
}
static void doSomeThing(Integer... i) {
System.out.println("INTEGER...");
}
static void doSomeThing(Object i) {
System.out.println("Object");
}
static void doSomeThing(Object... i) {
System.out.println("Object...");
}
规则:
1.Searches for exactly the same type (int -> int)
2.Widening (int -> long)
3.Boxing (int-> Integer, it is NEVER possible to implicit box AND wide (int -> Long NOT possible without cast))
!!Multiple boxing go BEFORE var args!!
int -> Object will be chosen before int -> int...
4.Var args (int -> int...)
5.Widening + var args (int -> long...)
6.Boxing + var args (int -> Integer...)
7.Boxing + widening + var args (int -> Object...)
public class Main{
public static void main(String...args) {
//primitive int
int i = 0;
doSomeThing(i); //int
//commented out doSomeThing(int i){}
doSomeThing(i); //long. It is not possible to narrow, so short, short... Short and Short... will NEVER be called when the input is larger than a short.
//commented out doSomeThing(long i){}
doSomeThing(i); //INTEGER
//commented out doSomething(Integer i){}
doSomeThing(i); //Object. Notice that there can be multiple boxing before moving to var args
//Error occured: compiler if confused: can either execute int..., long..., Object... or Integer...
//Object... and Integer... are commented out, because in the real world int... will be called first
doSomeThing(i); //int...
//commented out int...
doSomeThing(i); //long...
//commented out long... and uncommented Integer...
doSomeThing(i); //Integer...
//commented out Integer... and uncommented Object...
doSomeThing(i); //Object...
//Integer
//Integer
Integer i = new Integer(0);
doSomeThing(i); //INTEGER
//commented out doSomeThing(Integer i)
doSomeThing(i); //Object
//commented out doSomeThing(Object i)
doSomeThing(i); //int
//commented out doSomeThing(int i)
doSomeThing(i); //long so NOT int... it goes widening again
//commented out doSomeThing(long i)
//Error occured: compliler refused: not both have int..., long..., Integer... and Object...
//int... and long... are commented out
doSomeThing(i); //INTEGER...
//commented out doSomeThing(Integer... i)
doSomeThing(i); //Object...
//commented out doSomeThing(Object... i)
//uncommented doSomeThing(int... and long...)
doSomeThing(i); //int...
//uncommented doSomeThing(int... i)
doSomeThing(i); //long...
}