Data Types
Data Types
Data Types
Operators
The Primitive Types
The Primitive Types
• Java defines eight primitive types of data: byte, short, int, long, char,
float, double, and boolean.
• Java strictly specifies a range and behavior for each primitive type, which all
implementations of the Java Virtual Machine must support
• These can be put in four groups:
Integers This group includes byte, short, int, and long, which are for whole-
valued signed numbers.
Floating-point numbers This group includes float and double, which represent
numbers with fractional precision.
Characters This group includes char, which represents symbols in a character
set, like letters and numbers.
Boolean This group includes boolean, which is a special type for representing
true/false values.
Integers
• Java defines four integer types: byte, short, int, and long.
• byte b, c;
• short s;
• short t;
• int lightspeed;
• long days;
Example program
Floating-Point Types
• Floating-point numbers, also known as real numbers, are used when
evaluating expressions that require fractional precision.
• float hightemp, lowtemp;
• double pi, r, a;
• The type float specifies a single-precision value that uses 32 bits of storage.
• Double precision, as denoted by the double keyword, uses 64 bits to store a
value.
• When you need to maintain accuracy over many iterative calculations, or
are manipulating large-valued numbers, double is the best choice.
Example program
Characters
• In Java, the data type used to store
characters is char.
• In C/C++, char is 8 bits wide. This is not
the case in Java.
• Java uses Unicode to represent characters.
Unicode defines a fully international
character set that can represent all of the
characters found in all human languages.
• It is a unification of dozens of character
sets, such as Latin, Greek, Arabic,
Cyrillic, Hebrew, Katakana, Hangul, and
many more. For this purpose, it requires
16 bits.
• In Java, char is an unsigned 16-bit type
having a range of 0 to 65,536.
Booleans
• Java has a primitive
type, called boolean, for
logical values.
• It can have only one of
two possible values,
true or false.
• This is the type returned
by all relational
operators.
Variables
• The variable is the basic unit of storage in a Java program.
• A variable is defined by the combination of an identifier, a type, and
an optional initializer.
• In addition, all variables have a scope, which defines their visibility,
and a lifetime.
• Declaring a Variable
• type identifier [ = value][, identifier [= value] ...] ;
• The type is one of Java’s atomic types, or the name of a class or
interface.
• The identifier is the name of the variable.
Dynamic initialization of variables
The Scope and Lifetime of Variables
• Java allows variables to be declared
within any block.
• A block is begun with an opening
curly brace and ended by a closing
curly brace. A block defines a scope.
• A scope determines what objects are
visible to other parts of your
program.
• It also determines the lifetime of
those objects.
• Scopes can be nested.
• Variables are created when their
scope is entered, and destroyed
when their scope is left.
• The lifetime of a variable is
confined to its scope.
Cont..
Operators
Arithmetic Operators