my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes character of object array...scjp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "character of object array...scjp" Watch "character of object array...scjp" New topic
Author

character of object array...scjp

gokul nair
Greenhorn

Joined: Jul 14, 2005
Posts: 3
Just look on this program

class test
{
Object[] p=new Object[5];
piblic void sum()
{
Object[] q=new Object[2];
String s="hi";
q[0]=s;
s="hello";
q[1]=s;
p[0]=q;//adding an object array to p;
return p;
}
public static void main(String ar[])
{
test test1=new test();
test1.sum();
System.out.println(p[0]);
//here i want to get the values of q;
}
}
the code will compile and run properly.. but how can i access the values of q from p[0]; is it possible
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

p[0] holds an array (which is an object), and as far as i know, you cannot access q's elements through p. Try declaring a multidimensional array (Object[][]).

Cheers, Marzo.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Baiju Scariah
Ranch Hand

Joined: Mar 17, 2005
Posts: 33
Object[] p=new Object[2];
Object[] q=new Object[2];

String s="hi";
q[0]=s;
s="hello";
q[1]=s;

p[0]=q; // Wrong

Write like this:
p[0] = q[0];

Both are single dimension arrays. An element of a single dimension array can't be a reference of another array.

Hope it is clear??

Baiju Scariah
SCJP
gokul nair
Greenhorn

Joined: Jul 14, 2005
Posts: 3
I can't agree with that
if p[0]=q; is wrong, then the compiler should show an error .. am i write?

just try to compile and run the program.. it will run properly..
i think now u got my doubt...
thanks and regards
gokul
Baiju Scariah
Ranch Hand

Joined: Mar 17, 2005
Posts: 33
It has nothing to do with java compiler. Whether it is object reference or an array reference complier is not bothered about that. (Hope you know that an array refernce can be stored in an Object instance
ex: Object obj1 = new String[10] )

In java no error at the time of compilation doesn't mean that what you have written is correct.

There is my answer!!!

" Java compiler will not complain for all the mistakes you make.... "
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Originally posted by Baiju Scariah:
Both are single dimension arrays. An element of a single dimension array can't be a reference of another array.


That's incorrect. An element of a single-dimensional array can be a reference to any type of object, including another array.

Here is a corrected version of the program from the original post. This one outputs "hi"



SCJA 1.0 (98%), SCJP 1.4 (98%)
Baiju Scariah
Ranch Hand

Joined: Mar 17, 2005
Posts: 33
That is correct.

My point was on assigning the array reference to an Object refernce - show him that if that he was unnoticed. Casting must be done only when it is really required right??... Otherwise anything can be stored in anything and casted later into the real type (Most of the cases).

I really agree with you that as an answer for this Qn.
 
 
subject: character of object array...scjp