A question on wrapper class constructor behaviour

Thread Solved

Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

A question on wrapper class constructor behaviour

 
0
  #1
May 5th, 2008
hi,
why explicit cast is *not* required here?
static Float f3 = new Float(5.5); //causes compile-time error b'se, by default floating-point literal type is double
but requires here,
//static Short sh1 = new Short((short)12);
	static Short sh1 = new Short(12);		//causes compile-time error b'se, by default integer literal type is int
		
	//static Byte bt = new Byte((byte)10); 
	static Byte bt = new Byte(10); 		//causes compile-time error b'se, by default integer literal type is int

in case of
long var1 = 12l;
	float var2 = 3.2f
I call characters at end of literal 'l' and 'f' as literal-type specifier(correct me if im wrong)

Why there is run-time exception here?
static Long l3 = new Long("20l"); // causes run-time exception - java.lang.NumberFormatException which is not here,
static Float f2 = new Float("5.5f");
	static Double d2 = new Double("4.2d");

Can someone explain why there is such behaviour in constructor?
Am I asking silly question? I dont know, but i want to know WHY.

thanks in advance,
katharnakh.
Last edited by katharnakh; May 5th, 2008 at 4:44 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,347
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 251
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: A question on wrapper class constructor behaviour

 
0
  #2
May 5th, 2008
Because a number, typed directly into the code (i.e a = 15), is an integer. If you want a short, or a byte, or a char you need to cast it.

Edit. Also, the "character" for long is L not l.
Last edited by masijade; May 5th, 2008 at 5:08 am.
Java Programmer and Sun Systems Administrator

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

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: A question on wrapper class constructor behaviour

 
0
  #3
May 5th, 2008
Originally Posted by masijade View Post
Because a number, typed directly into the code (i.e a = 15), is an integer. If you want a short, or a byte, or a char you need to cast it.
Yes, i know... but why it is not the case with static Float f3 = new Float(5.5); where by default floating-point literal type is double!

Originally Posted by masijade View Post
Edit. Also, the "character" for long is L not l.
Not really... Because im using jdk1.5 on WinXp platform.

thanks for the reply.
katharnakh.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: A question on wrapper class constructor behaviour

 
0
  #4
May 5th, 2008
Originally Posted by katharnakh View Post
Yes, i know... but why it is not the case with static Float f3 = new Float(5.5); where by default floating-point literal type is double!
Ok, I got it... Because there is a constructor having a signature like this

thanks,
katharnakh.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,347
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 251
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: A question on wrapper class constructor behaviour

 
0
  #5
May 5th, 2008
Originally Posted by katharnakh View Post
Not really... Because im using jdk1.5 on WinXp platform.
And what does that have to do with anything? The character used to denote a long is still L and not l.
Java Programmer and Sun Systems Administrator

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

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: A question on wrapper class constructor behaviour

 
0
  #6
May 5th, 2008
Originally Posted by masijade View Post
And what does that have to do with anything? The character used to denote a long is still L and not l.
It does. Because, the character 'l' or 'L' can be used to denote long. If you see
static Long l1 = new Long(20l); // works fine
static Long l2 = new Long(20L); // works fine

static Long l2 = new Long("20l");	//causes exception java.lang.NumberFormatException.
static Long l1 = new Long("20L");	// executes fine

By seeing above behavior im little convinced with your statement, that L is used to denote long literal. But if you see the first two statements, is not the case, I mean even l can be used to denote long literal.

katharnakh.
Last edited by katharnakh; May 5th, 2008 at 7:28 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,347
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 251
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: A question on wrapper class constructor behaviour

 
0
  #7
May 5th, 2008
Actually, you may use either l or L when entering the number directly, but neither of them are to be used when entering a String.

And a quick test showed that both variants throw an exception. I think you're missing the quotes on the last example, in the actual code.

Edit: Either that, or the code simply does not attempt to execute the second, as it get an error on the first. They will both compile. Try reversing those two lines in your code (if you have them) and you should notice that the other throws an exception, too.
Last edited by masijade; May 5th, 2008 at 7:39 am.
Java Programmer and Sun Systems Administrator

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

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: A question on wrapper class constructor behaviour

 
0
  #8
May 5th, 2008
Originally Posted by masijade View Post
...
Edit: Either that, or the code simply does not attempt to execute the second, as it get an error on the first. They will both compile. Try reversing those two lines in your code (if you have them) and you should notice that the other throws an exception, too.
Yes, my mistake, thanks a lot.

katharnakh.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: A question on wrapper class constructor behaviour

 
0
  #9
May 5th, 2008
Originally Posted by masijade View Post
Actually, you may use either l or L when entering the number directly, but neither of them are to be used when entering a String.
One more question, is this statement valid with float and double. Because,
static Float f2 = new Float("5.5f"); compiles fine(as above), and also runs fine.
Even similar with static Double d2 = new Double("4.2d");
thanks in advance.
katharnakh.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,347
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 251
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: A question on wrapper class constructor behaviour

 
0
  #10
May 5th, 2008
What does the API documentation say about it?

They both say that the String will be evaluated as if by (and probably because it uses) valueOf from the respective Class. So, read the API docs for the valueOf methods of those classes and you will see what is, and what is not allowed. Double valueOf description even shows a regex you can use to "screen" input, in order to avoid a NumberFormatException.

This sort of question is always best answered using the API docs:
http://java.sun.com/j2se/1.5.0/docs/api/index.html
Java Programmer and Sun Systems Administrator

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

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC