class Test{
	
	static String str[];

	public static void main(String args[]){
		System.out.println("args "+args);
		System.out.println("str "+str);
	}
}

Output
args [Ljava.lang.String;@3e25a5
str null

My doubt is ... How some object address is printing for args when it has not initialise.
In case it has got initilised ,without array size specifiction how its is possible to initialize? If i will check by printing args[0], Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 will come...

When i have printed str variable its showing null .

So I have tried by direct initialization like String str[]={};

Now by printing this str i got some address instead of null.

so what is the use of String str[]={}; without any value?

Recommended Answers

All 2 Replies

Arrays are initialized when they have a length of 0. They may not contain any values, however they do store the length and allow you to reference it. If you attempt to reference the length of a null reference, you will get an exception. It can be useful when you are certain that an array is not null; for instance, if you store a final reference to an array, and do your check in the constructor of a class. Then you can avoid additional checks in the class (or at the beginning of a method):

for(int i = 0; i < array.length; i++) {
    ...
}

Instead of performing additional checks:

if(array != null)
    for(int i = 0; i < array.length; i++) {
        ...
    }

The actual usefulness is somewhat negligible, but that depends on your circumstances.

What you have there is an empty array, by practical standards it doesn't serve much purpose considering that an array object's size is immutable. But it is widely used to return from methods.

For example lets say you have a method that returns all the birthdays of people you know for the current week in an array. This method can return any number of dates, even 0 in the case of no one having a birthday on that week. So in the case of that happening what should you return null or an empty array? The correct thing to do is to return an empty array because when the method returns an empty array:
String str = {};
the calling code does not need to directly check what was return before using it, however sending null to another method without checking can cause some serious bugs.

So yea despite not being able to change the size of an already initialized array, an empty array can serve a purpose. It saves you the hassle of having to check what a method returns before using it(in some cases).

As for your initial question about how args has been initialized when you didn't do so explicitly, well that happens implicitly by running the class with the main method, you see when you perform:

java Test hello world//assuming this class has the main method

this creates an array with the strings "hello" and "world" so args[0] would produce hello and args[1] would produce world. But if nothing is written after the class name, then args gets initialized as an empty array because making it null would be too drastic and can cause bugs in your code.

Hope that helps and feel free to ask for clarification on any of the above.

commented: Fine explanation +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.