Primitive Vs Reference type
For primitive types; char, byte, short, int, long, float, double and boolean, ALWAYS use “==” (double equal) operator to compare for equality, this does not go against common sense, in fact, this is the probably the default thinking of many programmers — I hope by now you’ve gotten over the “=” as a way to compare values in Java, the single equal sign is not an operator for comparisson, that’s used to do assignment of values.
When you are comparing object types or reference types, it’s not very straightforward, what complicates it a bit is the fact that objects are stored slightly differently than their primitive counterparts.
When you define a variable of primitive type, say
int x = 10;
int y = 10;
if (x == y)
You allocated an area in memory for x, let’s just call that area in memory location-1001; it will be 4 bytes long, and on that same location in memory, the value 10 will be stored.
You also allocated another area in memory for y, let’s call this one location-1002; it will also be 4 bytes long, and on that same memory location, you stored the value 10.
So now, the statement x == y, really means that; go to the location-1001 (x) get it’s value, then go to location-1002, get it’s value, then compare them, were comparing is 10 exactly equal to 10, and the answer would be, yes (or true, the == operator will return a boolean value).
No surprises there, that’s suppose to happen, and it doesn’t go against common sense.
Comparing reference types
//
//
class EqualSample {
public static void main(String args[]) {
String x = "Hello";
String y = "World";
String z = "HelloWorld";
String a = x + y;
System.out.println(a == z);
System.out.println(a.equals(z));
}
}
if you run and compile this code, you will find out that a == z will return false, and a.equals(z) will return true. So that’s happening here?
The line String x = "Hello"; means — let’s define a variable of type java.lang.String somewhere in memory, let’s call it variable x, and let’s put in location-1001; were not quite done yet, we need to create a “new” java.lang.String object somewhere else in memory, put the String “Hello” in it, and put in location-2001. Now that we’ve created the actual string object, let’s tell the variable x where he can actually find the string object, we will put location-2001, inside location-1001; that way, when somebody looks for the content of variable x, they will not find the actual string object, but they will find the location of the actual string object.
The line String y = "World"; means — let’s define a variable of type java.lang.String, let’s put in location-1002, let’s call it x. After that, let’s create a new java.lang.String object somewhere else in memory, put the letters “World” in it, and put it away in location-2002. Let’s store location-2002 inside variable y.
The line String z = "HelloWorld"; means — let’s define a variable of type java.lang.String, let’s put in location-1003, call it z, then create a new java.lang.String object in location-2003, and put the letters “HelloWorld” in it. Let’s store location-2003 inside variable z.
The line String a = x + y; means — Let’s define variable “a” of type java.lang.String, let’s put in location-1004; Now, let’s look at what’s inside variable x, okay, it’s telling to us get the contents of location-2001, hold that for a while, then let’s look variable y, it’s telling us to look at location-2002 then get its content. The contents of location-2001 and location-2002 are “Hello” and “World” respectively, let’s add these strings together, and then the resulting string “HelloWorld”, lets store somewhere else, maybe in location-2009. Now that it’s all sorted up and added up, let’s tell variable a (in location-1004) that his contents are in location-2009.
You may need to read the last few paragraphs maybe 3x, read it slowly, and get pen + paper, do a mental image of that description. When you’re done, read it over again.
A bit dizzy? don’t worry, it’s almost over.
So the statement a == z what that means is is location-1001(which really contains location-2001) equal to to location-1002(which is really location-2002) of course not!
And the statement a.equals(z) really means – get location-1001, what’s inside? well, location-2001, get that! what’s inside? it’s “Hello”. Now get location-1004, find out what’s inside, if you find another address or pointer to a location, then go there, until you find what’s really inside. In the end, the statement would read "HelloWorld".equals("HelloWorld") Yes!
Lessons learned
When you define primitive variables, like this int i = 1;, the value 1, is stored immediately in the memory location marked by i, it doesn’t contain another address, it contains the value already.
When you are defining reference types, like this String x = "Hello"; The value “Hello” isn’t stored in the memory location marked by x, but is stored somewhere else, and what is stored inside the memory location marked by x? Well, the address of “Hello” is stored in the memory location marked by x, not “Hello” itself.
To C programmers, this will sound a lot like pointers, but trust me, they’re not. They just look a lot, and behave a lot like pointers, but there are significant differences.
{3 Comments below .. you can add one }
7.1.2009at 00:49
9.23.2009at 11:07
Brilliant for understanding primitives and reference types!
Clearly has a detailed understanding of the topic!
4.3.2010at 17:19
Sorry, I think these lines below:
“So the statement a == z what that means is is location-1001(which really contains location-2001) equal to to location-1002(which is really location-2002) of course not!”
should be changed to:
“So the statement a == z what that means is is location-1004(which really contains location-2009) equal to to location-1003(which is really location-2003) of course not!”
Because I think need compare location of a and z
Good site to learn.