Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Indexes And Compiling

 
Craig Oliver
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far I have found Java to be very strict with compiling and run time exceptions if things are not spot on. I was surprised the following compiled and ran okay considering there is no index given for arguments array when printed by method1.
public class MyClass
{
public static void main(String arguments[])
{
MyClass mc = new MyClass();
mc.method1(arguments);
}
public void method1(String arguments[])
{
System.out.println(arguments);
}
}
I was expecting compile error. In C, this would make sense as it would be pointer to start of character array, but in Java I do not understand what happens here. When I ran as
>java MyClass a b
it printed stuff like [java.lang.String;@cac268
Why was no error given or what is story with no index given when reference array?
 
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays are Objects in Java, so what you are seeing is the effect of calling the toString() method on the array Object - its the object reference rather than its actual contents.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic