• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Sierra & Bates SCJP 6 - C3 - Q3

 
Greenhorn
Posts: 22
Scala IntelliJ IDE Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

This is my first post at the ranch, so please forgive me if i screw up something.

I'm using the amazing Sun Certified Programmer for Java 6 Study Guide to teach Java to Brazilian Programmers.

Quick question: At Chapter 3, Question 3 (pg 270 in the hard cover release), we see the following code:



There is a comma at the end of {1,2,}, this compiles fine with Java 6.
My not that smart question is if this comma does anything special or is it just something like the ";" after a enum declaration? And is it a bug at Java Implementation keepen in order to mantain compatibility or is it a "feature"? If noone can answer the second one it is ok.

Cheers
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

The comma doesn't do anything special. I don't know why Java allows the extra comma - Java inherited a number of features from the C programming language, maybe this is one of those things. It's not a bug in the Java compiler.
 
Anthony Accioly
Greenhorn
Posts: 22
Scala IntelliJ IDE Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.

Woah! This forum is fast
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Comma has meaning here. It determines the number of entry in the array.
lets example
int[][] abc = {{1,2},{1,2}}
// here array abc is something like int[][] a = new int[2][2] with values.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gautam Pandey:
Hi,
Comma has meaning here. It determines the number of entry in the array.
lets example
int[][] abc = {{1,2},{1,2}}
// here array abc is something like int[][] a = new int[2][2] with values.



int [] arr={1,2,3,};
System.out.println(arr.length);

int [] arr1={1,2,3};
System.out.println(arr1.length);

Then length of these two arrays should differ?
 
Pandey Gautam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without giving comma between two curly braces we can define the size of an array in above type of declaration. If comma will be not their, it will be syntax error.
 
Pandey Gautam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry typo error

Without giving comma between two curly braces we can't define the size of an array in above type of declaration. If comma will be not their, it will be syntax error.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int [] arr={1,2,3,};
see question is talking about this comma that is written after 3, the bold one.
 
Anthony Accioly
Greenhorn
Posts: 22
Scala IntelliJ IDE Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for the input.
And yes, i was refering to the comma before the "}" with no apparent meaning. in {1,2,}
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That coma is not doing anything special, as Jesper said, it is just allowed by the compiler but does not determine the array length, if you try the sample given by Punit:


int [] arr={1,2,3,};
System.out.println(arr.length);

int [] arr1={1,2,3};
System.out.println(arr1.length);



You'll have the output:


3
3



also if you try to add the fourth element to the array with the extra-coma, like:

arr[3] = 4;


you'll get an ArrayIndexOutOfBoundsException

Even if the coma determines the array length, then you would be able to put something like:

int [] arr={1,2,3,,,};


But the compiler does not allow more than one extra-coma

Hope this help you.

Regards,
 
There is no "i" in denial. Tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic