• Post Reply Bookmark Topic Watch Topic
  • New Topic

How come this doesnt work?  RSS feed

 
Mario Skrlec
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a big newbie to Java and this might be the most simplest code of all codes but I still need help with it.




This is basic code so, please don't tell me exactly whats wrong, give me a hint so i can figure out myself. Thank you!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mario Digitron wrote:



No, it should not be true. It's working exactly as it should. The == operator used on references merely compares the values of the references for equality--that is, it checks if the two references are pointing to the same object, or are both null.

In order to compare two object's states for equality--including checking if two Strings have the same sequence of characters as you're trying to do here--you need to use the equals() method.
 
Mario Skrlec
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mario Skrlec wrote:I'm a big newbie to Java and this might be the most simplest code of all codes but I still need help with it.




This is basic code so, please don't tell me exactly whats wrong, give me a hint so i can figure out myself. Thank you!


Hi Mario,

Just to add one more point...I think you are reading the input from the console. The '==' operator here can return true too in case you are matching two literally defined String variable references. The reason is the pooling of String objects.

Just analyze these lines:



Thanks
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avi sinha wrote:The '==' operator here can return true too in case you are matching two literally defined String variable references.


While that's true, it's not really relevant to the question, and there's probably no situation in which it's appropriate to test for string1 == string2, unless you're the author of the String class and are writing String's equals() method.
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
While that's true, it's not really relevant to the question...


Are you sure , it is not relevant? so how are you going to describe the immutability and the need of StringBuilder??

and there's probably no situation in which it's appropriate to test for string1 == string2..

agreed.

, unless you're the author of the String class and are writing String's equals() method.

I think 'final' is still there... I won't get a chance.

Thanks & Regards
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avi sinha wrote:
Jeff Verdegan wrote:
While that's true, it's not really relevant to the question...


Are you sure , it is not relevant? so how are you going to describe the immutability and the need of StringBuilder??


Immutability and StringBuffer/StringBuilder have no dependence on the constant pool. More to the point, unless something else has come up, his problem is that he's getting false where he expects true on an equality test and that has only to do with the fact that he's calling == rather than equals(), and nothing to do with the constant pool.

, unless you're the author of the String class and are writing String's equals() method.

I think 'final' is still there... I won't get a chance.


Exactly.
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I just wanted to add a 'can be true' scenario to make it more clear.

Thanks anyways Jeff
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avi sinha wrote:Well, I just wanted to add a 'can be true' scenario to make it more clear.


Fair enough.

I probably would have couched it in a more cautionary sense though, along the lines of "While you may sometimes see == giving true for Strings, that's a side-effect of the constant pool. It sometimes misleads people into thinking you can compare Strings with ==, but just like with any other class, you need to use equals() for comparing Strings' contents."
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
avi sinha wrote:Well, I just wanted to add a 'can be true' scenario to make it more clear.


Fair enough.

I probably would have couched it in a more cautionary sense though, along the lines of "While you may sometimes see == giving true for Strings, that's a side-effect of the constant pool. It sometimes misleads people into thinking you can compare Strings with ==, but just like with any other class, you need to use equals() for comparing Strings' contents."


that's why i had added
analyze these lines:
. I wanted the guy to do some research on it.
 
Mario Skrlec
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got into java from C (nothing serious, went trought The C programming language and all the exercises) and i didnt think of String as an object (although i should) but as a primitive type. Yes, I ran the program from the console. The reason why i thougt that the comparison should work is beacuse of the main program.


and it worked, but i see now why it didnt work. It was obvious to the equals() and I shouldve realized that before my post. Does anyone have any advice on learning java, how set my mind into thinking in java beacuse i still think in C. Thank you in advance.

 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are coming from C background, please try to learn first OOPS concepts and then try to implement those in your programs.
Once you are done with this try to learn what features Java provides and what exactly an API is, so that instead of implementing your logic you reuse the Java API.

I would refer some of the books to you:-

1. Core Java volume 1 ( 8th Edition ) by Cay Horstmann.
2. Thinking in java 3rd edition or 4th edition by Bruce eckel.
3. A Programmer's Guide to Java SCJP Certification: A Comprehensive Primer, third edition by Khalid Mughal
4. Just Java 2, sixth edition by Peter van Der Linden.

For a complete beginner I would recommend
1. Core Java volume 1 ( 8th Edition ) by Cay Horstmann.

Happy Learning
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mario Skrlec wrote: . . . . . .
There was a very useful JavaRanch journal article which explains that problem.
 
Mario Skrlec
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajat Jindal wrote:If you are coming from C background, please try to learn first OOPS concepts and then try to implement those in your programs.
Once you are done with this try to learn what features Java provides and what exactly an API is, so that instead of implementing your logic you reuse the Java API.

I would refer some of the books to you:-

1. Core Java volume 1 ( 8th Edition ) by Cay Horstmann.
2. Thinking in java 3rd edition or 4th edition by Bruce eckel.
3. A Programmer's Guide to Java SCJP Certification: A Comprehensive Primer, third edition by Khalid Mughal
4. Just Java 2, sixth edition by Peter van Der Linden.

For a complete beginner I would recommend
1. Core Java volume 1 ( 8th Edition ) by Cay Horstmann.

Happy Learning


Thanks for the advice. I started with Head First Java but now, Core Java it is...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!