Why Java is not a purely Object-Oriented Language?

A language that supports or has features to treat everything inside the program as objects can be called a Purely Object-Oriented Language, Fully Object-Oriented Language or Completely Object-Oriented Language. The primitive data types like int, char, float, bool, etc must not be supported by a Purely Object-Oriented Language. For a programming language to be pure Object Oriented, the below qualities must be satisfied.

  • Encapsulation/Data Hiding
  • Inheritance
  • Polymorphism
  • Abstraction
  • All predefined types are objects
  • All user-defined types are objects
  • All operations performed on objects must be only through methods exposed to the objects.

One such programming language is Smalltalk.

Why Java is not a Pure Object-Oriented Language?

Java though supports the Encapsulation/Data Hiding, all user-defined types to be objects, Inheritance, Polymorphism, and Abstraction properties of an object-oriented language but not supports the other two properties, i.e, all predefined types are objects and all operations performed on objects must be only through methods exposed to the objects. Java supports the below properties that forbid it to become a Pure Object-Oriented:

Primitive Data Type ex. int, long, bool, float, char, etc as Objects:

The predefined types are non-objects i.e primitive types in Java.

Example:

public class Main 
public class Main 
{ 
    public static void main(String[] args) 
    { 
            int x = 5; 
            System.out.print(x);
    } 
}

Output:

5

In Smalltalk, however, there is no difference between values which are objects and values which are primitive types. The integers, booleans, and characters which are considered as primitive values are also objects in Smalltalk. Thus it is known as a “pure” object-oriented programming language, unlike Java and C++.

The static keyword:

A class can be used without the use of an object in Java when it is declared as static. The object-oriented feature thus defied when the function or variable can not be called by using dot(.) or class object when the static function or static variable is used.

Wrapper Class:

To convert primitive into object and object into primitive, the mechanism facilitated by the Wrapper class is used. Thus, the Integer, Float, etc. can be used in Java instead of int, float, etc and the objects can be communicated with, without calling their methods, using the wrapper classes.

Example: Using arithmetic operators:

public class Main 
{ 
    public static void main(String[] args) 
    { 
            String str = "XYZ" + "X" ;
            System.out.println(str); 
    } 
}

Output:

XYZX

Java however still does not become a pure Object-Oriented Programming language. The reason is simple. Internally the operations like Unboxing and Autoboxing will be used by Java. Thus, under the hoods, Java is going to use primitive type int only, even if Integer is created instead of int and a mathematical operation is performed on it.

Example:

public class Example 
{ 
    public static void main(String[] args) 
    { 
            Integer x = new Integer(100); 
            Integer y = new Integer(120); 
            Integer z = new Integer(x.intValue() + y.intValue()); 
            System.out.println(z); 
    } 
}

Output:

220

Explanation:

In the above example, the OOP language properties fail in two ways:

  • The primitive type “int” i.e. numbers 100 and 120 are used, while creating Integer class.
  • The primitive type “int” is used by Java, while doing addition.
Please Share