2

Java allows this:

class X{
    int i,j[]; // j is an array, i is not
}

and even worse, it allows this:

class X{
    int foo(String bar)[][][] // foo actually returns int[][][]
    { return null; } 
}

Okay, the reason for this might be that it was lent from C/C++. However, Java meant to be easier than C/C++. Why did the Java inventors decide to allow this hard-to-read construct. The convoluted types of C where the variable name is in the middle of the type are just hard to read and provoke programming errors.

Especially the brackets behind the method signature. I have never seen these in use and that is for a good reason. No one looks behind the signature when checking the return type of a method. While the first example may save some keystrokes (because int does not have to be written twice), the brackets behind the signature do not even save any, so I see absolutely no gain here.

So is there a good reason for this (especially the second one) that I am missing?

  • 7
    This would be a much better question with the rant edited out. – Blrfl May 12 '14 at 20:21
  • 8
    If this is the worst idea in language design you have seen in decades, you either have a serious problem with hyperbole or you've not spent a lot of time on language design. – user7043 May 12 '14 at 20:21
  • 2
    @Blrfl: True, I have edited it out ;). I was just writing a Java parser and that stuff ruined my nice grammar, so I was indeed quite upset. – gexicide May 12 '14 at 20:34
  • 1
    Java is part of the C ghetto. That's the answer to a lot of the mindbogglingly stupid things Java allows. – Jonathan Landrum May 12 '14 at 20:51
  • 1
    "A Wolf in Sheeps Clothing: C syntax to make developers comfortable" (from the horse's mouth) – gnat May 12 '14 at 21:22
3

The question can be divided into "Why does Java C support brackets behind variables and even behind method signatures?" and "Why would Java inherit so much from C?"; Java also inherited other weird syntax, especially that for switch (why not use curly braces as for everything else and use : and break ?).

To answer the first question - I believe back in the 70s in language design they didn't focus that much on readability but rather on performance. Back then programs weren't that complex or large so readability wasn't a main concern.

To answer the second question - back in the middle '90s when Java appeared most programmers worked in C and C++; C++ had a large success because it was a superset of C by adding OOP. Java simplified C++ as much as possible but tried to keep it close as syntax as much as as possible so that programmers can transit more easily from a language to another.

I actually believe that most Java programmers aren't even aware the code presented is a valid one (at least the second one) and hence do not use it when writing. The main thing that lead to this is a great thing called - Java Code Conventions which is most likely to be read by programmers than the language specification itself. Also this kind of stuff is a disadvantage of backwards compatibility.

| improve this answer | |
  • 1
    How does this syntax improve performance? – FrustratedWithFormsDesigner May 12 '14 at 21:22
  • back in the 70s in language design they didn't focus that much on readability This is just plain wrong. The syntax of B and C were a response to the verbose syntax of Cobol and Algol. – andy256 May 12 '14 at 23:38
  • @FrustratedWithFormsDesigner I said that they didn't focus on readability but on performance; I don't know by which inference you deduced from what I said that this particular syntax improves performance. – Random42 May 13 '14 at 6:18
  • @andy256 In B and C they reduced verbosity thus readability by writing less code - thus less memory occupied by code. – Random42 May 13 '14 at 6:22
  • 2
    @andy256 Cobol is highly readable, far more so for people with no strong background in mathematics than is C. Don't know Algol. – jwenting May 13 '14 at 6:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.