Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Another question from Majji's Exam

 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is the answer false here..Can somebody explain.
The following code will give
1: Byte b1 = new Byte("127");
2:
3: if(b1.toString() == b1.toString())
4: System.out.println("True");
5: else
6: System.out.println("False");
A) Compilation error, toString() is not avialable for Byte.
B) Prints "True".
C) Prints "False".
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
some one,please correct me if i am wrong
b1.toString() returns a new String.
== will test whether they both have same memory address.
this is only possible if it is only one object,referenced by two variables.
here two new Strings created have different addresses.
hence false returned
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know how far would this Link help this discussion. Still, do take a look.
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 11, 2001).]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Youre right, Leena. That is whats happening in this case.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roopa!
I agree with leena rane. I think she is right.
Mohammad Aktaruzzaman (Shohel)
 
leena rane
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shyamsundar,
I checked your link.

String s = "a";
String ss = s.toString();
System.out.println(s == ss);

these return true -> fine

but try this,

String s1 = new String("a");
String ss1 = s.toString();
System.out.println(s1 == ss1);

it returns false,
so (what i have concluded - correct me if wrong)
when a object is created using new,and then toString is called another object(String) is returned.
But String literals are an exceptions.
What do u say??
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool! You seem to have a valid argument here. Maybe Jim Yingst can provide us with a definitive answer because he gave me an expanation as to how the toString()works (you saw that in the link I provided).
But then, the new() operator was not involved in my question...Hmmm! Interesting.
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 11, 2001).]
 
This tiny ad will self destruct in five seconds.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic