| | |
A question on wrapper class constructor behaviour
Thread Solved |
hi,
why explicit cast is *not* required here?
but requires here,
in case of
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?
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.
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
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.
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.
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
----------------------------------------------
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
•
•
•
•
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.
static Float f3 = new Float(5.5); where by default floating-point literal type is double!Not really... Because im using jdk1.5 on WinXp platform.
thanks for the reply.
katharnakh.
•
•
•
•
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! thanks,
katharnakh.
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
----------------------------------------------
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
•
•
•
•
And what does that have to do with anything? The character used to denote a long is still L and not l.
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 fineBy 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.
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.
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
----------------------------------------------
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
•
•
•
•
...
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.
katharnakh.
•
•
•
•
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.
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.
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
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
----------------------------------------------
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
![]() |
Other Threads in the Java Forum
- Previous Thread: Socket Programming
- Next Thread: Desperately seeking jAVA Help- Inheritance Problem
| Thread Tools | Search this Thread |
6 actuate android api applet application applications array arrays automation balls bank binary bluetooth bold business c++ chat class clear client code codesnippet collections component coordinates database defaultmethod development dice doctype dragging ebook eclipse educational error file formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide ideas image infinite ingres integer intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans openjavafx parameter php problem program programming project recursion repositories scanner scrollbar sell server set sms sort sorting sql sqlserver state storm string sun superclass swing swt threads tree websites windows






