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.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
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 isstill L and not l.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
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.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
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
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494