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

Sierra & Bates SCJP 6 - C3 - Q3

 
Anthony Accioly
Greenhorn
Posts: 16
IntelliJ IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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: 16
IntelliJ IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.

Woah! This forum is fast
 
Pandey Gautam
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 16
IntelliJ IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • 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,}
 
Alejandro Galvan
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • 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,
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic