Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Interesting Question

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Byte b1 = new Byte("127");
if(b1.toString() == b1.toString())
System.out.println("True");
else
System.out.println("False");
WHy does this print FALSE??
Please answer!!!
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Expression b1.toString() == b1.toString() is false because you are comparing object references. If you want to compare contents of the object then you should use equals() operator. Expression b1.toString().equals(b1.toString()) will return true.
hope it helps.
cheers
Gaurav Mantro
------------------
http://www.mantrotech.com
 
Sagar Bilgi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Gaurav Mantro:
Expression [b]b1.toString() == b1.toString() is false because you are comparing object references. If you want to compare contents of the object then you should use equals() operator. Expression b1.toString().equals(b1.toString()) will return true.
hope it helps.
cheers
Gaurav Mantro
[/B]


Yeah gaurav, I know that but arent we comparing the same object?? ie b1
..
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sagar Bilgi:
Yeah gaurav, I know that but arent we comparing the same object?? ie b1 ..


No, you are not comparing the same object.
The toString() method is overriden in Byte class to return a new String.
So the if clause

if(b1.toString() == b1.toString())


creates two differentobjects because toString() is called twice.
Hence == will return False.
The contents of the objects are the same though.
Hence equals() will return true.
regards,
Jyotsna
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't post the same question in multiple forums - it wastes our time to reply to questions when there's already a perfectly good answer in another forum. I'm closing this thread - see duplicate in JiG Beginner.
 
expectation is the root of all heartache - shakespeare. tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic