1.5 Primitive Data Types
In previous section, we’ve discussed that all variables must first be declared before they can be used. This involves stating the variable's type and name :
int rollno = 1;
This statement tells your program that a variable named "rollno" exists, holds numerical data, and has an initial value of "1". A variable's data type determines the values it may contain, plus the operations that may be performed on it.
There are eight basic data types for the storage of integers, floating-point numbers, characters, and Boolean values. These often are called primitive types because they are built in parts of the Java language.
Integer Types
There are four data types you can use to store integers.
Type |
Size
|
Values That Can Be Stored
|
byte |
8 bits
|
-128 to 127
|
short |
16 bits
|
-32,768 to 32,767
|
int |
32 bits
|
-2,147,483,648 to 2,147,483,647
|
long |
64 bits
|
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
|
Floating-point Types
Integer can store only whole numbers, another type of number that can be stored is a floating-point number. Floating-point numbers are numbers with a decimal point.
In Java there are two data types that can represent floating-point numbers. They are float and double. The float data type is a single precision data type. It can store a floating-point number with 7 digits of accuracy.
The double data type is a double precision data type. It can store a floating-point number with 15 digits of accuracy. A float variable occupies 4 bytes of memory, whereas a double variable uses 8 bytes.
Character type
The
char
type is used for individual characters, such as letters, numbers, punctuation, and other symbols.Booleans
boolean
data type holds either true
or false
in Java.
No comments:
Post a Comment