It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Majji Exm1 : Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Majji Exm1 : " Watch "Majji Exm1 : " New topic
Author

Majji Exm1 :

eskay kumar
Ranch Hand

Joined: Jul 22, 2000
Posts: 71
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).]
chetan nain
Ranch Hand

Joined: Jun 21, 2000
Posts: 159
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

Joined: Jul 22, 2000
Posts: 71
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

Joined: Jul 22, 2000
Posts: 71
help !
RKB
Greenhorn

Joined: Aug 14, 2000
Posts: 9
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

Joined: Jul 22, 2000
Posts: 71
yeah i understand that about NaN. But what i don't understand is
how come the foll is true:

thanks
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
eskay kumar
Ranch Hand

Joined: Jul 22, 2000
Posts: 71
thanks ajith !
 
jQuery in Action, 2nd edition
 
subject: Majji Exm1 :
 
Similar Threads
Interesting Question
Strings Again.......
toString() method
String literals
== operator