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

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


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Notes

Integers

[an error occurred while processing this directive]

Integers are whole numbers, for example, -35, 0, 2048, .... Integers are represented in binary inside the computer, and in decimal in Java source programs. Java automatically converts decimal numbers you write in your source program into binary numbers internally.

Four (or five) kinds of primtive integers and two integer classes.

Primitive types. The are four types of integers in Java: byte, short, int, long. The most common is int. All integers are stored in signed, two's-complement, format.

char! Technically, char is an unsigned integer type altho it is almost exclusively used to store characters. Making it integer is largely because of Java's legacy from C++. Don't use char for integers unless you are sure of what you're doing.

Classes. In addition to the primitive types, there are two classes used for integers.

  • Integer - Primarily useful for utility methods and to put in the Collections data structure classes.
  • BigInteger - Used where unbounded arithmetic is important.

How Java stores integers in memory

Java stores all integers in memory as binary numbers.

typeSizeRange
namebytesbitsminimummaximum
byte 18 -128+127
short216-32,768+32,767
int 432-2,147,483,648+2,147,483,647
long 864-9,223,372,036,854,775,808+9,223,372,036,854,775,807

How to write integer literals

Here is how to write decimal integer literals (constants).

  • int literals are written in the usual decimal notation, like 34 or -222.
  • long literals are written by adding an L (or lowercase l altho this is almost impossible to distinguish from the digit 1), eg, 34L or -222L.
  • There is no way to write a literal byte or short, altho sometimes Java will automatically cast an int literal to the appropriate type.

Hexadecimal literals

You can write an int in hexadecimal by prefixing the hexadecimal number with the digit zero followed by the letter x, "0x" or "0X". The hexadecimal digits are 0-9 and the letters a-f in upper- or lowercase.

   int i;
   i = 0x2A;  // assigns decimal 42 to i.

The shame of integer arithmetic

Operations may produce numbers which are too large (overflow) to be stored in an int. No error is caused in this case; the result is simply an incorrect number (one of the shames of modern computer arithmetic). Division by zero will cause an execution exception (ArithmeticException). Use BigInteger to prevent arithmetic overflow.

Webliography

Ask programming questions?

 

 

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

 

 
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.

[an error occurred while processing this directive]