Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Conversion and coercion in Java

The accuracy and conversion of primitive arithmetic variables in Java is something that I'm questioned on regularly.

Ideally in Java, you'll perform arithmetic on two pieces of data of an identical type, and the result returned will be of the same type. However, if you perform a calculation on two different types the "least accurate" will be coerced up to the accuracy of the second. For example, integer is coerced to float which in turn may be coerced to a double.

What does this mean?







CalculationtypesResult type Stored in variable
of type
a = 5.0/2D/IDD
b = 5/2.0FI/FFF or D
c = 5/2I/III L F or D
d = 5/2.0I/DDD

I - int
L - long
F - float
D - double

Note that a constant such as 2.0 id double, whereas 2.0f or 2.0F is float.

If you wish to save a result into a "less accurate" variable, you may do so by casting it. For example
a = (float) (5/2.0)
will convert an integer up to a double for the division through coersion and will then cast it back to a float before saving it in the variable a

Select here for a download of my "variables in Java" training notes released under an open training notes license. Further examples of simple variable use in Java can be found in our modules resource centre.
(written 2004-11-22, updated 2006-06-05)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J703 - Java - Variables
  [4345] Incrementing a variable in Java - Pre and Post Increment - (2014-12-08)
  [4324] Learning to program - variables and constants - (2014-11-22)
  [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
  [3365] Turning bright delegates into bright and knowledgable ones - (2011-07-21)
  [3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
  [3041] Java - basic rules for arithmetic, variables and conversion - (2010-11-10)
  [3038] Setting up individual variables, and arrays, in Java - some commented examples - (2010-11-09)
  [2153] Class Loading and Variable Conversion in Java - (2009-05-02)
  [2148] Variable scope in Java Servlets and other web applications - (2009-05-01)
  [1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)


Back to
Feedback shows the tip of an iceberg
Previous and next
or
Horse's mouth home
Forward to
Technical Weekend / Geekmas
Some other Articles
Thanksgiving dinner
Spelling and grammar
Trawling our site to prevent student copying
Technical Weekend / Geekmas
Conversion and coercion in Java
Feedback shows the tip of an iceberg
Staff theft
PHP v Java
Short underground journeys and a PHP book
Passing arrays to procs in Tcl
4350 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/127_Conv ... -Java.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat