Home > Back-end >  Java new basic types of objects, is on the heap or stack
Java new basic types of objects, is on the heap or stack

Time:01-12

String e=new String (" a ");
String f="a";
System. The out. Println (e==f);
Why the result returns false
If the new object on the stack, should be placed in the constant pool, should be true,
If on the heap, but basic types are not on the stack?

CodePudding user response:

The String in Java is not basic types...

CodePudding user response:

Is a String object,==is equivalent to compare two objects address, save in the stack is reference variables, constant pool is not in a stack is independent of the memory space

CodePudding user response:

Use the String a="xx"; Is a String constant, constant pool of stack, but using the String b=new String (" xx "); Is to new an object, it is stored in the heap,
The above comparison, must return false;

CodePudding user response:

The new object must be in the heap

CodePudding user response:

System memory generally is divided into four
Heap heap objects is also something new out
Stack stack put local variable
Static variables and static segment static area for string constants
Data segement area code for the code
String e=new String (" a ");//e is lying in the heap memory, stack memory storage e corresponding reference address
String f="a";//f in the constant pool stack memory,
The String is not a basic types, directly defined String f="a"; In a constant pool, the new is on the heap memory

CodePudding user response:

New objects, points to the memory address values;
"" take the string from the constant pool;
The result must be false;



For example: String a="a"; String b="A"; Both took it from a constant pool, a==b is true
String a=new String (" a "); String b=new String (" A "); This is two completely two different memory address values, a==b is false

CodePudding user response:

==at the same time also more address values, if not the same object, certainly does not equal, and because of the new object is new allocate a memory address space, so will not equal

CodePudding user response:

String e=new String (" a ");//create two objects: one will be "a" in a constant pool pool (string), a new store references in the stack,
String f="a";//not create objects: look for the constant pool is there "a", have been found, thus directly out from the pool rather than creating a new object,
System. The out. Println (e==f);
//double for basic types of data comparison is value, which is for object references:
//if the output System. IdentityHashCode (e), return should be e the "address" in the stack,
//so e==f should be more e "address" in the stack and f in the "address" in the constant pool (e and f sharing "address") in a constant pool of

CodePudding user response:

Suppose in the main method of class A new instance of class B, the instance that the B is on the stack or heap? On the heap, in theory it is class A local variable,

Or say as long as the NEW object instances, are on the heap? Whether it is in the method body, or, as long as it is NEW, just on the heap,

Outside the method body not from NEW variables? Such as int A. (it is A class A global variable, it should be in the stack or heap),

I'm a bit confused now, in the heap or is based on the data type in the stack? As long as it is the basic data types such as int, char double all of these on the stack,
As long as the reference data type on the heap? String such as custom class instances, for example,

Has nothing to do with global and local?

CodePudding user response:

For example,
The class 1 {
Int a

The main ()
{
Int b}
}

There are the stack of data a to b?

CodePudding user response:

For example,
The class 1 {
A=new class2 ()

The main ()
{
B=new class3 ()}
}

Instances of the class A, B are existing in the heap memory?
  • Related