Path: csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!feeder.news-service.com!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Java puzzler Date: Tue, 10 May 2011 20:16:06 -0400 Organization: albasani.net Lines: 47 Message-ID: References: <871987d9-1034-441d-9d33-b2dd6b4de234@glegroupsg2000goo.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.albasani.net KfWRiWEWsQzRGckAFNTEQpd6iZ26Ka4SVyCDV9Fdj5+myM7ikVT92FLVGVxCKgaCP3BDzr+EVJv3H52ubG0zcg== NNTP-Posting-Date: Wed, 11 May 2011 00:16:03 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="iQ0Lxig6T+0/CNl8/NV84WaNryktVkKa6D3tgZ7KglAAl2/ijf7M4HmnI0wBaptk29Bjik7UVWxs7zhvZd4tGXHxE+XDrDCoUGMg7y4P5OxptCmhdmokUTwK0wpAxF2s"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110424 Thunderbird/3.1.10 In-Reply-To: <871987d9-1034-441d-9d33-b2dd6b4de234@glegroupsg2000goo.googlegroups.com> Cancel-Lock: sha1:HVmif3zC7jP89xgpCBOUHaH6IAE= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3942 On 05/10/2011 07:36 PM, Gábor Bakos wrote: > Hello, > > Sorry if this is already a known behaviour, but first I found this really strange: > int x = 0; > x += 0.5; > What does the code snippet above do: > - compile and run without error result is > - 0 > - 1 > - depends on the strict or nonstrict computation > - compile and ArithmeticException is thrown > - does not compile (Type mismatch: cannot convert from double to int) What happened for you when you tried it? > For those who want to know the answer check this[1] and this[2]. > ... > [1]: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26.2 > [2]: http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.3 What precisely did you find strange? package com.lewscanon.eegee; /** * Conversioneer. */ public class Conversioneer { /** * main. * @param args String [] */ public static void main( String [] args ) { int x = 0; x += 0.5; System.out.println( "x = "+ x ); } } -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg