Re: Incompatable conditional operand types



<carl.manaster@xxxxxxxxx> wrote:
> Half half = heightBefore() > heightAfter() ? after : before;
>
> And it doesn't like this line. It tells me, "incompatible conditional
> operand types After and Before".

That code is illegal in Java 1.4, but legal in Java 1.5. For 1.4, the
relevant section of the Java Language Specification is 15.25, which
says:

"If the second and third operands are of different reference types,
then it must be possible to convert one of the types to the other
type (call this latter type T) by assignment conversion (§5.2); the
type of the conditional expression is T. It is a compile-time error
if neither type is assignment compatible with the other type."

Since after can't be assigned to type of before, and before can't be
assigned to the type of after, the expression is illegal. You can solve
the problem by casting to the desired type explicitly in one or both of
the latter two operands.

Half half = heightBefore() > heightAfter() ? (Half) after : before;

The rules defined in Java 1.4 are conservative... that is, there are
safe operations such as yours that they exclude. But they are the rules
nevertheless.

Java 1.5 modifies that statement in section 15.25 to require a capture
conversion of the "lub" type operator, which is defined for type
parameter inference of methods as part of generics. The new version is
less conservative, and accepts your code as legal. In Java 1.5, the
resulting type of that expression is some beastly thing that's not
representable in Java at all, but is nevertheless assignment compatible
with Half. Indeed, it even accepts this rather surprising code:

public class Test
{
private static class A { }
private static interface X { }
private static class B extends A implements X { }
private static class C extends A implements X { }

public static void main(String[] args)
{
boolean b = true;
A a = b ? new B() : new C();
X x = b ? new B() : new C();
}
}

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
.



Relevant Pages

  • Re: Java or C++?
    ... I would like to roughly repeat what James Gosling (Java inventor) said at a lecture I attended: Java is nice because you can work in Java everywhere now - from embedded to desktops to supercomputers and servers. ... Even python is less helpful; ... but C bogs you down with administrative stuff (try converting an int to a string; I found myself googling for an hour!). ... assignment would only do a memory copy ...
    (comp.lang.python)
  • Re: Nulling an object
    ... a popped element would be unexpected and, I hope, clearly ... and the stack user should never need to be aware of it, i.e. if he is, ... In fact, assigning "null" to the variable can actually delay collection, because it causes the variable to be used at a later point in the code than without such an assignment. ... It seems entirely possible to me that Java does the same thing. ...
    (comp.lang.java.programmer)
  • Re: H E L P !!!!
    ... This assignment is due tomorrow at 8 AM. ... > Java but I had to take this course for my BSc in Computer Science. ... > plausible limit values. ...
    (comp.lang.java.programmer)
  • Is it Java or is it Iceweasel?
    ... Warning: assignment to undeclared variable speed ... <!-- Clock in Java Script .. ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx with a subject of "unsubscribe". ...
    (Debian-User)
  • Re: Is it Java or is it Iceweasel?
    ... Warning: assignment to undeclared variable speed ... <!-- Clock in Java Script .. ...
    (Debian-User)