aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array dimension confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array dimension confusion" Watch "array dimension confusion" New topic
Author

array dimension confusion

pawni jain
Greenhorn

Joined: Apr 10, 2008
Posts: 22
hello to all javaranchers ,,

My doubt is simple i think...but i got confused about array dimensions.
Please see at following code..

int[] a = null , b [] = null;
b = a;
System.out.println( b );

output:
compiler error
incompatible type..


compiler take b as 2-d array and a as 1-d array.
but b is also 1-d array here..how come b is 2-d array.

please do reply.

Thanks.
With Regards.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39

compiler take b as 2-d array and a as 1-d array.
but b is also 1-d array here..how come b is 2-d array.


Maybe, because b *is* a 2D array? (ie. an array of arrays)

Take this code....



You can actually separate this to two separate lines...



Does it look more like a 2D array now?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

Please quote the source of your questions. This appears to have come from my exam

It works like this. When you place the [] before a variable name the compiler treats the next element in the declaration as already having a dimension. Which is to say

int[] a = null , b [] = null;

a - one dimension b - 2 dimension

int a[] = null , b [] = null;

a - one dimension b - one dimension


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
Hi John, I did not get your point. Can you please explain with little more details.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

Originally posted by Dean Jones:
Hi John, I did not get your point. Can you please explain with little more details.


What part of the declaration is confusing to you ? As Henry pointed out the declaration can actually be split into 2 lines. If you notice my illustration you will find that the position of the [] characters differs for the first and second example
pawni jain
Greenhorn

Joined: Apr 10, 2008
Posts: 22
Hello

Thanks henry and john..i got the idea behind the code.

Regards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array dimension confusion
 
Similar Threads
Doubt........
Possible error in Master Exam...
Q4 of John's word document
Arrays
passing array as parameter