4

Possible Duplicate:
What trick does Java use to avoid spaces in >>?

In C++, nested parameters require extra spaces, so you see things like this:

List< List<String> >

In Java, no spaces are required, and it’s fine to write this:

List<List<String>>

You may use extra spaces if you prefer, but they’re not required. (In C++, a problem arises because >> without the space denotes the right-shift operator. Java fixes the problem by a trick in the grammar.)

Can any body explain what is the trick used in java grammar to solve issue?

1
  • I don't know for sure how it's done, but I'd guess it's related to the fact that it's not valid syntax to have a > at that point that isn't the end of a generic declaration. I.e., Because you couldn't have something like List<4 >> 1> or whatever it's just a matter of checking the brackets match up until you're past the declaration.
    – Vala
    Sep 27, 2012 at 11:54

3 Answers 3

3

The Java language specification has the same problem as C++

http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.2

The longest possible translation is used at each step, even if the result does not ultimately make a correct program while another lexical translation would.

Thus, the input characters a--b are tokenized (§3.5) as a, --, b, which is not part of any grammatically correct program, even though the tokenization a, -, -, b could be part of a grammatically correct program.

This means >> should always be recognized as one token, never two >'s, per spec.

This is probably a (trivial) spec bug, since no one in Java camp actually follows that.

1

Assuming that Java uses an actual grammar, and not some hand-coded mismatch of token extraction (a safe assumption), it's because the parser is attempting to find the end of a parameterization, and it doesn't consider the second angle bracket.

Here's a possible grammar (not likely to be the actual grammar used, and it's been years since I've written a grammar for anything, so anyone who wants to edit should feel free):

typeref          : classname
                 | classname paramaterization

parameterization : '<' typeref '>'

There are only certain places that a typeref can occur: variable/parameter declaration, casts, or following a new operator. The parser sees the opening angle bracket, so knows that it's processing a parameterized type. That parameterization ends with a single closing angle-bracket.

However, the definition is recursive. If it sees another opening angle-bracket, it knows that it's in another parameterization. Again, however, that inner parameterization ends with a single closing angle bracket.

0

My guess (this is how I would do) is that Java looks for pairs, while parsing the code, e.g. (), {}, [], <>, >>, << are pairs.

So while parsing the code if the first character in the pair is read already then it keeps looking for the second character of the pair and once that is found, it starts processing from the next character.

So when it sees the first > in List<List<String>> it considers it as the compliment of second < and so on.

I think C++ could have also done such a thing, but they preferred readability over this... :)

1
  • As the referenced thread mentions, C++ is also fixing this...
    – Curious
    Sep 27, 2012 at 12:00

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