Indexing access for Lists and Maps considered harmful?
Ted Neward
ted at tedneward.com
Wed Jun 24 03:31:17 PDT 2009
This is true regardless of the map/list syntax, though, isn't it?
public class BoxingPuzzler
{
public static void doSomething(int i) { System.out.println("int"); }
public static void doSomething(Integer i) { System.out.println("Integer");
}
public static void doSomething(Object o) { System.out.println("Object"); }
public static void main(String[] args)
{
Object o = null;
doSomething(o = 5);
}
}
This prints "Object" at the command-line. (Interestingly, after a few
seconds of experimentation, I can't get it to invoke the Integer version,
but it definitely doesn't invoke the "int" version. Not having the Object
version yields a compile error. Brighter minds than mine can probably get
Integer to be invoked.)
Ted Neward | Principal Consultant, ThoughtWorks
Java, .NET, XML Services
Consulting, Teaching, Speaking, Writing
http://www.thoughtworks.com | http://www.tedneward.com
> -----Original Message-----
> From: Rémi Forax [mailto:forax at univ-mlv.fr]
> Sent: Wednesday, June 24, 2009 3:04 AM
> To: Bruce Chapman
> Cc: Ted Neward; coin-dev at openjdk.java.net
> Subject: Re: Indexing access for Lists and Maps considered harmful?
>
> Bruce Chapman a écrit :
> > Ted Neward wrote:
> >
> >> Under what situation would a = b = c (where b is boxed(c) ) yield
> different
> >> results? Aside from concurrency scenarios, my understanding is that
> boxed or
> >> not, the value is still "c", thus a would hold the value "c", boxed
> or
> >> otherwise.
> >>
> >>
> > Digressing from a multiple assignment (its just one example of using
> the
> > value of an assigment expression), but still using an assignment
> > expression as an expression then given
> >
> > list<Integer> list;
> >
> > void doSomething(int v) { .... }
> >
> > void doSomething(Integer v) { ...}
> >
> > which method is invoked by the following?
> >
> > doSomething(list[0]=5)
> >
> > If the implementation is consistent with the existing JLS, then it
> MUST
> > be the second method.
> >
> > Bruce
> >
> Yes.
> I think you can rewrite your example as a puzzler :
>
> List<Integer> list = ...
> void doSomething(int v) { .... } //1
> void doSomething(Object o) { ...} //2
>
> doSomething(list[0]=5) // call #2
>
>
> cheers,
> Rémi
More information about the coin-dev
mailing list