• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Whiz Lab Sample Question

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Double d=Double.valueOf(120D);    //1  Compiles Successfully)

Float f=Float.valueOf(120D);       //2
Long a=Long.valueOf(120D);                       //3
Integer b=Integer.valueOf(120D);
    //4             // But not 2,3&4.

Could someone explain to me, clearly?
Why 2,3 & 4 are giving a compilation error but 1 doesn't?
Thanks for your help, in advance.
 
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because only double can have double value ...which is correct.

Float ,Integer and Long can’t have double value .....which is bigger value for all .

Conversion of bigger value to smaller value needs casting.
 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the case of wrapper object so either they can consider their own value or in string form . But yes float can take float,into and double.
 
R Karthik
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your quick response.
 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ramya Natarajan wrote:Thanks for your quick response.



Welcome ramya😊
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!