Visualization 01pi.com
Strings: Very useful objects
Code
...
String strA;
strA = "A";
boolean b1 = strA == "A";
boolean b2 = strA.equals("A");
String strB = "B";
String strC = "AB";
String strD;
strD = strA + strB;
boolean b3 = strC == strD;
boolean b4 = strC.equals(strD);
...
Board
Trace
Statements | "A" "B" "AB" strA strB strC strD b1 b2 b3 b4
-------------------------------- | --- --- ---- ---- ---- ---- ---- ------ ------ ------ ------
Description
We will be investigating String
objects.
String
is frequently used
b2
.To start with, String
is object.
String
is immutable
Because of immutability, your code creates and destroys many String
objects during its execution
System.out.println("AB".length());
Is this an error?
We will be investigating String
objects.
String
is frequently used
b2
.To start with, String
is object.
String
is immutable
Because of immutability, your code creates and destroys many String
objects during its execution
System.out.println("AB".length());
Is this an error?
Any String
appears in the form of "xx"
in the code is called Literal String.
String
s
"A"
, "B"
, and "AB"
.This is how I would like to think of it
Suppose there is a hypothetical statement
String "A" = "A";
So there should be
"A"
"A"
Suppose, for each literal string, such a statement is inserted at the top of my code.
If you do not like this idea, it is fine.
String
object is already placed in memory andstrA = "A";
statement does in the next step.String strA;
Creates variable strA
of type String
.
strA = "A";
This is the statement I was talking about.
"A"
object is already in memory location 0.strA
variables refers to that object.Let's check if you really understand this. Suppose the following statements are added at the end of the code.
String strE = "A";
String strF = "A";
"A"
object at location 0 for strA
."A"
for strE
?strF
?boolean b1 = strA == "A";
Let's check if strA
is equal to "A"
by ==
operator.
What should be the result?
boolean b2 = strA.equals("A");
This time we use equals
to check if strA
is equal to "A"
.
What should be the result?
That was easy. Wait for the next one.
String strB = "B";
does both declaration and initialization in one step.
String strB;
strB = "B";
String strC = "AB";
One more declaration and initialization in one step.
Let's make a table of which refers to where and to which content
variable | refers to | content |
---|---|---|
strA |
0 | "A" |
strB |
1 | "A" |
strC |
2 | "A" |
We are one step ahead of the tricky part:
String strD;
defines a new variable strD
of type String
.
Now we are at the tricky part:
+
operator concatenates two String
s."A" + "B"
results "AB"
. We do that.strD = strA + strB;
strA
is "A"
.strB
is "B"
.strD
is "AB"
."AB"
is the one at location 2
Finally, variable strD
refers to the corresponding object at location 4.
boolean b3 = strC == strD;
tests if strC
and strD
are the same.
==
operation checks equivalence in terms of location.
==
is true
if two variables refers to the same object. That is, both refers to the same object at the same memory location.strC
refers to the object at location 3.strD
refers to the object at location 7.strC == strD
returns false
.Suppose you have the following statement. What should be the value of a
s?
boolean a1 = "A" == "A";
boolean a2 = "xxx" == "xxx";
boolean a3 = ("xxx" == "xxx");
boolean a4 = ("xxx" == "x" + "xx");
boolean b4 = strC.equals(strD);
equals
method.equals
is inherited from Object
class.
Object
should overwrite it according to its purposes.String
, being a subclass of Object
redefines 'equivalence of two strings as having the same content.The contents of both strC
and strD
are the same although they are located in different locations. So strC.equals(strD)
should return true
.
That is it.
...
String strA;
strA = "A";
boolean b1 = strA == "A";
boolean b2 = strA.equals("A");
String strB = "B";
String strC = "AB";
String strD;
strD = strA + strB;
boolean b3 = strC == strD;
boolean b4 = strC.equals(strD);
...
. 1 2 3 4 5 6 7 8 9 9 10 11 12
Statements | "A" "B" "AB" strA strB strC strD b1 b2 b3 b4 -------------------------------- | --- --- ---- ---- ---- ---- ---- ------ ------ ------ ------ String strA; | A B AB u u u u u u u u strA = "A"; | A B AB A u u u u u u u boolean b1 = strA == "A"; | A B AB A u u u true u u u boolean b2 = strA.equals("A"); | A B AB A u u u true true u u String strB = "B"; | A B AB A B u u true true u u String strC = "AB"; | A B AB A B AB u true true u u String strD; | A B AB A B AB u true true u u strD = strA + strB; | A B AB A B AB AB true true u u boolean b3 = strC == strD; | A B AB A B AB AB true true false u boolean b4 = strC.equals(strD); | A B AB A B AB AB true true false true