If I have 2 reference types with the same value, does that mean only 1 object in memory

I am new to Java and have read a controversial statement about what I believe in. Please consider the following code.

String str1 = "dave"; String str2 = "dave"; 

Both str1 and str2, although unique variables, refer to the same value. So how many unique objects are created in memory? 1 or 2, and can someone explain why?

+4
source share
5 answers

In your example, they refer to the same object because the strings are interned .

In general, using new creates new objects using:

 String str1 = new String("dave"); String str2 = new String("dave"); 

will create two different objects on the heap.

+14
source

You have one unique Object and 2 links pointing to the same object. This happens as a result of String (or interning) joining. Given that both String literals have identical content, the only way to provide the ability to create 2 separate Objects would be to explicitly access one of the String constructors.

+9
source

It is not that difficult. If you are talking about Strings -)

First, let's ignore strings and accept this simple type:

 public class ValueHolder { private final int value; public ValueHolder(int value) { this.value = value; } public int getValue() { return value; } } 

If you have two lines:

 ValueHolder vh1 = new ValueHolder(1); ValueHolder vh2 = new ValueHolder(1); 

then you create exactly 2 objects on the heap. Despite the fact that they behave in exactly the same way and have the same values โ€‹โ€‹stored in them (and cannot be changed), you will have two objects.

So vh1 == vh2 will return false !

The same is true for String objects: there can be two String objects with the same value.

However , there is one peculiarity in String : if you use <24> (*) , ( , ).

So, in your example, the code str1 and str2 will point to the same object .

(*) or more precisely: a expression of a compile time constant of type String .

+9
source

You write a short version of this

 String str1 = new String("dave"); String str2 = new String("dave"); 

So str1 and str2 are different objects and can be changed separately as such

"dave", the original string, exists only once in memory, with another reference.

0
source

it depends. if you write a small test program, then there is a very good chance that they will contain the same link, because java tries to do you a favor, preserving memory and reusing links. if str2 comes from user input, then it will probably be two different links. A good way to check is to use == in comparison. if they are equal ==, then they refer to the same memory location. if not, then these are two different links. this discards a lot of novice programmers because when they start writing code, they use ==, see that it works, and then along the way they canโ€™t understand why their comparisons do not work.

I cannot explain specifically when java reuses backstage links, but this is due to how and when values โ€‹โ€‹are created

-1
source

Source: https://habr.com/ru/post/1469519/


All Articles