Java – do we have all the original types of pools in the permgen area of the heap?

I know the concept of string pool in permgen area of heap So when we do something like this

String firstString = "Stack";
String secondString = "Stack";

Both references firststring and secondstring point to the same object in the pool But I try to use variables of type int

int firstInt = 5;
    int secondInt = 5;

    if(firstInt == secondInt) {
        System.out.println("Both point to same allocated memory");
    } else {
        System.out.println("Both point to different allocated memory");
    }

The result is that both point to the same object when I try

Integer firstInteger = new Integer(2);
Integer secondInteger = new Integer(2);

    if(firstInteger == secondInteger) {
        System.out.println("Both point to same object");
    } else {
        System.out.println("Both point to different object");
    }

The output points to different objects

I tried the same char and the results were similar So my question is that we have all primitive types of pools, such as int, char? When we actually create an object with the same content using new (), as in the second case mentioned above, is the object cloned and stored in the same pool area or outside the pool?

Solution

There are many misunderstandings in your post, and it's even difficult to explain Get some decent books At present, there are some facts that may help you:

>String is not the original type, > there is no pool of the original type because there is no reference to the original type (the answer is that it is completely wrong to keep them only on the stack!) > If you use a new one, you bypass the swimming pool anyway; Therefore, executing a new string ("ala") will always create a new string object; You cannot change the new semantics; > If you want to use available pools, use factory methods on objects (such as integer. Valueof), which - to some extent - pool instances (it is not feasible or beneficial to collect all possible integers and floats equivalents)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>