File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper class question from Danchisolm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrapper class question from Danchisolm " Watch "Wrapper class question from Danchisolm " New topic
Author

Wrapper class question from Danchisolm

Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
Hi all,
This is a question from Danchisolm.

class F {
public static void main (String[] args) {
Float f1 = new Float('B' - 'A'); // 1
Float f2 = new Float(010); // 2
Float f3 = new Float(0x10); // 3
Float f4 = new Float(.1e1); // 4
Float f5 = new Float(1.0); // 5
Float f6 = new Float(1.0d); // 6
System.out.print(f1+","+f2+","+f3+","+f4+","+f5+","+f6);
}}

What is the result of attempting to compile and run the program?

Acoording to them ans is 1.0,8.0,16.0,1.0,1.0,1.0
I tried executing it. The o/p is same as the above.

But my doubt is... How can a Float can take double argument?
Becoz to my knowledge, comipler will not apply an implicit narrowing conversion to the argument.

what is correct in these??
Can anyone help me?

Thanks in Advance
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Hi Lavanya

--------------------------------------------------------
But my doubt is... How can a Float can take double argument?
Becoz to my knowledge, comipler will not apply an implicit narrowing conversion to the argument.

---------------------------------------------------------

Float takes Double argument because it has double constructor

to ensure it check in JAVA API
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
You're probably thinking of float, not Float. This causes a compiler error:

float f = 18.9;
Compiler error message: possible loss of precision

This is fine because it doesn't use automatic casting, just the Float(double d) constructor:
Float f = new Float(18.9);


SCJA 1.0 (98%), SCJP 1.4 (98%)
Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
Thanks.

As all the wrapper classes takes it's primitive type value and String as their constructor arguments... I thought the same case with Float also.

But exceptionally.... Float has a constructor which takes double as it's argument. it's surprising....

anyways.....thanks
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
i didn't get how this will b calculating
Float f4 = new Float(.1e1); // 4

can anyone explain me
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
You're absolutely right, Float is the exception to the normal 2-constructor pattern among the wrapper classes.

It's understandable that they chose to do it this way, since it's generally convenient to be able to call new Float(6.3) rather than always using new Float(6.3F) and the user of Float takes responsibility for losing precision from the double passed in.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Originally posted by Parameswaran Thangavel:
i didn't get how this will b calculating
Float f4 = new Float(.1e1); // 4

can anyone explain me


.1e1 is a numerical notation that is valid in Java. The e can be read as "times 10 to the power of". This example is ".1 times 10 to the power of 1", which yields 1.0

Below are some examples, where I use ^ as an exponent operator (which it isn't in Java, by the way) just to illustrate the math involved. This is not Java code; it's just math:
3.45e5 = 3.45 * 10^5 = 3.45 * 100000 = 345000
.0008e4 = .0008 * 10^4 = .0008 * 1000 = 8.0
45.2e-3 = 45.2 * 10^(-3) = 45.2 * .001 = .0452
3.6e0 = 3.6 * 10^0 = 3.6 * 1 = 3.6
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
thanx joe
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Float f2 = new Float(010); // 2

Could u guys explain exactly above 5 st's complile..
Though it is Funny Ques i cant able to get as i am new biw to this forum and too java toooo...


Pls....


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Originally posted by Gowtham Ganamukala:
Float f2 = new Float(010); // 2

Could u guys explain exactly above 5 st's complile..

I think you're asking why new Float(010) returns a Float that prints out as "8.0". The reason is that in Java a zero (0) at the beginning of an integer literal means octal, or base eight. The octal value 010 is equal to the decimal value 8.
 
jQuery in Action, 2nd edition
 
subject: Wrapper class question from Danchisolm