• Post Reply Bookmark Topic Watch Topic
  • New Topic

which is correct syntax ?  RSS feed

 
sam robert
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

can someone please tell me which of the following are true and which of the following are false ? I would appricate if you can provide me a complete detailed solution?

a. new Short("1")
b. new Short("-1")
c. new Short("+1")
d. new Short("1.0")
e. new Short("0x1")
f. new Short("011")
 
Mahesh Bhatt
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sam,

the incorrect declarations are the following:
1)new Short("+1");
2)new Short("1.0");
3)new Short("0x1") ;

These are the correct answers because, the Short class has only two constructors: one accepts a primitive short; the other accepts a String. A String argument must represent an integral primitive type. A leading minus sign can be added to indicate a negative value. A leading plus sign generates a run-time error. The constructor is not able to determine the radix of the String value by examing a prefix such as 0 or 0x. The 0 prefix used to identify octal values is accepted, but the String is parsed as a decimal value. The prefix 0x generates a run-time error. A run-time error is generated if the String argument is not formatted as a decimal integer. A floating-point format results in a run-time error.
[ October 04, 2004: Message edited by: prashant bhogvan ]
 
Vipin Das
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number format exception is an unchecked one, therefore it will be thrown only during runtime. So the compiler will be happy even if you give new short("*#**%*#_%_"); this is because the compiler knows only
that there is a constructor with string argument nothing else. Another funny thing about the Shorts other constructor is it wont take
new Short(1), i'm not sure but I think this is because a literal is integer by default. But short s = 1; has no problem. and new Short(s) will work.I will be greatful if anybody explain how we can give a short
literal to the constructor Short(short).
 
Andrew Eccleston
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vipin Das:
I will be greatful if anybody explain how we can give a short
literal to the constructor Short(short).


I believe that you can cast the literal int like so: Short((short)1)

Not sure if there's a big advantage, though...

andrew
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sam robert:
Hi all,

can someone please tell me which of the following are true and which of the following are false ? I would appricate if you can provide me a complete detailed solution?

a. new Short("1")
b. new Short("-1")
c. new Short("+1")
d. new Short("1.0")
e. new Short("0x1")
f. new Short("011")


Well, I'm glad to see that several people helped you with your homework. You do realize that by creating a simple program and testing each delcaration, you could have figured out which ones worked and which ones didn't work. But I guess that's too much work? :roll:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!