GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Array Basics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Basics" Watch "Array Basics" New topic
Author

Array Basics

Akanksha Joy
Greenhorn

Joined: Jun 05, 2009
Posts: 17


If I place the [] after the datatype then does it mean that all the variables declared thereafter will be arrays? I mean in the above code 'arr ' is an array but what about 'num'? 'num' is also an declared array?
I am geting compiler error 'incompatible type' at line marked 1.
But If I do:

then 'num' is just a variable of type int, not an array. No compiler error is there.
W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
both are arrays

Edit: In your FIRST example, both are arrays. Hope that wasn't confusing.


SCJP 6, SCWCD 5
Akanksha Joy
Greenhorn

Joined: Jun 05, 2009
Posts: 17
You mean to say that it matters where we place the square brackets?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Akanksha Joy wrote:


If I place the [] after the datatype then does it mean that all the variables declared thereafter will be arrays? I mean in the above code 'arr ' is an array but what about 'num'? 'num' is also an declared array?
I am geting compiler error 'incompatible type' at line marked 1.
But If I do:

then 'num' is just a variable of type int, not an array. No compiler error is there.


I'm not confortable with your programming style. even if it gives the desired results, it seems like bad practice to have int arr[], num; in one line, mixing data types like that. if you want arr to be an array and num to be an integer, I'd put them on separate lines. To me that makes it clearer what you want to do. But that is more about style than correctness I guess.

also in the first example I would have coded int[] arr, num; to make them both arrays, for clarity, but It sounds like that does the same thing, makes them both arrays.
W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
Akanksha Joy wrote:You mean to say that it matters where we place the square brackets?

Yes, for example in your first code snippet, putting [] before variable names makes them all arrays. But if you had something like thisonly j and l are made into arrays, while k is a normal int type. I would highly recommend following Fred's suggestion and putting these on separate lines.for readability's sake and to cause less confusion.
Akanksha Joy
Greenhorn

Joined: Jun 05, 2009
Posts: 17
Thanks a lot....
 
GeeCON Prague 2014
 
subject: Array Basics