Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is java Purely Object Oriented?

Tags:

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));
    }
}
like image 607
NaveenKumar1410 Avatar asked Mar 18 '13 05:03

NaveenKumar1410


4 Answers

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.

like image 161
rra Avatar answered Nov 04 '22 17:11

rra


Since there are 8 primitive types in Java it is not purely object oriented language. But primitive types make Java more efficient.

like image 45
Evgeniy Dorofeev Avatar answered Nov 04 '22 17:11

Evgeniy Dorofeev


Because primitive types are auto-boxed(in java terms) to objective types. For example, int are wrapped in a Integer Object.

like image 2
yehe Avatar answered Nov 04 '22 16:11

yehe


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.

like image 2
Kumar Shorav Avatar answered Nov 04 '22 18:11

Kumar Shorav