• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Wrapper class Doubt

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:

Integer i = new Integer (42);
Long 1 = new Long (42);
Double d = new Double (42.0);

Which two expressions evaluate to True? (Choose Two)
A. (i ==1)
B. (i == d)
C. (d == 1)
D. (i.equals (d))
E. (d.equals (i))
F. (i.equals (42))
Ans: D,E

Doubt:How D, because two different classes?


Please help,
Regards,
Gitesh
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
(i.equals (42)) is true
reason :
42 can be boxed to Integer and then the equals method will compare the value both the Integer objects are holding .


D) cannot be correct . equals method in the Integer class checks the instance's type using instanceof operator .
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gitesh

I agree with Alok i tried the above
given program,i am getting only
one condition to be true

ie i.equals(42)
Hence the answer is F


The above given answer D and E are wrong!!!
i think you must check it by a small
program!!!

I hope this helps!!

Preparing SCJP 1.5
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class tt {
public static void main(String args[]) {
Integer i = new Integer (42);
Long l = new Long (42);
Double d = new Double (42.0);

System.out.println( "(i ==l) ---> " + ((d ==1)));
System.out.println( "(i.equals (d)) ---> " + ( (i.equals (d))));
System.out.println( "(d.equals (i))" + ((d.equals (i))));
System.out.println( "(i.equals (42)) ---> " + ( (i.equals (42))));
}
}


C:\Documents and Settings\138408\Desktop>java tt
(i ==l) ---> false
(i.equals (d)) ---> false
(d.equals (i))false
(i.equals (42)) ---> true



Apart from the above conditions everything else results in an error of some sort
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic