wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sierra & Bates SCJP 6 - C3 - Q3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sierra & Bates SCJP 6 - C3 - Q3" Watch "Sierra & Bates SCJP 6 - C3 - Q3" New topic
Author

Sierra & Bates SCJP 6 - C3 - Q3

Anthony Accioly
Greenhorn

Joined: Dec 22, 2008
Posts: 16

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

Joined: Aug 16, 2005
Posts: 14117
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Anthony Accioly
Greenhorn

Joined: Dec 22, 2008
Posts: 16

Thank you very much.

Woah! This forum is fast
Pandey Gautam
Greenhorn

Joined: Nov 05, 2008
Posts: 14
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.


SCJP6
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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?


SCJP 6
Pandey Gautam
Greenhorn

Joined: Nov 05, 2008
Posts: 14
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

Joined: Nov 05, 2008
Posts: 14
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

Joined: Oct 16, 2008
Posts: 952
int [] arr={1,2,3,};
see question is talking about this comma that is written after 3, the bold one.
Anthony Accioly
Greenhorn

Joined: Dec 22, 2008
Posts: 16

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

Joined: Jan 02, 2008
Posts: 48
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,


SCJP 5.0<br />Preparing SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sierra & Bates SCJP 6 - C3 - Q3