Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Integers

JavaFAQ Home » Mathematics Go to all tips in Mathematics


Bookmark and Share

Java: Integers

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 kinds of integers

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.

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

How Java stores integers in memory

Java stores all integers in memory as binary numbers.
type Size Range
name bytes bits minimum maximum
byte 1 8 -128 +127
short 2 16 -32,768 +32,767
int 4 32 -2,147,483,648 +2,147,483,647
long 8 64 -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.

Errors

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).
 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy