CodeGym /Courses /Java Syntax Zero /Comparing references

Comparing references

Java Syntax Zero
Level 3 , Lesson 8
Available

1. Comparisons

Programmers need to compare different variables with one another all the time. But, as you have already seen, everything is not so simple.

Integers are very easy to compare — you just use == and you're done. To compare real numbers, you have to compare their difference (or rather, the absolute value of the difference) with some very small number.

Comparing strings is even more difficult. Above all, this is because strings are objects. What's more, programmers often want the string comparison to go a little differently depending on the situation.


2. How strings are arranged memory

As you have already seen, strings are stored in memory differently than integers and real numbers:

How strings are arranged memory

Two blocks of memory are used to store strings: one block stores the text itself (its size depends on the size of the text) while the second block (4 bytes) stores the address of the first block.

Although an experienced programmer would say something like "the String str variable stores a reference to a String object.


3. Assigning references to a string

The benefits of this approach become apparent when you need to assign one string variable to another string variable. Example:

String text = "This is a very important message";
String message = text;

And here's what memory will contain as a result:

Assigning references to a string

After this type of assignment operation, the String object remains where it was, and just its address (a reference to the object) is copied into the message variable.


4. Working with references and objects

But if you decide to convert a string to uppercase (capital letters), the Java machine does everything right: you will end up with two String objects, and the text and message variables will store references, each to its own object.

Example:

String text = "This is a very important message";
String message = text.toUpperCase(); 

And here's what memory will contain as a result:

Working with references and objects

Please note that the toUpperCase() method does not change the string it is called on. Instead, it creates a new string (new object) and returns a reference to it.

How about an even more interesting example. Let's say you decide to pass a string to a Scanner object (so that it reads values from the string).

Example:

String text = "10 20 40 80";
Scanner console = new Scanner(text);
int a = console.nextInt();
int b = console.nextInt();

You can learn more about the how the Scanner class works here.

This is how it will all be stored in memory:

Working with references and objects. Scanner class

In this case, a single String object remains in memory just as it was — only references to it are passed around and stored in variables.


5. Comparing references to String objects

And finally, we've reached the fun part: string comparison.

There are two operators you can use to compare string variables: == (equal) and != (not equal). You can't use the "greater than", "less than", or "greater than or equal to" operators — the compiler won't allow it.

But there's an interesting nuance here: what actually gets stored in string variables? That's right: addresses (references) to objects. And it is these addresses that will be compared:

String text = "Hi";
String message = text;
String s1 = text.toUpperCase();
String s2 = text.toUpperCase(); 

Here's what will be in memory:

Comparing references to String objects

The message and text variables refer to (store the address of) the same object. But the s1 and s2 variables store references to objects that are very similar but distinct.

And if you compare these 4 variables in the code, then you get the following result:

Code Console output
String text = "Hi";
String message = text;
String s1 = text.toUpperCase();
String s2 = text.toUpperCase();
System.out.println(text == message);
System.out.println(text == s1);
System.out.println(s1 == s2); 




true  // The addresses are equal
false // The addresses are different
false // The addresses are different

3
Task
New Java Syntax, level 3, lesson 8
Locked
Minimum of two numbers
Minimum of two numbers
3
Task
New Java Syntax, level 3, lesson 8
Locked
18+
18+
3
Task
New Java Syntax, level 3, lesson 8
Locked
Bouncer policy
Bouncer policy
3
Task
New Java Syntax, level 3, lesson 8
Locked
Quadrants
Quadrants
Comments (10)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
SinACosB Level 4, Delhi, India
21 February 2024
photos are too small to see
Aarush Bagchi Level 7, United States of America, United States
7 July 2023
bruh
Jennifer Olland Level 4, United States of America, United States
23 October 2023
yea my brain just smoothed over on that one too lol
Spooky 999 Level 5, Zolwer, Luxembourg
30 June 2023
Can someone explain where we can use the variable adress comparasion? Thanks!
Abhishek Tripathi Level 72, Rewa, India Expert
22 December 2023
yes when you are comparing two objects by using the equals() method of Object class. You start the comparison by just comparing the references of the object if it is equal then there is no point to compare the data of the objects just simply return true. In that manner this reference comparison (==) saves the time.
Sergey K Level 12, United States of America, United States
2 January 2023
10 years of studying will not summarize "whole Java".
Kinshuk Level 4, Vapi, India
1 January 2023
Can anyone summarize the whole Java.
Z Level 4
7 October 2022
can anyone write the keypoints in short?
Cuvar Sjena Level 5, Sarajevo, Bosnia and Herzegovina
22 October 2022
Figure 1. The string occupies 2 blocks. 1st block (4byte) in which the text is stored, and 2nd block the address of the first block (its size depends on the amount of text). Figure 2. Each string has its own address, but they can use the same text (text in memory) because they can use the same path to the text. Figure 3. The case from picture 3 is different from picture 2 because if we ask for a string to be called, but to change some of its text, even if it was the size of the letters, then a new string and a new address are formed. Figure 4. A string forms its own code address and its own text, The scanner creates its own position in memory called Scanner and in it there is a scanned I3 string (string data) that uses the original string text. Which means that the Scanner does NOT have its own text, but uses String's text in memory. As for int, they are special and are not related to either Scanner or string, which is also logical. Figure 5. String G3 text and G3 Message have 2 different addresses because they are two different strings, however their content or text is the same and that is why they use a common address. While G11 and G15 are 2 different strings and that's why they have different addresses, but they also have different texts in memory for the reason that they don't have their own text but refer to another string AND ASK that string to change in the form of changing the size of the letters in the text . // Sorry, my English is not very good, but I hope I helped you
Abe Level 13, los angeles, United States
6 November 2022
Thank you! Very helpful and your English is perfectly file.