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.

Recommended Answers

All 11 Replies

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.

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!

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.

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.

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.

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.

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.

...
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.

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.

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

Thanks a lot Masijade, i will look into it.

katharnakh.

how to get the wrapper classes constructor

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.