• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Wrapper HEAVY Confusion

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following class instance creation expressions would generate a compile/run-time error?

class tt {
public static void main(String args[]) {new tt();
new Short("1"); //1
new Short("-1") ;//2
new Short("1.0"); //3
new Short("0x1"); //4
new Short("011") ;}//5
}
options
a 1
b 2
c 3
d 4
e 5
f None of the above

Ans: F


Please help

regards,
gitesh
[ August 31, 2007: Message edited by: Gitesh Ramchandani ]
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Gitesh!!


Why dont you try this example by
compiling and running
,you can definately come to know what 's the answer
This way i think you will learn more!!!


Preparing SCJP 1.5
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gitesh


I just tried the above example

i am getting runtime error at line
3 and 4 ,that is an exception
NumberFormatExceptin!!!



I hope this helps!!!
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi gitesh,

You will get the run time expection at \\3 and \\4 .

thanks,
pradeep,
SCJP1.4
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks dhwani and Pradeep,

I conpiled and got the exceptions, but I'm not able to understand the reason behind the exceptions.

Hope i get the answer,

gitesh
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi gitesh!!
you are flooding the questions..!!! great thing man keep going.
its always good to have questions in mind.... so keep thinking.

the reason behind exception at //line-3 is that short is a subtype of integer and you are passing a double type to that, which to large to resolve for short.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Kumar Meruva:
Hi gitesh,

You will get the run time expection at \\3 and \\4 .

thanks,
pradeep,
SCJP1.4



//3 is obviously not a valid number for a short. Here's the explanation for //4 exception, from the API: "The string is converted to a short value in exactly the manner used by the parseShort method for radix 10".

Such details drive me nuts !!!
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Dave,

if

"The string is converted to a short value in exactly the manner used by the parseShort method for radix 10".



Then how //5 is not giving error, as it is an Octal?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gitesh,
Here it is...

You cannot specify a 'Hexa String'(like "0x12" ) while constructing a Wrapper object. It will through Number format exception at runtime.
Ex: Integer iw = new Integer("0x12"); //runtime exception
Ex: Double iw = new Double("0x12"); //runtime exception

Reason: Wrapper Classes except Character class has a constructor which accepts string as argument. So it will not give the compilet error.
During Run time, it tries to conver the string "0X12" in to Integer as the same way Integer.valueOf("0x12"); does, but it is not able to convert so it throws the Exception;

You can specify Octal string to an Integer class, but it will not take the actual value; it will conver the String in to the wrapper value as valueOf() does.
Ex.
Integer iw = new Integer("012"); // 12 not 10
Byte iw = new Byte("012"); //12
Short iw = new Short("012"); //12

Double iw = new Double ("012"); //12.0
Float iw = new Float("012"); //12.0
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot "Thiru Mu ".

Excellent explanation
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic