This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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


Win a copy of Java 8 in Action this week in the Java 8 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: 13884
    
  10

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: 688

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array of zero size
 
Similar Threads
array doubt
enhanced for loop problem -- from a mock
Why will I get this output?
working of increment operator
What is the input file?