Home > News content

Java in the end is not a pure object-oriented language?

via:博客园     time:2016/6/29 10:00:47     readed:1481

English text:Why Java Is a Purely Object-Oriented Language ... Or Why Not

Java & mdash; & mdash; whether indeed the & ldquo; & rdquo ;? pure object-oriented Let's dig into the Java world, trying to prove it.

I just started learning Java in front of a few years, I know that Java is to follow from a book & ldquo; object-oriented programming paradigm (Object Oriented Programming paradigm) & rdquo; of. Everything is an object in the Java world within, and even strings (String) These are the objects (in C language, strings are arrays of characters), then, I think Java is an object-oriented language.

But then, I have seen on the Internet site many developers say & ldquo; Java is not really a pure object-oriented, because not everything is an object in the Java world & rdquo ;. Many of their arguments can be summarized as the following two points:

  • All static content (static key variables and methods modified) does not belong to any object, so these things are non-objects.
  • All basic types (char, boolean, byte, short, int, long, float, double) are not objects, because we can not do something like normal object has an operation (for example: the use of & ldquo; & rdquo; property to access the object. and methods).

At that time, due to the limited reserves of personal knowledge and experience, I am very content to believe the above argument, and also began to think & ldquo; Java is not a pure object-oriented programming language & rdquo ;.

To more later in my first JVM learning process, I have a new discovery:

JVM is created when the object actually creates two objects:

  • A is an instance of an object.
  • Another is the Class object. The Class object within the JVM will only be loaded once, static methods and static properties of the class are loaded together, JVM using the Class object to create a specific instance of an object (such as object above).

For example, in the following Java statement, there will be two objects are created:

Employee emp = new Employee ();

A is an instance of an object emp; the other is a Class object, we can refer to it by Employee.class; this Class object that owns the static variables and static methods defined by this class all at the same time, if we have access to access static objects by emp content, you will find that it actually points to an object is Employee.class.

It also opened another mystery: Why static content in an object (whether or emp emp2) is changed, also change in another object, because the two objects are changed in the same Employee.class Object contents inside.

Now, the first argument to the above mentioned we have to cancel. Because static content indeed proved to belong to an object.

But we would also like to confirm that the second argument: As mentioned earlier, primitive types are not objects in Java, they can not do the operation similar objects. To solve this problem, Java official for each primitive type launched a corresponding wrapper class (for example: Integer correspond int, Long correspondence long, Character correspond char), so, in fact, we can create a wrapper object to the original type, while they do objects related operations. Furthermore, since the automatic entry box, we can put a primitive type value assigned to reference its corresponding wrapper class. But we still can not do these types of primitive objects Operation & mdash; & mdash; we need to create the corresponding object wrapper classes.


Integer obj = new Integer (5); // here we can do i.toString ();
int i = 5; // but we can not do i.toString () here

So far, from an end user's point of view, we can confirm that & ldquo; the original category is not an object & rdquo ;. (Java is a Java developer end users, because we are using it, rather than creating it).

If you stand JVM perspective, there will be a new discovery:

In fact, it seems that all the JVM & ldquo; primitive & rdquo; is treated as an object & rdquo;, to prove that this can be explained by the Class class source code or Javadoc in Class class.

According to the source code of java.lang.Class class, the class's comment is:

Java official description:

Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.


Examples of Class class for running Java application classes and interfaces. Like enumeration is a class and annotation is a kind of interface. Each array is also reflected as part of an array of all elements with the same type and size of a number of shared class object class. Primitive Java types (boolean, byte, char, short, int, long, float, and double) and the keyword void are also represented as Class objects.

But also according to Javadoc definition of Class.isPrimitive () method, to determine

Java official description:

public boolean isPrimitive ()

Determines if the specified Class object represents a primitive type.

There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as t he primitive types that they represent, namely boolean, byte, char, short, int, long , float, and double.

These objects may only be accessed via the following public static final variables, and are the only Class objects for which this method returns true.


true if and only if this class represents a primitive type



Reference translation:

public boolean isPrimitive ()

Determining whether the specified Class object represents a primitive type.

A total of 9 to set a good Class object representing the primitive type void and corresponding keywords. These objects are created by the JVM. & Hellip;


If and only if this class represents a real basic types

All of the above description, in the JVM inside, in fact, the original type is the object.

When you open the Javadoc forClass The definition of class, by & ldquo; CTRL + F & rdquo; search key & ldquo; primitive & rdquo ;, will find evidence of surface & ldquo; in the JVM, it is the basic type as an object to handle & rdquo ;.

We can look at an example:Integer.TYPEIn this part of the document clearly recorded:

Java official description:

public static final Class & lt; Integer & gt; TYPE

The Class instance representing the primitive type int.

All of the above description, in the JVM inside, in fact, the original type is the object.

So, since that & ldquo; JVM & rdquo; will be created for all the basic type of an object, then why are we so commonly used & ldquo; & rdquo ;, primitive type rather than using the corresponding wrapper class object?

This is because, for the & ldquo; primitive & rdquo; objects created within the JVM is very lightweight, relatively the corresponding wrapper class object we created directly done a lot of optimization; also because lightweight sake, these primitive class functionality is relatively small (for example, we can not call its internal methods, because they have been optimized for internal There is no way a)

Use practical examples to illustrate why we should use the & ldquo; & rdquo ;: the original type

& Ldquo; primitive & rdquo; a faster speed (for example, the following code execution on our machine needs nine seconds, but when I put into Long after long, 0 seconds to complete)

public static void main (String [] args) {
    long millis = System.currentTimeMillis ();
    Long sum = 0L; // uses Long, not long
    for (long i = 0; i & lt; = Integer.MAX_VALUE; i ++) {
        sum + = i;
    System.out.println (sum);
    System.out.println ((System.currentTimeMillis () - millis) / 1000);

& Ldquo; primitive & rdquo; allow us to directly use the & ldquo; == & rdquo; to compare

new Integer (3) == new Integer (3); // false
new Integer (100) == new Integer (100); // false
Integer.valueOf (5) == Integer.valueOf (5); // true
Integer.valueOf (200) == Integer.valueOf (200); // false

We pay attention to see the fourth sentence, the output is really & ldquo; false & rdquo;. This is due to the [-128; 127] the 265 integer interval will be stored JVM cache, so in this section, JVM returns the same object; however, beyond this range, there is no longer JVM cache, will be created new object, so the result is unequal.

So to sum up is: inside the JVM, primitive type as an object is to be handled. But we developers directly to the & ldquo; primitive & rdquo; as objects, developers should use the corresponding package to.

Above it is why I say & ldquo; Java is indeed a pure object-oriented language & rdquo; the validation process. If you have any of the other views, please leave a message in the comment, discuss.


Translation link:

Translation of:Code rural network & Ndash; Dee1024

China IT News APP

Download China IT News APP

Please rate this news

The average score will be displayed after you score.

Post comment

Do not see clearly? Click for a new code.

User comments