• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Majji Exm1 :

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the Majji1 exam.

The ans to the above is false. I compiled and checked and it is false- can someone please explain why it is so, and why it isn't true.
i added the foll to the code to figure out why
String s1=b1.toString();
String s2=b1.toString();
System.out.println(s1);
System.out.println(s2);
and got output :
s1= 127
s2= 127
here since a string object is being constructed - wouldn't the second one i.e. s2, point to the same one in the string pool implying that the == operator should return true.
thanks,
eskay!
[I added UBB CODE tags to your source code to make it more readable. Please try to use them in the future - Ajith]

[This message has been edited by Ajith Kallambella (edited August 30, 2000).]
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
remember that the toString() method returns a new string object for all classes extending object, and
ONLY RETURNS A REFERENCE TO THE SAME OBJECT FOR STRING CLASS.
thus, if you have the code:
if("Test".toString() == "Test".toString())
result will be true

now, in your code of:
public static void main(String[] args) {
Byte b1 = new Byte("127");
if(b1.toString() == b1.toString())
System.out.println("True");
else
System.out.println("False");
}
a new string object is created at each toString() invocation in the if() clause. thus, you have two different objects,
hence , false

hth
chetan
 
eskay kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks chetan for the prompt reply! I got it now.
can u help me with this code

output is :
false
true
now tell me if my logic is right
( Double.NaN == Double.NaN ) false bcoz there is no particular value for NaN implying that all values resulting from a arithmetic expression which are not numbers
or the +_infinity and -_infinity - then they are falling in the set of NaN. Then how come
9: if( a.equals(b) ) is true.
I guess I'm losing it ... i'm giving my exam in 10 days and i think i'm unable to think through things - overstaturated!
Please Help
thanks
shilpa !

[This message has been edited by eskay kumar (edited August 30, 2000).]
 
eskay kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
help !
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
JLS says:
NaN is unordered,
if either operand is NaN
-The numerical comparison operators <, <=, >, and >= return false
-The equality operator == returns false,
-The inequality operator != returns true
 
eskay kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah i understand that about NaN. But what i don't understand is
how come the foll is true:

thanks
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,try search keyword 'majji" in this site and you will get all the discussion about the majji mock exam, you can get the answer you want. good luck.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double.equals() returns true if both the objects being compared contain NaN. I found the following in the java.lang.Double.java source file. It explains the behaviour you have seen



* Note that in most cases, for two instances of class
* <code>Double</code>, <code>d1</code> and <code>d2</code>, the
* value of <code>d1.equals(d2)</code> is <code>true</code> if and
* only if
* <pre>
* d1.doubleValue()�== d2.doubleValue()
* </pre>

* also has the value <code>true</code>. However, there are two
* exceptions:
*


  • If <code>d1</code> and <code>d2</code> both represent
    * <code>Double.NaN</code>, then the <code>equals</code> method
    * returns <code>true</code>, even though
    * <code>Double.NaN==Double.NaN</code> has the value
    * <code>false</code>.
    *
  • If <code>d1</code> represents <code>+0.0</code> while
    * <code>d2</code> represents <code>-0.0</code>, or vice versa,
    * the <code>equal</code> test has the value <code>false</code>,
    * even though <code>+0.0==-0.0</code> has the value <code>true</code>.
    * This allows hashtables to operate properly.


Ajith
[This message has been edited by Ajith Kallambella (edited August 31, 2000).]
 
eskay kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ajith !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic