Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Every variable must have a data type. A variable's data type determines the values the variable can contain and the operations that can be performed on it. For example, in theMaxVariablesDemo
program, the declarationint largestInteger
declares thatlargestInteger
has an integer data type (int
). Integers can contain only integral values (both positive and negative). You can perform arithmetic operations, such as addition, on integer variables.The Java programming language has two categories of data types primitive and reference. A variable of primitive type contains a single value of the appropriate size and format for its type: a number, a character, or a boolean value (see the following figure). For example, an integer value is 32 bits of data in a format known as two's complement, the value of a
char
is 16 bits of data formatted as a Unicode character, and so on.A variable of primitive type contains a value of a particular size and format.
The following table lists, by keyword, all the primitive data types supported by the Java platform, their sizes and formats, and a brief description of each. The
MaxVariablesDemo
program declares one variable of each primitive type.
Primitive Data Types Keyword Description Size/Format Integers byte
Byte-length integer 8-bit two's complement short
Short integer 16-bit two's complement int
Integer 32-bit two's complement long
Long integer 64-bit two's complement Real numbers float
Single-precision floating point 32-bit IEEE 754 double
Double-precision floating point 64-bit IEEE 754 Other types char
A single character 16-bit Unicode character boolean
A boolean value ( true
orfalse
)true or false
Purity Tip: In other programming languages, the format and the size of primitive data types can depend on the system on which the program is running. In contrast, the Java programming language specifies the size and the format of its primitive data types. Hence, you don't have to worry about system dependencies.You can put a literal primitive value directly in your code. For example, if you need to assign the value
4
to an integer variable you can write this:The digitint anInt = 4;4
is a literal integer value. The following table gives some examples of literal values for various primitive types.
Examples of Literal Values Literal Value Data Type 178
int
8864L
long
37.266
double
37.266D
double
87.363F
float
26.77e3
double
'c'
char
true
boolean
false
boolean
Generally speaking, a series of digits with no decimal point is typed as an integer. You can specify a long integer by putting an
'L'
or'l'
after the number.'L'
is preferred because it cannot be confused with the digit'1'
. A series of digits with a decimal point is of typedouble
. You can specify afloat
by putting an'f'
or'F'
after the number. A literal character value is any single Unicode character between single quote marks. The two boolean literals are simplytrue
andfalse
.Arrays, classes, and interfaces are reference types. The value of a reference type variable, in contrast to that of a primitive type, is a reference to (an address of) the value or set of values represented by the variable (see the following figure). A reference is called a pointer, or a memory address in other languages. The Java programming language does not support the explicit use of addresses like other languages do; you need to use the variable's name instead.
A variable of reference type contains a reference to (an address of) an object or an array.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.