A question on wrapper class constructor behaviour

K

kath

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

Mayur

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.

This is not a silly question. It is a strange behaviour in Long class,
even i would like to know the reason why it is so?
 
T

Thomas Fritsch

kath wrote:
[...]
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");
Eeeh, really? I have java.lang.NumberFormatException also there.
The constructors are declared as
public Float(String s) throws NumberFormatException
public Double(String s) throws NumberFormatException
as seen in the JavaDocs at
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Float.html> and
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Double.html>
 
K

kath

why explicit cast is *not* required here?
static Float f3 = new Float(5.5); //compiles successfully
Sorry for the mistake in first post.
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

thanks.
 
K

kath

Thanks Thomas, the link really helped.

I also have another case, which is tells me i should really clarify.

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

static Long l1 = new Long(20L); // works fine
static Long l2 = new Long("20L"); // executes fine

I am using jdk1.5 on WinXp platform. From the above behavior what i
should understand i can use both 'l' or 'L' or only 'L' to represent
long literal.

thanks in advance.
 
J

Joshua Cranmer

kath said:
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

From the JLS 3, § 5.3 Method Invocation Conversion:
Method invocation conversions specifically do not include the implicit
narrowing of integer constants which is part of assignment conversion
(§5.2). The designers of the Java programming language felt that
including these implicit narrowing conversions would add additional
complexity to the overloaded method matching resolution process (§15.12.2).
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)

The JLS uses the terms "Integer type suffix" and "Float type suffix."
 
M

Mark Space

Joshua said:
The JLS uses the terms "Integer type suffix" and "Float type suffix."

BTW, using a lower case L makes it hard to distinguish between L and 1.
12l looks like 121. Use an upper case L.

long var1 = 12L;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top