This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper classes

 
Neha Sawant
Ranch Hand
Posts: 204
  • 0
  • 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
Pie
Posts: 6547
21
Java Linux Mac Scala Spring
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx JUNILU
I got it
Regards
Neha
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
that was cool. i never knew that!!
regards,
maulin.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic