aspose file tools*
The moose likes Beginning Java and the fly likes difference between integer & integer array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between integer & integer array" Watch "difference between integer & integer array" New topic
Author

difference between integer & integer array

rabby gail
Greenhorn

Joined: Jul 24, 2006
Posts: 12
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

Joined: Mar 02, 2005
Posts: 580
Because array's are objects
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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?


[Jess in Action][AskingGoodQuestions]
rabby gail
Greenhorn

Joined: Jul 24, 2006
Posts: 12
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Mar 02, 2005
Posts: 580
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

Joined: Jul 24, 2006
Posts: 12
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Jul 24, 2006
Posts: 12
sorry ernest i'm confused now
can u help me out with my doubt (i) stated earlier
rabby gail
Greenhorn

Joined: Jul 24, 2006
Posts: 12
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

Joined: Oct 02, 2003
Posts: 11499
    
  16

(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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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.


The Best way to predict your future is to create it - Every great individual common man
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Jul 24, 2006
Posts: 12
Thanks to all

fred,

is array similiar to refference.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between integer & integer array