• 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

another tricky point of Java

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java is really tricky.
byte b1=1; //compile
short s1=1; //compile
Byte b2=new Byte(1); //compile error, casting needed
Short s2=new Short(1); //compile error, casting needed
float f1= 1.0; //compile error, casting needed
Float f2=new Float(1.0); //compile
Float f3=new Float (Double.POSITIVE_INFINITY); //compile
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bin,
Byte b2=new Byte(1);
--> gives compiler error because the constructor of Byte accepts either string or byte types, here you are passing an integer value( unless you specify 1 is an integer)
so if you modify Byte b2=new Byte(b1); it will work.

Short s2=new Short(1);
--> same as 1st case.
float f1= 1.0;
--> suffix "f" is always required on a float literal. otherwise it is double so compiler error will occur
Float f2=new Float(1.0); //compile
--> it compiles because the constructor accepts either float or double. here 1.0 is considered as double so no error occurs.

Hope this helps,
Vanitha.

[This message has been edited by Vanitha Sugumaran (edited June 05, 2001).]
reply
    Bookmark Topic Watch Topic
  • New Topic