- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Integer Data Type and Range
Java defines these four integer types:
- byte
- short
- int
- long
All of these are signed, positive, and negative values.
Java doesn't support unsigned, positive-only integers. Lots other computer languages support both signed and unsigned integers.
Anyhow, Java's designers felt that unsigned integers were unnecessary. Specifically, they felt that the concept of unsigned was used largely to specify the behaviour of high-ordered bit, which defines the sign of an integer value. Java handles the significance of high-ordered bit differently, by adding a special "unsigned right shift" operator. Therefore, the requirement for an unsigned integer type was eliminated.
The width of an integer type should not be intended of as the amount of storage it consumes, but instead as the behaviour it defines for the variables and expressions of that data type. However, the Java run-time environment is always free to use whatever the size it wants, as long as the types behave as you declared them.
Integer Width and Range
The width and ranges of these integer data types vary widely, as shown here in this table :
Name | Width | Range | |
---|---|---|---|
long | 64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | -263 to 263 -1 |
int | 32 | -2,147,483,648 to 2,147,483,647 | -231 to 231 -1 |
short | 16 | -32,768 to 32,767 | -215 to 215 -1 |
byte | 8 | -128 to 127 | -27 to 27 -1 |
Now, lets take a look at each type of integer.
byte
byte is the smallest integer type. This is signed 8-bit data type which has a range from -128 to 127. Variables of type byte are especially useful when you are working with stream of data from network or file. They are also useful when you are working with raw binary data which may not be directly compatible with other built-in types of Java.
Byte variables are declared by use of the byte keyword. For instance, the following declares two byte variables called b and c :
byte b, c;
short
short is a signed 64-bit type. It has range from -32,768 to 32,767. It is the least-used type. Here are some examples of short variable declarations :
short s; short t;
int
As you know that the most used integer data type is int. It is a signed 32-bit type having range from -2,147,483,648 to 2,147,483,647.
In addition to other uses, int type variables are commonly employed to control the loops and to index arrays. Even though you might think that using a byte or short would be more efficient than using an int in situations in which the larger range of an int data type isn't needed, this may not be the case. The reason is that when byte and short values are used in an expression, then they are promoted to int when the expression is evaluated. (You will learn about Type Promotion in two other chapters i.e., automatic type promotion and type promotion rules ). Therefore, int is the best choice when an integer is needed.
long
long is a singed 64-bit type and is useful for those occasions where an int data type is not large enough to hold the desirable value. The range of a long is quite large. This makes it helpful when big, whole numbers are needed. For instance, here is a program that computes the number of miles, light will travel in a given number of days :
/* Java Program Example - Java Integer Data Types * Compute distance light travels using long variables */ public class JavaProgram { public static void main(String args[]) { int lightspeed; long days; long seconds; long distance; /* approximate speed of light in miles per second */ lightspeed = 186000; days = 1000; // specify number of days here seconds = days * 24 * 60 * 60; // convert to seconds distance = lightspeed * seconds; // compute distance System.out.print("In " +days); System.out.print(" days ligh will travel about " + distance + " miles."); } }
When the above Java program is compile and run, it will produce the following output :
Clearly, the result could not have been held in an int variable.
« Previous Tutorial Next Tutorial »