This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Array of zero size 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 » Java » Beginning Java
Bookmark "Array of zero size" Watch "Array of zero size" New topic
Author

Array of zero size

Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
Why I get the junk output for the below program.What am I doing wrong?Wat is wrong at line (1)


Output:
D:\>javac test4.java

D:\>java test4
[I@108786b


Thanks,
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Your printing an array object, and which returns its hashcode value !

See How Object#toString() defaults implementation works !!

Whenver you print object ,it calls it toString() method,


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
i would like to know what happens when we try executing (1) in above code.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

It creates an array of length zero, which is perfectly legal in Java.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

It creates an array of length zero, which is perfectly legal in Java.


It is not only legal to create an array of zero length but it is usually better to simplify your code so that you don't have to special case to see if the array is null or not.

In the following code, the for-loop just works with a zero-length array without any special casing to see if the array is null or not.



In the above code, a zero-length array runs OK, it just doesn't run the loop-body any times. If arr is null then a NullPointerException is thrown.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array of zero size