JLS8 section 4.2.5 conversion of integer/float expressions and references to boolean values
Alex Buckley
alex.buckley at oracle.com
Wed Nov 25 22:02:58 UTC 2015
On 11/19/2015 12:35 AM, Florian.Wiedenmann at grob.de wrote:
> Hello!
>
> I have a question regarding chapter 4.2.5 of the current
> specification JLS8.
>
> The last passage of page 51 and the first one on page 52 refer to the
> conversion of float and int expressions as well as object references
> to a boolean value. I have never heard or seen that before and I'm
> not able to reproduce that behavior with jdk1.8.0_25 (out of date - I
> know ;) ).
I can only assume you mean this text, but that you missed the last
clause in each sentence:
---
An integer or floating-point expression x can be converted to a boolean
value, following the C language convention that any nonzero value is
true, by the expression x!=0.
An object reference obj can be converted to a boolean value, following
the C language convention that any reference other than null is true, by
the expression obj!=null.
---
Alex
> I have tried the following statements:
>
> // the float expression 0.55f - 0.33f is not converted to a boolean
> value - i.e. the entire expression cannot be evaluated // compiler
> message: // error: incompatible types: float cannot be converted to
> boolean boolean b = 0.55f - 0.33f ? true : false;
>
> // trying to assign a float value to a boolean variable // compiler
> message: // error: incompatible types: float cannot be converted to
> boolean boolean b = 0.55f;
>
> // trying to assign a float expression to a boolean variable: //
> error: incompatible types: float cannot be converted to boolean
> boolean b = 0.55f - 0.33f;
>
>
> // trying to convert a new reference to boolean: // incompatible
> types: Object cannot be converted to boolean boolean b = new
> Object();
>
> // trying reference conversion in expression: // error: incompatible
> types: Object cannot be converted to boolean boolean b = new Object()
> ? true : false;
>
> Now I'm wondering if I misunderstood the passage (which is likely -
> I'm not exactly an expert) or whether it's implementation specific
> (maybe it works with OpenJDK?). In any case I think that behaviour is
> not very clear - any of the conversions can easily be written as a
> Boolean expression which tells the reader exactly what it does (e.g.
> new Object() != null ? true : false). I guess it has been added as a
> concession to C developers.
>
> I'd be really happy to hear from you!
>
> Greetings from Germany ;)
>
> Best regards, Florian Wiedenmann
>
>
>
> ________________________________ Die in dieser Nachricht enthaltenen
> Informationen sind streng vertraulich. Sie sind nur f?r den
> Adressaten bestimmt. Falls Sie nicht der beabsichtigte Empf?nger sein
> sollten, l?schen Sie bitte die Nachricht umgehend aus Ihrem System.
> Das Lesen, Speichern, Weitersenden oder jede andere Verwendung der
> Nachricht ist strengstens untersagt. Bitte benachrichtigen Sie den
> Absender (+49 8261 996 0). Vielen Dank! F?r jegliche Sch?den
> resultierend aus der elektronischen Daten?bermittlung durch das
> Internet ?bernehmen wir keine Haftung.
>
> This e-mail contains strictly confidential information. It is only
> intended for the addressee. If you are not the intended recipient of
> this message, please delete the e-mail immediately from your system.
> Reading, saving, forwarding or any other use of the message is
> strictly prohibited. Please notify the sender (+ 49 8261 996 0).
> Thank you! We are not liable for any damage resulting from the use of
> electronical data transmittance via Internet.
>
> GROB-WERKE GmbH & Co. KG, Mindelheim, Amtsgericht/District Court
> Memmingen, HRA 11233 Pers?nlich haftende Gesellschafterin/Partner
> with unlimited liability: GROB-WERKE Verwaltungs GmbH, Mindelheim,
> Amtsgericht/District Court Memmingen, HRB 12669
> Aufsichtsratsvorsitzender/Head of Supervisory Board: Dr. h. c. rer.
> nat. Dipl.-Ing. Burkhart Grob Generalbevollm?chtigter/General
> Representative: Dipl.-Ing. (FH) / Dipl.-Wirtschaftsing. (FH)
> Christian Grob Gesch?ftsf?hrung/Management: Dipl.-Ing. (FH) German
> Wankmiller (Vorsitzender/Chairman of the Board & CEO) Dipl.-Kfm.
> Wolfram Weber (Stellv. Vorsitzender/Vice Chairman of the Board)
> Dipl.-Ing. (FH) Jochen Nahl (Vertrieb/Member of the Board & CSO)
>
More information about the jls-jvms-spec-comments
mailing list