Thread: JAVA 101 help
View Single Post
  #6   Spotlight this post!  
Unread 18-11-2016, 13:11
Nirnaeth Nirnaeth is offline
Coach/Mentor
AKA: Michael Liang
FRC #6367 (The Brooke High ElectroLights)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2001
Location: Boston, MA
Posts: 44
Nirnaeth has much to be proud ofNirnaeth has much to be proud ofNirnaeth has much to be proud ofNirnaeth has much to be proud ofNirnaeth has much to be proud ofNirnaeth has much to be proud ofNirnaeth has much to be proud ofNirnaeth has much to be proud of
Re: JAVA 101 help

Quote:
Originally Posted by Ethan_P View Post
You have (door == "t"), this is not recommended.

Strings are objects, unlike ints and other primitive types. And because Strings are objects, you can't use '==' to compare two Strings. You would need to use
string1.equals(string2);
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.
__________________
Lead Mentor - Team 6367 - The Brooke High ElectroLights
Former Mentor - Team 5969 - The English Skunkworks
Former Mentor - Team 1754 - The Excel Nitro Knights
Reply With Quote