doesn't make sense syntax

P

Peter

Hi
Vector v = new Vector();
v.add((long)-123);
long l=(Long)v.get(0);

variable l will store -123 finally but it doesn't make sense of this
syntax : "long l=(long)v.get(0);", the vector v can store the
primittive type long, but can't do retireve it as long , has to change
it to Object Long first.
May be during this "v.add((long)-123);", -123 will convert to Object
Long before store it to vector, am i correct?

thanks
from Peter ([email protected])
 
E

Edmond wong

Peter said:
Hi
Vector v = new Vector();
v.add((long)-123);
long l=(Long)v.get(0);

variable l will store -123 finally but it doesn't make sense of this
syntax : "long l=(long)v.get(0);", the vector v can store the
primittive type long, but can't do retireve it as long , has to change
it to Object Long first.
May be during this "v.add((long)-123);", -123 will convert to Object
Long before store it to vector, am i correct?

thanks
from Peter ([email protected])

Yes, you are correct. It looks like you are using some JDK 1.5's
shorthand. In JDK1.4, line "v.add((long)-123);" will give you a
compiler error.

Edmond
 
P

Peter MacMillan

Peter said:
Hi
Vector v = new Vector();
v.add((long)-123);
long l=(Long)v.get(0);

variable l will store -123 finally but it doesn't make sense of this
syntax : "long l=(long)v.get(0);", the vector v can store the
primittive type long, but can't do retireve it as long , has to change
it to Object Long first.
May be during this "v.add((long)-123);", -123 will convert to Object
Long before store it to vector, am i correct?

thanks
from Peter ([email protected])


Vectors don't store primative types. In 1.4, your code won't compile. In
1.5, the v.add(...) line will "autobox" the long into a Long (eg. it
will be equivlant to: v.add(new Long(-123));)

The third line, long l = ..., works under the same "autobox"ing feature.
It converts the Long object back into the primative long. This is a
feature of 1.5. However, since you're using the basic Vector (and not,
say, Vector<Long>) the type of v.get(0) is Object. Your cast is
unchecked and may cause a compiler warning (jdk 1.5.0_02).

//---
Vector<Long> v = new Vector<Long>();
v.add(-123L);
long l = v.get(0);
//---
 
P

Peter

Peter MacMillan said:
Vectors don't store primative types. In 1.4, your code won't compile. In
1.5, the v.add(...) line will "autobox" the long into a Long (eg. it
will be equivlant to: v.add(new Long(-123));)

The third line, long l = ..., works under the same "autobox"ing feature.
It converts the Long object back into the primative long. This is a
feature of 1.5. However, since you're using the basic Vector (and not,
say, Vector<Long>) the type of v.get(0) is Object. Your cast is
unchecked and may cause a compiler warning (jdk 1.5.0_02).

//---
Vector<Long> v = new Vector<Long>();
v.add(-123L);
long l = v.get(0);
//---

thanks guys
this line "long l = v.get(0);" doesn't work in jdk 1.5.0. If it
auto-box to Long when add(), pls auto-box to long when .get() but it
don't, so the syntax doesn't look good (it is only my opinion)
thanks
from Peter ([email protected])
 
P

Peter MacMillan

Peter said:
thanks guys
this line "long l = v.get(0);" doesn't work in jdk 1.5.0. If it
auto-box to Long when add(), pls auto-box to long when .get() but it
don't, so the syntax doesn't look good (it is only my opinion)
thanks
from Peter ([email protected])

Java syntax doesn't come down to opinion.


Vector<Long> v = new Vector<Long>();

Creates a Vector that only contains Long objects (and subclasses). The
signature of the add and get functions are as follows (for Vector<Long>):

void add(Long o);
Long get(int pos);

Thus, the statement: v.add(-123L); only works because the -123L is (1)
recognized as a long and (2) automatically placed into a Long object
(which satisifies the Long parameter). The statement: long l = v.get(0);
works because the return type of v.get is Long. Our variable is a long
and so it automatically extracts the long value from the Long object.

The 1.4 syntax would be:

//---
Vector v = new Vector();
v.add(new Long(-123L));
Object obj = v.get(0);
if (obj instanceof Long) {
long l = ((Long)obj).longValue();
}
//---

Because the Vector is anonymous (it contains only Objects), we *must*
check the type before casting it. Anything else would be unsafe.

The 1.5 syntax allows:

//---
Vector<Long> v = new Vector<Long>();
v.add(-123L);
long l = v.get(0);
//---

This creates a specialized Vector that contains Long objects (and not
Object). The compiler can recognize that the -123L has to be converted
into a Long to be stored into the Vector because of the parameter type.
Similarly, it can extract the long from the Long when we assign it to a
long.

If this is confusing, I strongly suggest you familarize yourself with
the 1.5 documentation.

http://java.sun.com/j2se/1.5.0/docs/

Specifically:
http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
 
M

Mark 'Kamikaze' Hughes

Peter MacMillan said:
The 1.4 syntax would be:
//---
Vector v = new Vector();
v.add(new Long(-123L));
Object obj = v.get(0);
if (obj instanceof Long) {
long l = ((Long)obj).longValue();
}
//---
Because the Vector is anonymous (it contains only Objects), we *must*
check the type before casting it. Anything else would be unsafe.

What's unsafe is that your code silently ignores inappropriate objects
being in the collection. The correct 1.4 way to do that is:

Vector timestamps = new Vector();
timestamps.add(new Long(-123L));
long l = ((Long)timestamps.get(0)).longValue();

If you foolishly put a non-Long object in the Vector, you'd get a
ClassCastException, and could work back from the stack trace to the
programmer error that made the initial mistake.

Putting the wrong object type in a container is a nearly impossible
mistake to ever make if you're even halfway competent, and test-first
development would weed it out quickly, but the error checking is there
for a reason. Working around it and throwing away data as you did is
irresponsible. Someone who did that in production code wouldn't be
working with me for very long.

Autoboxing is a minor convenience, *if* you know exactly what it's
doing, but it's also incredibly confusing and error-prone, as the
original poster has discovered the hard way. Sun did not do us any
favors with this feature.
 

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

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top