From the course: Nail Your Java Interview

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Understand string equality

Understand string equality - Java Tutorial

From the course: Nail Your Java Interview

Start my 1-month free trial

Understand string equality

- [Illustrator] The roots of string equality lie in how we create an initialized strings. There are two ways we can create strings. We can create string literals or string objects. The difference between these is how they are stored and referenced in our code. Most of the time, you'll likely create a string literal. That's a series of characters with quotes around them. As string literals, both string A and string B refer to the same string in memory. They are both references to the same literal ABC value within the string constant pool. This matters in the case of string equality, which we'll discuss in a minute. The other way to create a string is with the string object constructor. In this case, instead of creating a string literal, we are creating a string object. Object A and object B do not refer to the same string value because they are created as separate objects with the string constructor. Usually, you'll want to…

Contents