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));
}
}
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.
Since there are 8 primitive types in Java it is not purely object oriented language. But primitive types make Java more efficient.
Because primitive types are auto-boxed(in java terms) to objective types. For example, int are wrapped in a Integer Object.
Java is Purely Object Oriented because every thing in Java is treated as an Object. However, Java is not purely Object Oriented because still it supportd primitive data types that violates the OOPs philosophy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With