Page 60 - iprime_V2.2_class8
P. 60
Data Types
A data type defines the size and type of value that a variable can store. Java provides two types
of data types: primitive and non-primitive.
Primitive Data Types
Java provides eight basic data types, as shown in the following table:
Size
Data Type Minimum Value Maximum Value
(in bits)
byte 8 -128 127
short 16 –32768 32767
int 32 –2147483648 2147483647
long 64 –9223372036854775808 9223372036854775807
char 16 0 65535
float 32 1.4E -45 3.4028235E38
double 64 4.9E-324 1.7976931348623157E308
boolean 1 True or False
Non-Primitive Data Types
The non-primitive data types are derived from primitive data types. Non-primitive data types are
also known as reference data types. Some of the examples are class, interface, and array.
Literals
A literal denotes a constant value. Java has various types of literals. They are as follows:
Character Literal: A character literal is enclosed in single quotes and has exactly one character.
For example, ‘a’ is a character literal that means the letter a.
String Literal: A string literal is enclosed in double quotes. For example, “Hello World.”
Integer Literal: An integer literal is any number without a fraction. For example, 10 and 012
Floating-Point Literal: Any number with decimal points, like 3.12, is treated as a
floating-point literal.
Boolean Literal: Boolean literals can have either true or false values. They do not correspond
to 0 or 1 values as in C/C++.
Variables
Variables are memory locations used to store values. When a variable is created, space is
allocated to it in the memory. This memory space is identified by the name that we give to the
variable. It is easy to create a variable in Java.
Declaring a Variable
In Java, a variable needs to be declared before use. Declaring a variable involves two steps: giving
the variable a name and stating what type of data is to be stored in the variable.
58 iPRIME (Ver. 2.2)–VIII

