File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Basics

 
Akanksha Joy
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
both are arrays

Edit: In your FIRST example, both are arrays. Hope that wasn't confusing.
 
Akanksha Joy
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean to say that it matters where we place the square brackets?
 
Fred Hamilton
Ranch Hand
Posts: 684
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot....
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic