wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes problem with initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with initialization" Watch "problem with initialization" New topic
Author

problem with initialization

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
public class Test1{
public static void main(String[] args){
int arr[] = new int[3];
for(int i = 0;i < arr.length;i++){
System.out.println(arr[i]);
}
}
}

/***********************question*****************/

it is said that we have to initialize everything in a method.
but here in this program how are these 3 integers initialized to 0 because i get 3 zero's as output.
Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
Whenever you create an array, depending on the data type of array (int, String, double etc.) the elements are initialized to some particular value. For int this value is 0. For String it is null, so if you have this:

you will print 'null' thrice!
jenita brown
Ranch Hand

Joined: Feb 20, 2007
Posts: 51
Dear Prasanna,

Since Arrays are Objects they can be initialized using the new operator and when created Arrays are automatically initialized with the default value of their type.

Here in your case you have initialized the array with type of integer which takes the default value of 0.

See the following examples for default initilization

String[] s = new String[100]; // default values: null
boolean[] b = new boolean[4]; // default values: false
int[] i = new int[10][10]; // default values: 0

Array references declared as members are initialized to null BUT array references declared in methods are not initialized so they wont take the default value. Look at the following code

class TestArray {
int[] arr; // member declaration, initialized to 'null' and not 0

public static void main(String[] args) {
int[] arr1; // reference variable 'arr1' not initialized

// compiles ok
System.out.println("arr:" + new TestArray().arr);
// compile error
System.out.println("arr1: " + arr1);
}
}


I hope that I have been successful in explaining it and hope you understood. All the very Best !!!
Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
Hi Prasana,
It is not must to declare and initialize a variable in the method.

If you are going to declare outside the method that after the class then those variable are globally assigned else if it is in the method its scope will be only with in it.

Once you declare a variable and you did not initialize it that variable will have its default values.For int '0',String and Object 'null' like that.

Have you got it !!!
Sharon Adar
Greenhorn

Joined: Apr 29, 2007
Posts: 18
Hey!

In addition to all the answer above, remember you can always create your own array in creation time by using the following syntax :
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
thanks guys.
i got it


cheers
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: problem with initialization