[英]What is the difference between a class wrapper and Java, and a singleton in Objective-C?
[英]What is the difference between a normal class object and a wrapper class object in java
通常,当我尝试使用System.out.println();
打印对象时System.out.println();
class Car {
String color = "red";
}
class Main {
public static void main(String[] args) {
Car car = new Car();
System.out.println(car);
}
}
输出类似于:
Car@677327b6
它的class name
+ '@'
+ hashCode
。 在内部,它正在调用toString()
方法。 好像很好 但是当我实现自动装箱如下时会发生什么:
class Main {
public static void main(String[] args) {
int i = 100;
Integer obj = i;
System.out.println(obj);
}
}
这里的输出是100
。 为什么它不像Main@hexcode
? 我以为我会将原始i
转换为Integer
类型的对象。 请纠正我。
Class @ hashCode是Object.toString()
的默认返回值。 Integer
类覆盖toString()
。
public String toString()
返回表示此
Integer
值的String
对象。 该值将转换为带符号的十进制表示形式,并以字符串形式返回,就像将整数值作为toString(int)
方法的参数给出一样。返回值:
以10为基数的此对象的值的字符串表示形式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.