aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper classes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Wrapper classes" Watch "Wrapper classes" New topic
Author

Wrapper classes

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
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());
}
}


nss
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4118
    
    2

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


Junilu - [How to Ask Questions] [How to Answer Questions] [MiH]
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
thanx JUNILU
I got it
Regards
Neha
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1865
hi,
that was cool. i never knew that!!
regards,
maulin.

1. Have fun @ http://faq.javaranch.com/java/JavaRaq
2. Looking for simple infix2postfix conversion and postfix evaluation package? Click here
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Wrapper classes
 
Similar Threads
More questions
doubts in 4 questions
compiler error?
Wrapper class constructors
overloaded methods