Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > a unusual codes

Reply
Thread Tools

a unusual codes

 
 
jtl.zheng
Guest
Posts: n/a
 
      07-24-2006
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[i] );
}
------------------
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

 
Reply With Quote
 
 
 
 
Jean-Francois Briere
Guest
Posts: n/a
 
      07-24-2006
yes yes and yes
100% congratulations

 
Reply With Quote
 
 
 
 
jtl.zheng
Guest
Posts: n/a
 
      07-24-2006
haha
thank you very much
: )

 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      07-24-2006
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?
 
Reply With Quote
 
AndrewMcDonagh
Guest
Posts: n/a
 
      07-24-2006
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.
 
Reply With Quote
 
jtl.zheng
Guest
Posts: n/a
 
      07-24-2006
> 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....

 
Reply With Quote
 
Bart Cremers
Guest
Posts: n/a
 
      07-24-2006

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

 
Reply With Quote
 
jtl.zheng
Guest
Posts: n/a
 
      07-24-2006
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?

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      07-24-2006
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


 
Reply With Quote
 
Bart Cremers
Guest
Posts: n/a
 
      07-24-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to write codes to connect access database using html codes hiralcp Software 0 12-18-2007 08:49 PM
How to use file I/o codes with form and controls codes Allen ASP .Net 1 12-03-2007 12:04 AM
Virtual Key Codes, Scan Codes and ASCII Codes in C gj_williams2000@yahoo.co.uk C Programming 2 08-20-2005 11:04 AM
RegEx replace of html codes to ascii codes Greg -- ASP .Net 4 08-09-2005 07:27 PM
ICS unusual problem richardewing@gmail.com Wireless Networking 2 04-14-2005 09:35 AM



Advertisments