Nexie
Junior Newbie
|
 |
«
Posted
2011-04-07 09:51:28 » |
|
Hiya - I've been lurking the forums for a while looking for good re-introductions to Java programming (I've done a bit of C# about a year ago). Anyway - I've been following a series of videos on TheNewBoston on Java, and I've hit a bit of a dead end. I am trying to compare the contents of a text String to a String value in an If statement, but I can't seem to get it to acknowledge that the two are the same - Could you try and explain what I am doing wrong? The video only deals with Int comparing which is pretty simple to understand, compared to my own little attempt. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import java.util.Scanner;
class inputtest { public static void main(String arg[]) { Scanner userinput = new Scanner(System.in); String ishumantest; System.out.println("Are you a human? If so type in yes"); ishumantest = userinput.nextLine(); if (ishumantest == "yes") { System.out.println("You are welcome to enter."); } else { System.out.println("Non-Humans not allowed."); } } } |
|
|
|
|
Riven
|
 |
«
Reply #1 - Posted
2011-04-07 10:10:12 » |
|
== checks for identical references, not for object equality 1 2
| boolean eq = new String("abc") == "abc"; boolean eq = new String("abc").equals("abc"); |
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
Nexie
Junior Newbie
|
 |
«
Reply #2 - Posted
2011-04-07 10:16:29 » |
|
Oh I see - he failed to mention that. Your suggestion works perfectly. 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
ReBirth
|
 |
«
Reply #3 - Posted
2011-04-07 11:16:51 » |
|
Because String is kinda special. new String("abc") is not same with constant "abc" itself. To take it rude, "there two of them on memory".
That's what I remember from reading about how bad String is. That's why some are using StringBuilder.
|
|
|
|
cylab
|
 |
«
Reply #4 - Posted
2011-04-07 13:45:36 » |
|
That's why some are using StringBuilder.
Not exactly. StringBuilder provides means to concatenate Strings without creating new Strings over and over, since Strings are immutable. Has nothing todo with comparison. As a rule of thumb: use .equals() on all types starting with a capital letter.
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
ToXSiK
Senior Newbie 
|
 |
«
Reply #5 - Posted
2011-05-11 23:08:08 » |
|
Like they said above. I just wanted to throw in there that Object.equals(otherObj) is used when comparing OBJECTS. == is used ONLY when comparing primitive data types (float, int, byte, etc.). Every time you use a java object class, you will have the .equals method available (Since they all implement Comparable - Look into doing this if you want to make you're own .equals method). ToXSiK 
|
|
|
|
counterp
|
 |
«
Reply #6 - Posted
2011-05-11 23:40:02 » |
|
Like they said above. I just wanted to throw in there that Object.equals(otherObj) is used when comparing OBJECTS. == is used ONLY when comparing primitive data types (float, int, byte, etc.). Every time you use a java object class, you will have the .equals method available (Since they all implement Comparable - Look into doing this if you want to make you're own .equals method). ToXSiK  Not true, sometimes you have to check if an object is null (and calling equals() on a null object will throw an exception), or you want to see if two objects are just the same references.
|
|
|
|
ra4king
|
 |
«
Reply #7 - Posted
2011-05-12 01:28:40 » |
|
Like they said above. I just wanted to throw in there that Object.equals(otherObj) is used when comparing OBJECTS. == is used ONLY when comparing primitive data types (float, int, byte, etc.). Every time you use a java object class, you will have the .equals method available (Since they all implement Comparable - Look into doing this if you want to make you're own .equals method). ToXSiK  Also wrong in the fact that .equals() is not in Comparable, but in Object, which is the superclass of ALL classes 
|
|
|
|
pjt33
|
 |
«
Reply #8 - Posted
2011-05-12 07:14:43 » |
|
Could you try and explain what I am doing wrong? You're writing C#  This is one of the things that trips a lot of new Java programmers up, so the designers of C# decided to put in a special case for strings. So people who move from C# to Java are now even more likely to trip up than people who start Java with no prior knowledge of any language.
|
|
|
|
ToXSiK
Senior Newbie 
|
 |
«
Reply #9 - Posted
2011-05-13 18:18:32 » |
|
Sorry, I meant in object. We all make typos  Not true, sometimes you have to check if an object is null (and calling equals() on a null object will throw an exception), or you want to see if two objects are just the same references. But by calling .equals(), whether you are checking if it is null or not or if it is just a reference, you still must use .equals for situations dealing with objects... All I am saying.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
CyanPrime
|
 |
«
Reply #10 - Posted
2011-05-13 18:20:43 » |
|
I use String.compare().
|
|
|
|
ra4king
|
 |
«
Reply #11 - Posted
2011-05-13 21:32:21 » |
|
compareTo() is only useful for comparing two Strings lexicographically (by letter value). equals() is best for comparing equality.
|
|
|
|
Eli Delventhal
|
 |
«
Reply #12 - Posted
2011-05-13 22:06:44 » |
|
I feel like nobody has said what's really important to learn here.
The reason why you can't use == for a String in Java is because a String is an Object, not a primitive. If you call == on an object, you are just comparing the memory addresses of pointers. So, if both pointers are at the same memory address, then it's true, otherwise it's false. When calling the equals() method, you are actually comparing the objects that lie at the memory you are pointing to, using a method that each object has its own implementation of. A string for example compares every single letter for equality (or maybe it does a hash of some kind, I'm not sure).
Primitives are: int boolean float double byte char
Everything else is an object. Just think of == as "the exact same object" and .equals() as "have the same value." Also, you don't need to use == to check for null (even though I would do it that way), because myObj.equals(null) will also be false.
|
|
|
|
pjt33
|
 |
«
Reply #13 - Posted
2011-05-13 22:14:40 » |
|
int boolean float double byte char short long
|
|
|
|
ra4king
|
 |
«
Reply #14 - Posted
2011-05-13 22:15:21 » |
|
Forgot "short" and "long" 
|
|
|
|
ReBirth
|
 |
«
Reply #15 - Posted
2011-05-14 03:01:51 » |
|
Forgot "short" and "long"  you can forget short but not long, it's important for updating delta time on game loop 
|
|
|
|
counterp
|
 |
«
Reply #16 - Posted
2011-05-14 03:19:24 » |
|
Sorry, I meant in object. We all make typos  Not true, sometimes you have to check if an object is null (and calling equals() on a null object will throw an exception), or you want to see if two objects are just the same references. But by calling .equals(), whether you are checking if it is null or not or if it is just a reference, you still must use .equals for situations dealing with objects... All I am saying. That's not the problem, the problem is that you said: == is used ONLY when comparing primitive data types which is not true.
|
|
|
|
Eli Delventhal
|
 |
«
Reply #17 - Posted
2011-05-16 17:13:18 » |
|
int boolean float double byte char short long  Thanks, I knew I was forgetting a couple.
|
|
|
|
|