2
votes
public class Ambiguity {
    public static void main(String[] args) {
        int Integer = 3;
        System.out.println((Integer) null);       // cast; prints "null"
        System.out.println((Integer) - Integer);  // subtraction; prints "0"
        System.out.println((Integer) (-Integer)); // cast; prints "-3"
    }
}

In Java, I can declare a variable named Integer, and then write code that could be a cast to the Integer class or an expression involving the Integer variable. How does Java resolve the ambiguity between class names and variable names? Is there a formal rule somewhere in the spec? Is it an ad-hoc heuristic thing? I know C compilers have to deal with a similar problem, and I know how they deal with it, but Java seems to be more permissive.

3
can you share more details ? Have you read about Autoboxing ? - Geek
You need to post the code - Juned Ahsan
I tried, but the site wouldn't let me. I'll try it again in an edit. EDIT: Okay, there's a code sample now. - user2357112 supports Monica
Your question is quite abstract. Please give some examples. That would help community come up with clear solutions. - Santosh
I can appreciate that this may just be something you're wondering about, but note that, according to Java coding standards I know of, variable names should start with a lower-case letter and class names with an upper-case letter, so this should really ever be a problem. Also, don't name something Integer/integer. - Bernhard Barker

3 Answers

1
votes

Please refer to JLS for evaluation order

"The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right."

(Integer) -Integer

Now when this is parsed,its clear that there is a subtraction operator with (Integer) and Integer as operands. Starting from the left, the parenthesis expression should be evaluated to a value (as its followed by and operator) and hence it wont assume that as Type but the variable.

(Integer) (-Integer)

When this is parsed, there are two tokens (each within parenthesis) without any operator and hence no ambiguity (of whether the expression is value or Type). The first parenthesis will always be the Type and expression followed by that will always be evaluated to a value to casted.

0
votes

As far as I know the parser goes from right to left. The first statement is obvious.

In the second statement the parser first finds a variable and then an expression "-" after that sign he expcts another variable, but he finds a right parenthesis, which means he first need to parse the expression whithin the the parenthesis. In your example there is only the variable. So the parser takes the value and subtrakt it with the other Variable.

In the third statement the parser first finds a right parenthesis => he will parse the expression within the brakets (-3) after that he didn't find any operatoion (+, *, /, - e.g.) so the next braket means this is a Cast.

-1
votes

There is no ambiguity, compiler always expects either Class or a variable, there is no operation where both would fit. E.g.

    Integer Integer  = 1;
    Integer = new Integer(1);
    Integer = Integer  + 1;

1) in assigment there is always type / variable name pair

2) you can call new only on a Class

3) in arithmetic operation cannot be any class name