This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread 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


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: 14195
    
  20

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
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.
 
GeeCON Prague 2014
 
subject: Array of zero size