• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with initialization

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!!
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!!
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys.
i got it


cheers
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic