Question 4: "How do I compare strings in Java?"

Java - Top 10 Most Viewed Questions on Stack Overflow Question 4: "How do I compare strings in Java?"
19 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.99
List Price:  €92.85
You save:  €27.85
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$105.96
List Price:  A$151.38
You save:  A$45.42
S$94.48
List Price:  S$134.98
You save:  S$40.50
HK$546.78
List Price:  HK$781.15
You save:  HK$234.36
CHF 63.33
List Price:  CHF 90.48
You save:  CHF 27.14
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.44
List Price:  NZ$166.35
You save:  NZ$49.91
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$355.02
List Price:  B$507.19
You save:  B$152.17
R1,295.44
List Price:  R1,850.71
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪260.15
List Price:  ₪371.66
You save:  ₪111.51
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,713.01
List Price:  ¥15,304.96
You save:  ¥4,591.95
MX$1,187.73
List Price:  MX$1,696.83
You save:  MX$509.10
QR254.83
List Price:  QR364.06
You save:  QR109.23
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.65
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,773.40
List Price:  ISK kr13,962.60
You save:  ISK kr4,189.20
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.26
List Price:  ден5,712.05
You save:  ден1,713.78
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,334.28
List Price:  Ft36,193.38
You save:  Ft10,859.10
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
Already have an account? Log In

Transcript

The fourth most viewed question on StackOverflow, with over 3.7 million views over the years is how do I compare strings in Java? So yeah, the comparison between strings is special. But in general, it is interesting. How can I compare two objects with each other? and Java knows two different comparisons, two different kinds of comparisons comparisons. And so yeah, you have two different approaches to compare two strings, or in general, two objects with each other.

And we want to take a look at both. So the first approach is the equality operator. It is written with two equal signs and The equality operator compares two references with each other. We take a look what what is the house Exactly. And the second one is the equals. In a call of the equals method of an object, so the equals method is part of the class object.

And in Java every every kind of class is extending the object class. So every every object every kind of, of data type in Java has the equals method because equals method is part of the object class. Okay? And with the equals method, you check the content of two objects, so you compare the content, not the references of it. So let's have a look how Java works. As I said, with with the equality operator, you can compare two reference with each other if they are pointing on the same object on the heap.

So let's take a look what this means. I have created an example. In line three, we are creating an object with a string with the content hello world, and an reference string as one. So as one is pointing to an object with the content hello world. And if you create an object in Java, every created object is stored in the heap of your computer from your working storage. Yes.

All right. So yeah, just call credit. An object in the heap I call it string one, it's a portal name with the content hello world and as one is a reference to this object, so s one is pointing to this object in line four, I create another reference called string as to and say as two equals as one. And what it does is that as as two is pointing to the same object as as one, okay, so, as you can see in Java, on the left side, there are only references, which are pointing to objects or to other yeah to two objects. Tonight, we will see it later. And the objects are in the heap now In this case, after computing line four, it looks like that, that s one and s two is pointing on the same object.

So it's the theft the reference is the same. So if you compare the references with the equal operator, it says true. So as one equal operator as two is true, okay, because as one and as two as pointing on the same to the same object, so the reference is the same. So, if we continue with our code, and we write as two equals new string, hello world, what what happens in the heap is it creates another a second Object Soto called string two with the same content, but the object is another one. And so, we have now we have two different objects string one and object string two, but they have the same the same content, but are different objects. And with this call in line to the reference s two isn't pointing to object one anymore, but now it is pointing to object to.

So it looks like this. And now, if I am checking with the equal equality operator, if s one and s two is the same reference, it says false. Yeah. Okay. It makes sense because they are not pointing on the same object. anymore.

And it is the content of the object is Yeah, doesn't matter here, it is all only looking at the references. If you want to compare the content of the objects, you have to use the equals method. So that's the second approach to compare strings in Java or objects. Now, as I said, there are two different kinds of comparison and now we are looking at the second approach. The second approach takes a look at the content of two objects. And in this case, if we are looking at our example from from that we just saw, so we create as one and as two points on the same object like as one The content is the same for sure.

As one equals as two is true, clear. And if I create, then another string object with also HelloWorld is content and I create, or I asked is as one equals as two. And it says also true, because the content is the same. Yeah, let's have a look at what what is happening here. So we in our case, we are using the string class. Go to navigate, open type.

Then we type string, Java string. And there must be an equals method. You can go Quick outline. And then you can type equals and then you see, okay, this is the implementation of the equals method from class string. Let's have a quick look at object we go to navigate open type object. And as you can see, object s equals two.

Yeah, and that's the default implementation equals. So, it compares the instance where the equals method is called. And then it compares is this instance, the same reference as the given option. Correct. So it both references are pointing on the same object if yes, so equals is also true. So the content is the same.

Clear. And, yeah, this kind of this method is an overriding this equals method from object. And it's come it's a bit confusing, but it compares the value of the string. And value. You can see it here. value as the value is used for character storage.

So in this variable the text value of the string is stored. And in the equals method, the value of the instance and the value of an object. In this case, an object is a string. So a string is cast. An object is cast. to string on and then the value from this one and this is compared with each other.

So, yeah, you will see the equals method is looking at the content of objects. If you are writing an own own data type you can you can override the equals method and you can say, or you can define when two to two different objects are the same content regarding the content by overriding the equals method, okay. So, beware of my pointer exceptions. You if you are having data types, like strings or any kind of objects that are not primitives, You can initialize them by with with Niall. And if you are asking or checking the references is a three equals as for or the equal for equality operator. It's a it's true.

Yeah, for sure imports are not references. But if you are looking at F three dot equals as four, it gets my pointer exception. So why at three is nine and you you aren't allowed to invoke methods on on NIH. If you try to you get an NIH point exception. There is later on I think the question number nine This is handling with nine point exceptions and how to avoid and how to fix it. So let's have a look later on this issue.

But for for now. You have to be aware you can't always call the equals method on each object because the object may be nice. So, yeah, be aware of the nine point exception. In this case. Since Java version seven there is a class called objects. And this class has a equals method on its own, and it's an i pointer exception safe I call it y.

If it checks if A is now so let's have a look how it works. You ask the class is object a and object B are the reference from from type object a and reference of type object called B. Is it the same, or equals is the essence content. So it says, Okay, if A is pointing to the same object as B, it is true for sure. If it's not the same reference, then this is one wall, then it evaluates this expression. And first it take a look if A is is not equal to NY.

And if this is the case, then a dot equals b is called is a null. And then this one isn't called anymore. Yeah, to get rid of the nine point exception, and, yeah, so you'll see if you want to use the equals method, and you don't want to get an Aponte exception Or you, you don't want to, to have the the dangerous of an eye upon exception, use the objects fast objects dot equals and if both objects in it, and it is now a pointer exception safe. As you can see, comparing strings with each other is a bit special and a bit confusing at the beginning. And that's why I think the question is how do I compare strings in Java with each other and not how do I compare objects with each other because in in the case of strings, there is a bit confusion because of the string pool is a so called string pool.

What is the string pool? You can initialize strings in two different ways. Like you can write it like in a line to for example, without using an constructor, and you can create and string like in line three with a constructor. If you are using the expression without constructor, you get an string with the same with the same content as in the in the seven. But if you are using this one, they are the string object you get. The reference is pointing to an string object that isn't in the heap, but it is in the string so called string foo.

So every time you create while you are using suction string without using the constructor, Java is creating an object of class string And lays it in the string pool for reusability. So, if you are using later on the same string without calling the constructor, Java looks in the string pool and sees, okay, there's already a string with this content. So, I could reuse it and I give it back to the same reference. So, if you are writing s five and six, they are they are pointing to the same object worth in the string quiz. So yeah, if you if you if you think you are using for example 100 times hello world, why whatever, but you are using it. And if they are wearing a string pool, Java had Chava had to To create 100 times an object of types string with the same content.

And with the string pool, it is it has only created one object and gave away 100 reference to the same object. So that's why the string pool or that's the background of the string pool. So yeah, by calling as five and six, both the same way, if we are we if we compare those, the reference, we get true as reside. So they are pointing to the same object within the string pool. But you can explicitly create a new object on the heap and you can say I don't want to use the object of the string pool. Want my own object.

And if you do so, or if you want to do so, you should use the constructor of class string. And if you do, if you write new string hello world, Java don't use doesn't use the string pool. It's used the constructor and creates a new string object on the heap with the same content like the object in the string pool. But that's the reason why, if you compare the reference between as six and seven, it says false. So that's the special special thing regarding the strings. So if you don't know the string pool, and then you you would think, okay, I create an object here create an object There are so the two different objects.

Why is says reference true Why? Why the reference the same? The answer is because Java is using a string pool. And now you are aware of this kind of comparison and you understand why this is the same. That's the special thing about the string pool. All right, that's so far the comparison between two strings in Java or in general is a comparison between two objects in Java.

Let's have a look at the next question.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.