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
• Liutauras Vilda
• Tim Cooke
• Paul Clapham
• Jeanne Boyarsky
Sheriffs:
• Ron McLeod
• Frank Carver
• Junilu Lacar
Saloon Keepers:
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Frits Walraven
• fred rosenberger

# Wrapper class question from Danchisolm

Ranch Hand
Posts: 76
• • Number of slices to send:
Optional 'thank-you' note:
• • 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?

Ranch Hand
Posts: 580
• • Number of slices to send:
Optional 'thank-you' note:
• • 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

Ranch Hand
Posts: 195
• • Number of slices to send:
Optional 'thank-you' note:
• • 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);

Pallavi ch
Ranch Hand
Posts: 76
• • Number of slices to send:
Optional 'thank-you' note:
• • 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

Ranch Hand
Posts: 485
• • Number of slices to send:
Optional 'thank-you' note:
• • i didn't get how this will b calculating
Float f4 = new Float(.1e1); // 4

can anyone explain me

Joe Sondow
Ranch Hand
Posts: 195
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
Posts: 195
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
Posts: 485
• • Number of slices to send:
Optional 'thank-you' note:
• • thanx joe

Ranch Hand
Posts: 225 • • Number of slices to send:
Optional 'thank-you' note:
• • 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.... Joe Sondow
Ranch Hand
Posts: 195
• • Number of slices to send:
Optional 'thank-you' note:
• • 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. Ever since I found this suit I've felt strange new needs. And a tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising
{"ft_lang_label":"__label__en","ft_lang_prob":0.66095847,"math_prob":0.7873102,"size":747,"snap":"2022-40-2023-06","text_gpt3_token_len":249,"char_repetition_ratio":0.16689098,"word_repetition_ratio":0.0,"special_character_ratio":0.37884873,"punctuation_ratio":0.22580644,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9543581,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,2,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-02T13:50:33Z\",\"WARC-Record-ID\":\"<urn:uuid:89abf04f-5738-477d-974a-39d46da855f3>\",\"Content-Length\":\"45636\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f3b5a4bf-a777-4833-bcd1-b2e2d204586a>\",\"WARC-Concurrent-To\":\"<urn:uuid:140d4318-d35c-4eba-8f0e-e282269856d8>\",\"WARC-IP-Address\":\"204.144.184.130\",\"WARC-Target-URI\":\"https://www.coderanch.com/t/248690/certification/Wrapper-class-Danchisolm\",\"WARC-Payload-Digest\":\"sha1:PXEX3NZG7K4RLTDSDROPONBLMRDU3DFX\",\"WARC-Block-Digest\":\"sha1:NJWKLW3HR2ZJKTLOWKRA7F6PGAE5TRUB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337322.29_warc_CC-MAIN-20221002115028-20221002145028-00549.warc.gz\"}"}