本文介绍了java纯粹面向对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们说java不是纯粹的面向对象,因为原始数据类型不是对象。但是在下面的代码中,对象如何保存原始数据类型?
We are saying that java is not purely object oriented since primitive data types are not objects.But in below code how object is holding primitive data type?
public class Test{
public Object meth(Object obj){
System.out.println(obj instanceof Object);//It prints true
System.out.println("Value = "+obj);//It prints "Value = 1"
return obj;
}
public static void main(String[] args) {
int a = 1;
System.out.println(new Test().meth(a));
}
}
推荐答案
这是称为。基本上,当您在需要它们作为对象的上下文中使用它们时,Java编译器会将原始数据类型转换为对象。
It's called autoboxing. Basically, the Java compiler converts primitive data types into objects for you when you use them in a context that requires them to be objects.
这篇关于java纯粹面向对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!