Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

character of object array...scjp

 
gokul nair
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Baiju Scariah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"

 
Baiju Scariah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic