Newsgroup: comp.lang.java.programmer


Date: Wed, 12 Apr 2006 15:44:29 +0300
From: Diomidis Spinellis <dds@aueb.gr>
Organization: Athens University of Economics and Business
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060130 SeaMonkey/1.0
MIME-Version: 1.0
Newsgroups: comp.lang.java.programmer
Subject: Re: when is it necessary to use suffix of literals?
References: <1144844837.328276.120720@i39g2000cwa.googlegroups.com>
In-Reply-To: <1144844837.328276.120720@i39g2000cwa.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
xian_hong2046@hotmail.com wrote:
> Hello,
> 
> I'm a bit confused about when to use trailing characters such as "L",
> "F" and "D" for literals.  For example, if I have a double, then
> naturally I'll have something like:
> 
> double j = 42;
> 
> Then why would I bother to use
> 
> int j = 42D;
> 
> Similarly for other trailing characters.

You use the trailing suffixes when you want to force a calculation to be 
performed with the corresponding data type.  For example:

// Will print 0, because the result overflows a 32-bit int.
	System.out.println(0x80000000 * 0x100);

// Will evaluate the result as a long and print 549755813888	
		System.out.println(0x80000000L * 0x100);

-- 
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality?cljp



Newsgroup comp.lang.java.programmer contents
Newsgroup list
Diomidis Spinellis home page

Creative Commons License Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.