Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 String problem... again..
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MysticVegeta




PostPosted: Fri Dec 16, 2005 12:11 pm   Post subject: String problem... again..

This is really weird. I dont know whats going on! I go soemthing like this
code:
String a = "abcde";
String ab = a.substring(1,2);
System.out.println(ab);
if (ab == "a") {
System.out.println (a);
//doesnt go here!!!
}


Note: Pseudocode, anyways, what is the problem, ab = "a" but "a" not= ab? what the hell?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Dec 16, 2005 12:41 pm   Post subject: (No subject)

common mistake in Java.

ab is an object of class String, while "a" is a string native value.

== checks for equality, but Object instance is not the same as an instance of a value. You have to compare their content explicitly.

Something like ab.equals("a")

It's been a while since I've done Java
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
wtd




PostPosted: Fri Dec 16, 2005 2:50 pm   Post subject: (No subject)

Tony wrote:
common mistake in Java.

ab is an object of class String, while "a" is a string native value.


Wrong.

Strings are always objects. The thing is, "==" on objects checks to see if they're exactly the same object, not that their contents are identical.

As mentioned, use the "equals" method.
Tony




PostPosted: Fri Dec 16, 2005 3:34 pm   Post subject: (No subject)

Smile
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
MysticVegeta




PostPosted: Fri Dec 16, 2005 4:29 pm   Post subject: (No subject)

omg you are joking!! I thought .equals and "==" meant the same! Why do they have to make it so confusing?!
wtd




PostPosted: Fri Dec 16, 2005 6:27 pm   Post subject: (No subject)

MysticVegeta wrote:
omg you are joking!! I thought .equals and "==" meant the same! Why do they have to make it so confusing?!


Because there's no operator overloading.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: