Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between integer & integer array

 
rabby gail
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pls let me know when local variable are not initialised. it throws an error. but why not when an array in a block not like that.

Thanks
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because array's are objects
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables -- variables defined in a block of statements -- always must be initialized before use. But member variables -- those defined directly in the body of a class -- are automatically initialized to 0/null/false when the object is created, so you're not required to initialize them yourself.

If that doesn't clear it up: can you show brief examples of your error and not-error conditions?
 
rabby gail
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks vidya sagar.

too many but let me start from this

class Test {

public static void main(String args[]) {

int arr[] = new int[2]; // int a ;

System.out.println(arr[0]); // System.out.println(a);

}

}

Thanks Ernest
[ July 26, 2006: Message edited by: rabby gail ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, you're asking why array elements don't have to be initialized? Because every array is automatically initialized to all zeros/null/false values when it is created -- that's just how it wirks.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i stated previously arrays are objects in java.So arr get its default values 0(since array type is int default value is 0).

But in the case of primitive variable, it wont get default value,so compiler complains us.
 
rabby gail
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest it was really nice.
need to know more

public class example {
int i[] = {0};
public static void main(String args[]) {
int i[] = {1};
change_i(i);
System.out.println(i[0]);
}
public static void change_i(int i[]) {
i[0] = 2;
i[0] *= 2;
}
}

result is 4

my doubt here
(i) void returns nothing. if so it should have displayed 1.

(ii) is array like static. retaining values.

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's absolutely wrong, I'm afraid; saying it's because "arrays are objects" misses the point. The difference here is between local variables and array elements. Despite the fact that an array is an object, both a and b in the following

int a;
int[] b;

are uninitialized and will elicit a compiler error if you try to use them.
 
rabby gail
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry ernest i'm confused now
can u help me out with my doubt (i) stated earlier
 
rabby gail
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi vidya,

i understood that arrays r object & is set to default values.

But still what abt when it's passed in methods..
Pls help me out..

Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(i) void returns nothing. if so it should have displayed 1.


a void method does indeed return nothing. however, if the method has and "side effects", they will definatly be seen after the method returns.

you pass a reference to the array into your method. that's like saying "make a copy of Fred's home address, and hand it to someone."

then, inside the method, you say "go to the address i gave you, and paint his door blue. then, throw away your copy of the address. Don't tell me when you're done, don't tell me how much paint you used. Don't even tell me if you succeeded or not.".

so, you call this method. the person goes and paints my door. you never hear back from them. but, the next time you look at my door, it's blue.

it's the same in your code. you pass a way to get to the array into your method. you then use that to change what is inside the array.

you leave the method. the next time you go look at that array, something inside it has changed.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
That's absolutely wrong, I'm afraid; saying it's because "arrays are objects" misses the point. The difference here is between local variables and array elements. Despite the fact that an array is an object, both a and b in the following

int a;
int[] b;

are uninitialized and will elicit a compiler error if you try to use them.


No Ernest,

Here I might be wrong, but I just want to correct you that Visya Sagar is absolutely correct because till now


You cann't called it as an Object it's just a reference.. So we cann't call it an Object..

So that concludes that if there is an Object then it will definately get a default value of 0/false/null etc...

Pls correct me if I am wrong.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankur Sharma:
[CODE]
Pls correct me if I am wrong.


Both you and Visya Sagar are making factually correct statements, but making correct statements is not the same as providing an answer to the original poster's question.

vidya sagar states that array elements are initialized (which is true) and that primitive variables are not (which is at best misleading.) The fact that the int variable is not initialized has nothing to do with the fact that it's a primitive and that the array is an object; it only matters that the int is a local variable, and local variables -- be they primitive or reference -- are not initialized to a default value.
 
rabby gail
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all

fred,

is array similiar to refference.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic