Thread: JAVA 101 help
View Single Post
  #8   Spotlight this post!  
Unread 26-11-2016, 23:31
tcjinaz tcjinaz is offline
Tim
FRC #3853
Team Role: Mentor
 
Join Date: May 2011
Rookie Year: 2011
Location: Arizona
Posts: 205
tcjinaz has a spectacular aura abouttcjinaz has a spectacular aura about
Re: JAVA 101 help

Quote:
Originally Posted by Nirnaeth View Post
It might be useful to talk about the reason for this.

Let's say I have two Strings:

String a = "blah";
String b = "blah";

When I instantiate these Strings, Java allocates two different memory locations for them. And 'a' points to the first one that has the "blah" data in it, and 'b" points to another location with the "blah" data in it.

That's why a==b returns false, because 'a' and 'b' are pointing to two different objects, even if they have the same value.

The '==' operator in this case checks to see if they are the same object, not if the have 'equal' values.
That's barbaric. At least C++ allows overloading operators.
All my fears of Java are coming true.

Tim
__________________
Software Mentor
3853 Pridetronics[

Reply With Quote