Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java Notes

Numbers

[an error occurred while processing this directive]

Two kinds of numbers. There are basically two kinds of numbers in Java and most other programming languages: binary integers (most commonly using the type int) and binary floating-point numbers (most commonly using the type double). Although these numbers are stored in the computer as binary numbers, you will usually use decimal numbers in your Java source program, and the Java compiler translates them to the equivalent binary form.

Floating-point numbers can have a fractional part and we usually write them with a decimal point, for example, 3.14159265, 0.01, -4.0.

Integers are "whole numbers" and can not have a fractional part. for example, 3, 0, -4.

Typical Issues

  • Deciding which to choose: integer or floating-point.
  • Be sure the range (minimum to maximum value) is sufficient.
  • Be sure the accuracy (number of digits of precision) is sufficient in floating-point.
  • Conversions between integer, floating-point, and String.
  • Consequences of integer and floating-point arithmetic.

Numbers in Java

Topics to eventually be added

  • BigInteger - Unbounded integer range.
  • BigDecimal - Decimal arithmetic.
  • IEEE-754 representation of floating-point numbers, including overflow, underflow, infinity, and NaN.
  • strictfp

Number FAQ

Q: Isn't it more efficient to use small types (like short) than int?
A: In principle small variables (those using fewer bytes) should be faster than large variables. So on the surface it would seem that you should use, for example, byte for integers that are in the range -128 to +127 because a byte integer requires only 1/4 the memory that an int does.

First, if you have only a small amount of data, as many programs do, the effect would be unmeasurable.

Second, because entire memory blocks are passed between the CPU and RAM there may not actually be any difference for small amounts of data! The exact way that memory is handled in the hardware is complex and often requires benchmarking (actual tests) to determine the speed of various options.

Large amounts of data are another matter, for example, when using arrays. If you're storing millions of numbers, their size has a definite effect on performance.

int and double are the types that the Java compiler likes for integer and floating-point constants and expressions, so you will find it much easier to use them. If you use other sizes of numbers, you will have to write a lot of cast operators, which is annoying and makes your code harder to read. I used to choose the smallest type that would work for my data for efficiency reasons, but have finally given up and just use int and double because of the convenience - except for large amounts of data.

Webliography

  • Check out William Kahan's homepage if you want to find out more about the dark corners of floating-point numbers and Java. www.cs.berkeley.edu/~wkahan/

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.