a unusual codes

Discussion in 'Java' started by jtl.zheng, Jul 24, 2006.

  1. jtl.zheng

    jtl.zheng Guest

    the codes:
    -------------------
    Object[] stuff = new Object[5];
    stuff[0] = "eggs";
    stuff[1] = new StringBuffer( "flour" );
    stuff[2] = 3.56;
    stuff[3] = 'c';
    stuff[4] = 123;
    stuff[0]="33";
    for( int i=0; i<stuff.length; i++ ) {
    System.out.println( stuff );
    }
    ------------------
    It seems odd.....

    followings is what I guest. please tell me whether it's correct, thank
    you very much

    1:
    what I think is because that the Object Class is any class's
    superClass, so it can point to any type (right?), but in these codes it
    only have Object Class's interface, not the interface which it point
    to.
    is it right?

    2:
    stuff[0]="eggs"

    in this sentence,the JVM create a String object(right?), although it's
    a String object and of course it has all the String object's
    interface, but what the reference(stuff[0]) can access is only the
    Object Class's interface.not the String interfaces(just like
    concat(),charAt()....)
    is it right?

    3:
    stuff[0]="eggs"
    System.out.println( stuff[0] );

    and when print it,the print function call stuff[0]'s toString()
    function,and this toString() is the String object's , not the Object
    Class's
    is it right?

    thank you very much

    JTL
     
    jtl.zheng, Jul 24, 2006
    #1
    1. Advertising

  2. yes yes and yes
    100% congratulations :)
     
    Jean-Francois Briere, Jul 24, 2006
    #2
    1. Advertising

  3. jtl.zheng

    jtl.zheng Guest

    haha
    thank you very much
    : )
     
    jtl.zheng, Jul 24, 2006
    #3
  4. jtl.zheng

    Mark Space Guest

    jtl.zheng wrote:

    > stuff[2] = 3.56;
    > stuff[3] = 'c';
    > stuff[4] = 123;


    But these assignments can't work at all, can they? These are
    primitives, not objects, so they don't derive from Object.

    stuff[2] = new Float(3.56);

    Would be the correct syntax? Or do primitives get promoted to objects
    somehow?
     
    Mark Space, Jul 24, 2006
    #4
  5. Mark Space wrote:
    > jtl.zheng wrote:
    >
    >> stuff[2] = 3.56;
    >> stuff[3] = 'c';
    >> stuff[4] = 123;

    >
    > But these assignments can't work at all, can they? These are
    > primitives, not objects, so they don't derive from Object.
    >
    > stuff[2] = new Float(3.56);
    >
    > Would be the correct syntax? Or do primitives get promoted to objects
    > somehow?


    You can have arrays of primitives.
     
    AndrewMcDonagh, Jul 24, 2006
    #5
  6. jtl.zheng

    jtl.zheng Guest

    > stuff[2] = 3.56;
    > stuff[3] = 'c';
    > stuff[4] = 123;


    it can be compiled in JBuilder
    and it print:
    ----------------
    33
    flour
    3.56
    c
    123
    ----------------

    stuff[4]=123;
    I think it is turn to "new Integer(123);" automatismly when in
    compiling
    I guest....
     
    jtl.zheng, Jul 24, 2006
    #6
  7. jtl.zheng

    Bart Cremers Guest

    Mark Space schreef:

    > jtl.zheng wrote:
    >
    > > stuff[2] = 3.56;
    > > stuff[3] = 'c';
    > > stuff[4] = 123;

    >
    > But these assignments can't work at all, can they? These are
    > primitives, not objects, so they don't derive from Object.
    >
    > stuff[2] = new Float(3.56);
    >
    > Would be the correct syntax? Or do primitives get promoted to objects
    > somehow?


    Since Java 5 this is correct syntax. The auto-boxing feature will
    actually compile this to:

    ...
    stuff[0] = "eggs";
    stuff[1] = new StringBuffer("flour");
    stuff[2] = Double.valueOf(3.56D);
    stuff[3] = Character.valueOf('c');
    stuff[4] = Integer.valueOf(123);
    stuff[0] = "33";
    ...

    Regards,

    Bart
     
    Bart Cremers, Jul 24, 2006
    #7
  8. jtl.zheng

    jtl.zheng Guest

    so what I suppose is that any superClass's reference can point to its
    any subClass
    but what the reference can access is only the superClass's interface,
    not the subClass's
    and the method it called is exactly what the subClass has overrided.
    is it right?
     
    jtl.zheng, Jul 24, 2006
    #8
  9. jtl.zheng

    Chris Uppal Guest

    Mark Space wrote:
    > jtl.zheng wrote:
    >
    > > stuff[2] = 3.56;
    > > stuff[3] = 'c';
    > > stuff[4] = 123;

    >
    > But these assignments can't work at all, can they? These are
    > primitives, not objects, so they don't derive from Object.


    You are correct, but...

    > stuff[2] = new Float(3.56);
    >
    > Would be the correct syntax? Or do primitives get promoted to objects
    > somehow?


    Yes, the compiler (since 1.5) automatically converts the original expression
    into something similar to what you have. It's called "autoboxing" and is a
    bloody stupid idea.

    The compiler actually generates code to call the static valueOf(<primitive>)
    method rather than using an explicit constructor, which can in some cases make
    use of cached lists of pre-allocated instances. E.g. the line stuff[4] = 123
    will generate a call to the (new in 1.5) method Integer.valueOf(int), which
    (from the 1.5 code, not the spec) will in fact use the cache since 123 is in
    the cached range [-128, 127].

    -- chris
     
    Chris Uppal, Jul 24, 2006
    #9
  10. jtl.zheng

    Bart Cremers Guest

    jtl.zheng schreef:

    > so what I suppose is that any superClass's reference can point to its
    > any subClass
    > but what the reference can access is only the superClass's interface,
    > not the subClass's
    > and the method it called is exactly what the subClass has overrided.
    > is it right?


    An array of Object's can contain any Object. As every class in Java
    extends Object, the array can contain every single instance of any
    class.

    Without casting you can only access the methods defined in Object,
    that's correct, and if the method you're trying to call is overridden
    by the subclass, the overridden method will be called.

    Example:

    String test = "a string";
    Object o = test;

    test.substring(5); // Correct
    o.substring(5); // Incorrect
    ((String) o).substring(5); // Correct

    The reason the second one is incorrect is that the compiler does not
    know it actually is a String. In the third line you tell the compiler
    to trust you (the programmer) and assume the object o references
    actually is a String and cast it before executing the method.

    regards,

    Bart
     
    Bart Cremers, Jul 24, 2006
    #10
  11. jtl.zheng

    Piotr Kobzda Guest

    Chris Uppal wrote:

    > Mark Space wrote:
    >> Would be the correct syntax? Or do primitives get promoted to objects
    >> somehow?

    >
    > Yes, the compiler (since 1.5) automatically converts the original expression
    > into something similar to what you have. It's called "autoboxing" and is a
    > bloody stupid idea.


    What's stupid in that idea? Can you argue please?

    >
    > The compiler actually generates code to call the static valueOf(<primitive>)
    > method rather than using an explicit constructor, which can in some cases make
    > use of cached lists of pre-allocated instances. E.g. the line stuff[4] = 123
    > will generate a call to the (new in 1.5) method Integer.valueOf(int), which
    > (from the 1.5 code, not the spec) will in fact use the cache since 123 is in
    > the cached range [-128, 127].


    That's from spec (JLS 3rd ed.):

    "5.1.7 Boxing Conversion

    (...)

    If the value p being boxed is true, false, a byte, a char in the range
    \u0000 to \u007f, or an int or short number between -128 and 127, then
    let r1 and r2 be the results of any two boxing conversions of p. It is
    always the case that r1 == r2."


    piotr
     
    Piotr Kobzda, Jul 24, 2006
    #11
  12. jtl.zheng

    Bart Cremers Guest

    Piotr Kobzda schreef:

    > Chris Uppal wrote:
    >
    > > Mark Space wrote:
    > >> Would be the correct syntax? Or do primitives get promoted to objects
    > >> somehow?

    > >
    > > Yes, the compiler (since 1.5) automatically converts the original expression
    > > into something similar to what you have. It's called "autoboxing" and is a
    > > bloody stupid idea.

    >
    > What's stupid in that idea? Can you argue please?


    I'm not totally against autoboxing, but it should be used with great
    care. Specially when used in conjunction with generics. Take following
    code:

    List<Integer> carefull = new ArrayList<Integer>();

    carefull.add(1);
    carefull.add(43);
    carefull.add(227);

    carefull.remove(43);

    System.out.println(carefull.size());

    This compiles fine and the output seems pretty straightforward, but
    what you get is this:

    Exception in thread "main" java.lang.IndexOutOfBoundsException:
    Index: 43, Size: 3

    on the remove line.

    Regards,

    Bart
     
    Bart Cremers, Jul 24, 2006
    #12
  13. jtl.zheng

    Chris Uppal Guest

    Piotr Kobzda wrote:

    > > Yes, the compiler (since 1.5) automatically converts the original
    > > expression into something similar to what you have. It's called
    > > "autoboxing" and is a bloody stupid idea.

    >
    > What's stupid in that idea? Can you argue please?


    The basic problem is that it makes it look as if one thing is happening, when
    in fact something completely different is happening. That is just bad language
    design, for both beginners and experts. /Especially/ when it doesn't add to
    the expressive power of the language.

    (Of course the /real/ problem here, is the use of non-object primitive types at
    all -- at least as the "normal" way to express numeric values -- but that's a
    different debate...)

    It (autoboxing, or indeed any other sleight-of-hand[*] in the compiler) can
    cause the unwary to introduce performance problems, or semantic problems,
    without realising it. E.g, some time ago, there was a person posting about
    memory problems here -- it was only after some considerable amount of
    speculation and debate that we realised he was holding around a million
    "floats" in an ArrayList (or something like that). Since he didn't realise
    that his -- otherwise perfectly reasonable -- Java code wasn't doing what he
    thought it did, he didn't initially mention how he was storing all those
    values. That only emerged later.

    Autoboxing causes (yet more) ugly little glitches with generics too -- but I
    can't remember exactly what and it's too hot to go searching ;-)


    > That's from spec (JLS 3rd ed.):


    Aha! Thank you for the correction.

    -- chris

    [*] "Sleight-of-hand" as in the shell-and-bean game, or its variants, where the
    unwary punter is induced to bet, unwisely, that the bean is under the shell it
    /seemed/ to be placed under....
     
    Chris Uppal, Jul 25, 2006
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Duke of Hazard

    newbie: unusual email output

    Duke of Hazard, Jan 21, 2004, in forum: Perl
    Replies:
    4
    Views:
    758
  2. William A. Sempf

    Unusual performance problem.

    William A. Sempf, Aug 27, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    378
    William A. Sempf
    Aug 27, 2003
  3. Greg  --
    Replies:
    4
    Views:
    2,214
  4. Replies:
    2
    Views:
    2,897
    Malcolm
    Aug 20, 2005
  5. Allen
    Replies:
    1
    Views:
    667
    Mark Rae [MVP]
    Dec 3, 2007
Loading...

Share This Page