Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

equals( ) method

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer i = new Integer(42);
Long l = new Long(42);
Double d = new Double(42.0);

Which of the following are true ( Select two answers)

a)(i==l)
b)(i==d)
c)(d==l)
d)i.equals(d)
e)d.equals(i)
f)i.equals(42)

Please give the inputs !!
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the compilation point of view, first three are "incomparable", and the last equals call has int as the parameter when it requires an (Object obj).

So, options 4 and 5 are true, from the compilation perspective.

Regards,
Anil
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's doubtful about d and e are true.

The equals methods of all wrapper class check not only the primitive representation, but also that the checked object is of the same wrapper class (in our case Integer and Double) as well.
 
Anil Hulikal
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as I emphasized, they are true from compilation perspective: In other words, they don't give compilation errors. This does not mean that run time results would be boolean "true".

Please try compiling and running the following code as-is, and then try again with "//" removed for the print statements one at a time.


The following specs from the sections 15.21.3 and 5.5 of JLS may be pertinent here


|
A compile-time error occurs if it is impossible to convert the type of either operand to the type of the other by a casting conversion (�5.5).
|




|
The detailed rules for compile-time correctness checking of a casting conversion of a value of compile-time reference type S (source) to a compile-time reference type T (target) are as follows:
If S is a class type:
If T is a class type, then S and T must be related classes-that is, S and T must be the same class, or S a subclass of T, or T a subclass of S; otherwise a compile-time error occurs.
|




Regards,
Anil
 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic