This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with initialization

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys.
i got it


cheers
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic