• Post Reply Bookmark Topic Watch Topic
  • New Topic

testing String in a condition  RSS feed

 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I just started to learn java and I'm trying to make somewhat of a bank application for learning purpose.

Here is the code, I don't want any correction of it I just have some question about it:



It doesn't work so my question is :
Can I test a String like I did ? If I can how should I do it ? Edit : I just found out "==" test if the string is the same object and I should use equals(). So equals test if the String are the same values and "==" test if it is the same object. I don't really understand that last sentence "test to see if it's the same object". As far as I understand the only same object a string could have is itself ? Am i wrong ?
Would it be better to have the String being casted into an int ? I think it will be demanding more work if I do so because then I will have to test the string to see if it can actually be cast into an int. It's no big deal now but i would like to optimize my coding the best I can now that my coding is simple. It will have good echo when i'll code more heavy stuff in the future I think.


my edit brought me some questioning. I'll try to be as clear as possible trying to formulate what is my question since it is really confuse in my head right now:

A program is made of objects. A string is an object itself but is higher in the hierarchy of objects and is used by all the programs we want to create (at least if we appeal a String at some point). Why aren't the type int, long, double, etc not objects then ?

thanks in advance.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cedric,
Your doubt is correct. You need to do reponse.equals("1") or "1".equals(reponse) instead of reponse == "1".

Whether or not "1" is the same String object depends on how it is created so you don't want to be relying on it. For example consider,


s1 == s2 returns false but s1.equals(s2) returns true.



here both s1 == s2 and s1.equals(s2) return true.

As you can see, you don't want your code to be brittle and rely on how the String was created.

You may see "1".equals(reponse) in "real" code. It looks backwards as a trick to avoid having to worry about whether reponse is null.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two objects can be logically equivalent, even if they are not the same object. So, two $100 dollar bills are equal but different objects. However, a husband and wife can both refer to their joint checking account - it is the SAME account, regardless of how you get their (through the husband or the wife).

Strings do have some funny rules about how they get created, but don't worry about that. Just remember that for any kind of object (including Strings), .equals is almost ALWAYS the right answer (like, 99.9999999999% of the time).
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Cedric,

As you can see, you don't want your code to be brittle and rely on how the String was created.


Thanks, I understand the "how" (I need to use equal() for Strings) but I'm not sure I do get the "why". Two objects are equal if they are created the same way and have the same value ?

fred rosenberger wrote:Two objects can be logically equivalent, even if they are not the same object. So, two $100 dollar bills are equal but different objects. However, a husband and wife can both refer to their joint checking account - it is the SAME account, regardless of how you get their (through the husband or the wife).

Strings do have some funny rules about how they get created, but don't worry about that. Just remember that for any kind of object (including Strings), .equals is almost ALWAYS the right answer (like, 99.9999999999% of the time).


but in the example of boyarsky above you that I'll recall :

Jeanne Boyarsky wrote:


here both s1 == s2 and s1.equals(s2) return true.
Here we have two different accounts but they are equal.

I'm being stubborn but it's just that I want to understand clearly.


 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have had to change some of the long lines in your post, and you can see how it should be done.
You cannot cast a String to an int.
A long time ago, Corey McGlone wrote an article about Strings, which I think will answer your questions. Try here.
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

I have had to change some of the long lines in your post, and you can see how it should be done.
You cannot cast a String to an int.
A long time ago, Corey McGlone wrote an article about Strings, which I think will answer your questions. Try here.


Dude thanks a lot! awesome article. I'm just at the beggining and it's getting late here so I'll read it completly tomorrow but it seems really great.

So as far as I understood:
Jeanne Boyarsky wrote:
are actually the same objects. The value "1" is the actual object and the names account1 and account2 are the references to that object. I now understand what the second guy (fred) tried to say with his analogy.

Anyway I'm really happy you shared that with me. Also I didn't know there were java certifications exams, i'll look into that aswell.

Thanks buddy.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cedric Bosch wrote:Thanks, I understand the "how" (I need to use equal() for Strings) but I'm not sure I do get the "why". Two objects are equal if they are created the same way and have the same value ?

First, you have to understand that variables in Java are not objects themselves, they are references to objects (just like pointers in C - the variable contains the address of an object in memory, not the object itself). You can have two variables that refer to the same object.

The == operator* compares the references, not the objects themselves. The result of == will only be true if the two expressions on the left and right hand side refer to the exact same object. It will be false otherwise, even if the objects contain the same value.

With strings it's made more complicated because of an optimization that Java uses. If you use the same string literal more than once, then Java will create only one String object, and re-use that everytime you use the same string literal.

* when used on non-primitive types
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks !

Yup so my thoughts are well founded. Strings use different reference to "go" to the same object if the string is the same since strings cannot be changed. On the other hand types like the integer type refer to different objects since the variables integer can be changed. So two integer with the same value will be two reference referring to two different object while two strings with the same value will be two reference going to one object which is the value.

That's how I see it at the moment and at least now it make sens :p
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!