Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper classes

 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)
public class Test {
public static void main(String[] args)
{
Float f = new Float(32D);
System.out.println(f);
}
}

compiles and prints 32.0.
I thought this should give compiler error because of 32D
whereas
2) public class Test {

public static void main(String[] args){
byte a = 10;
Byte b = new Byte(a);
Byte c = new Byte(11);
System.out.println(b.compareTo(c));
}
}
gives compiler error.
I know this gives compiler error because 11 is int.
3) And why does this give Runtime error.
I thought it should be true
public class Test {

public static void main(String[] args){
Float f = new Float(16/0);
System.out.println(f.isNaN());
}
}

 
Junilu Lacar
Bartender
Posts: 7557
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer to the JavaDocs: Float has an overloaded constructor that accepts a double. Byte does not have a constructor that takes an int.
16/0 is an integer expression since both operands are integers. Integer division by 0 will generate an ArithmeticException. Make one or both operands a float by using 16.0 or 0.0 or both.
Junilu
------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx JUNILU
I got it
Regards
Neha
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
that was cool. i never knew that!!
regards,
maulin.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic