This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Pointless semantics or important? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pointless semantics or important? " Watch "Pointless semantics or important? " New topic

Pointless semantics or important?

David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
The JLS calls this: int[][] an array of arrays, even though it also refers to it as a 2D array. I think about it as a 2D array or matrix because traversals through an array of arrays is exactly as you would expect traversing across a 2D matrix.

I usually consider stuff like this to be like the tomato pronunciation argument. This weekend I was working on a distributed multiprocessing project in C, where I am using a 2D char array, which is actually a dynamically allocated array of pointers to arrays of chars. Anyway, it just got me thinking about how many times I have read someone on these and other Java boards refer to a 2D array only to get corrected semantically.

The reason this bothers me is that Sun, for better or worse, decided to hide memory details and the almost always refer to elements of the language and API in more abstract terms. Memory details are irrelevant, yet when it comes to this, a proper description of what is going on in memory is seen as more appropriate to a more abstract term that describes its behavior.

Does it really matter?
[ June 04, 2007: Message edited by: David McCombs ]

"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

I would say it does matter. As you know, the array-of-pointers-to-char is like Java's "2D arrays", but C also has another kind of 2D array, the "real" kind: a contiguous block of memory, where you do a little computation involving both the row and column number to get the offset into the block for each element.

In the case of arrays, Sun didn't hide all the details; there are things you can do with the ptr-to-ptrs kind of matrix that you just cant do with the other kind, and vice versa. If you think of it as a 2D array, then the ptr-to-ptrs manipulations don't seem obvious, even though they're common things to do. For example, the Java kind doesn't have to be rectangular, while "real" 2D arrays do. The Java kind can be dynamically allocated in both dimensions, and can "grow" (by copying and reallocating) at runtime in either dimension, while the "real" kind has at least one dimension permanently fixed at compile time.

[Jess in Action][AskingGoodQuestions]
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Thanks for your comments, I understand what you are saying and agree, It just seems like a semantic argument in most cases.

I guess I just haven't used them for anything more then basic purposes so it doesn't seem as important.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
It may seem silly for people who use Java only (or only other languages that don't have true multidimensional arrays). But for people who also use languages that do have both types of arrays, it's useful to keep the two types straight, and preferable to use consistent terminology even when programming in Java.

There's a similar issue between passing by reference, and passing a reference by value. To Java-only programmers these sound like the same thing, because Java does not support true pass by reference. But to people who use other languages that do support it, it's an important distinction.

"I'm not back." - Bill Harding, Twister
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
I agree with the importance of the pass by reference/value issue. I have no problem keeping that straight, even when pass by value behaves like pass by reference. I think the usage of the "phrase passing a reference to x" is the root of the confusion.

I will read up more on the gory details of 2D arrays and array of arrays.

Thanks for the input.
Have you checked out Aspose?
subject: Pointless semantics or important?
It's not a secret anymore!