• 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

Is it correct to say that the the array notation actually 'belongs' to the reference variable?

 
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Working my way through the Java 8 OCA Study guide by Boyarsky and Selikoff (exam 1Z0-808) and have a question regarding the text on page 119/120. The authors break down what the following declaration is:

char[] letters

where:
char is a primitive and what goes into the reference variable
letters is a reference variable
[] notation indicates an array

They give four samples on page 120 that are functionally equivalent (I have modified slightly to match the first example):

char[] letters;
char [] letters;
char letters[];
char letters [];

Until I read this text, I always believed that it was read as 'a reference variable named 'letters' that contains an array of char values' but I may have to modify this based on what I have read. It seems to indicate something slightly different like 'an array named 'letters' that contains char values'. I guess you could argue semantics but it seems different to me.

What concerned me more was the example on page 121 as follows:

The definition of:
int[] ids, types; // two reference variables that are arrays that contain int values)

is DIFFERENT than:
int ids[], types // one reference variable (ids) that is an array that contains int values and an int primitive named 'types'

At first I thought the second line would not compile as it was two different types you were trying to initialize on the same line but based on my 'new' interpretation, they are both int primitives and it is the reference variable that is different (one being an array and another being a regular int).

 
Ranch Hand
Posts: 62
5
Eclipse IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris J Allen wrote:char[] letters

where:
char is a primitive and what goes into the reference variable
letters is a reference variable
[] notation indicates an array


Arrays themselves are objects and thus they are reference types.

Chris J Allen wrote:
char[] letters;
char [] letters;
char letters[];
char letters [];


These are all "legal" for java compiler. The first, however, is the only common, conventional, suggested declaration style.

Chris J Allen wrote:
What concerned me more was the example on page 121 as follows:

The definition of:
int[] ids, types; // two reference variables that are arrays that contain int values)

is DIFFERENT than:
int ids[], types // one reference variable (ids) that is an array that contains int values and an int primitive named 'types'


Correct.

If [] is near the type (spaced or not), it "belongs to the type". Otherwise, if [] is on the right of the variabile, it "belongs to the variable".


int[] var1, var2[][], var3[];

is equivalent to

int[] var1;
int[][][] var2;
int[][] var3;
 
Chris J Allen
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RE: If [] is near the type (spaced or not), it "belongs to the type". Otherwise, if [] is on the right of the variabile, it "belongs to the variable".

That is a nuance that I had not considered. I find Java very flexible in terms of whitespace and variable declarations (i.e. varargs declarations are a great example) but this seems to be pretty rigid. I guess it is something that one has to remember.
 
Andrea Binello
Ranch Hand
Posts: 62
5
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris J Allen wrote:That is a nuance that I had not considered. I find Java very flexible in terms of whitespace and variable declarations (i.e. varargs declarations are a great example) but this seems to be pretty rigid. I guess it is something that one has to remember.


Simple rule: never put [] on the right of variables.

If you want a monodimensional int array and a bidimensional int array, please do:

int[] arr1;
int[][] arr2;

Do NOT do:

int arr1[], arr2[][];

and even less (which is confusionary/not intuitive)

int[] arr1, arr2[];

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic