Java
One step Ahead
Anita Seth &
B.L. Juneja
© Oxford University Press 2018. All rights reserved.
Chapter 3
Data Types, Variables and
Operators
© Oxford University Press 2018. All rights reserved.
Data Types in Java
Java defines eight primitive (basic) types of data
1. Integral types—byte, short, int, long
2. Floating point types—float, double
3. Character type—char
4. Boolean type—boolean
• There is a non-data type called void and no data can be of type void.
• This type is used for methods that do not return any value.
© Oxford University Press 2018. All rights reserved.
Declaration of Variables
• A variable is declared by first declaring its type followed by its
identifier or name.
• The name or identifier should not be a keyword of Java language.
• It can be of any length and may consist of characters and digits.
© Oxford University Press 2018. All rights reserved.
Non-primitive Types
These are the class and interface types. The name of a class or
interface is the name of type. A class object is declared as
© Oxford University Press 2018. All rights reserved.
Data Types
Integers
Integers are whole numbers, that is, they represent numbers that do
not have a fractional part.
Examples of such numbers are as follows:
1. Number of passengers in a bus
2. Number of runs made in a cricket match
3. Numbers of events
4. Number of eggs in a basket
In Java, these numbers may be positive or negative.
© Oxford University Press 2018. All rights reserved.
Data Types
The integers can be declared in four types according to the size of
memory allocated:
1. byte
2. Short
3. int
4. long
More the memory allocated, the higher is the range of values that can
be stored.
© Oxford University Press 2018. All rights reserved.
Data Types
Integers
© Oxford University Press 2018. All rights reserved.
Program Ex-1
Integers
© Oxford University Press 2018. All rights reserved.
Characters
• A variable may have value in terms of a character in which the
type of variable is char.
• These characters represent integer values.
• Java supports Unicode for the representation of characters.
• Unicode supports all the character sets of all the major languages
of the world.
• The initial version of Unicode allocated 2 bytes for storing
characters.
• The range of values for characters in the initial version of Unicode
comprised from ‘\u0000’ to ‘\uffff ’ that is from 0 to 65535 both
end values inclusive.
© Oxford University Press 2018. All rights reserved.
Program Ex-2
© Oxford University Press 2018. All rights reserved.
Floating Point Numbers
• The numbers that are not whole numbers, or those that have
fractional part, Examples are 3.141, 476.6, and so on.
• Java supports two types of such numbers.
Float: This type is used for single-precision decimal floating point
numbers, that is, 7 digits after the decimal point. These numbers
are stored on 4 bytes.
© Oxford University Press 2018. All rights reserved.
Floating Point Numbers
• Double This type is used for double-precision decimal floating
point numbers, that is, 15 digits after the decimal point.
© Oxford University Press 2018. All rights reserved.
Floating Point Numbers
Program Ex-3
• Double This type is used for double-
precision decimal floating point
numbers, that is, 15 digits after the
decimal point.
© Oxford University Press 2018. All rights reserved.
boolean Type Data
• For dealing with logical statements, variable of type boolean is
supported.
• The value of boolean type variable is either true or false.
• The boolean type variables are unsigned as similar to char type
variables.
• The variable may be declared as follows :
boolean a;
a = x > y;
© Oxford University Press 2018. All rights reserved.
boolean Type Data Program
© Oxford University Press 2018. All rights reserved.
Type Casting
• If an expression has more than one type of a variable, the different types
have to be changed to conform to a single type.
• The conversion of types of data is called type casting or type conversion.
• Some type castings are done automatically by a compiler, while others
have to be done by the programmer.
• Examples :
byte n = 8;
short m = 67 + n;
n is promoted to match type short.
Similarly, in the following code,
int P = m + n;
In
© Oxford University Press 2018. All rights reserved.
Type Casting
• The explicit type casting is carried out by the following code:
type variable = (new_type) variable;
• It is illustrated by the following code lines:
double D = 6.865;
int A = (int) D;
• In such a conversion, there is loss of data, and therefore, the
value.
© Oxford University Press 2018. All rights reserved.
Type Casting- Program EX
© Oxford University Press 2018. All rights reserved.
Literal Constants
• Each character is a constant value.
• An array of characters or a string also represents a constant value.
• The digits in decimal system, octal system, or hexadecimal system
represent constant values.
• These are all called literal constants.
• The char literals are enclosed in single quotes (‘ ’). The examples
‘A’ and ‘B’.
© Oxford University Press 2018. All rights reserved.
Symbolic Constants
• A symbolic constant is a variable whose value does not change
throughout the program.
• Some of the examples include PI, NORTH, EAST etc.
© Oxford University Press 2018. All rights reserved.
Symbolic Constants-Program EX
• A symbolic constant is a variable whose
value does not change throughout the
© Oxford University Press 2018. All rights reserved.
Static Variables and Methods
• The variables in a class may be modified by modifier static.
• The non-static variables declared in a class are instance variables.
• Each object of the class keeps a copy of the values of these
variables.
• The static variables are class variables.
• The static variables are accessed through class reference, whereas
the instance variables are accessed through class object
reference.
© Oxford University Press 2018. All rights reserved.
Static Variables and Methods
© Oxford University Press 2018. All rights reserved.
Mathematical Functions
• The class Math of the package [Link]. defines several
mathematical methods. functions.
• [Link] package is imported by default in every Java program,
there is no need to place the line import java. lang;
• All the methods of class Math are declared static, that is, it can be
called even.
© Oxford University Press 2018. All rights reserved.
Mathematical Functions
© Oxford University Press 2018. All rights reserved.
Attribute Final
• The value of a variable declared final cannot be changed in the
program.
• It makes the variable a constant.
• A few examples of declarations are as:
final double PI = 3.14159;
final int M = 900;
final double X = 7.5643;
© Oxford University Press 2018. All rights reserved.
Introduction to Operators
1. Arithmetic operators
2. Compound assignment operators
3. Increment and decrement operators
4. Relational operators
5. Boolean logical operators
6. Bitwise logical operators
7. Shift operators
8. Special operators
• If an operator takes only one operand for its operation, it is called
unary operator.
• If it takes two operands, it is called binary operator.
• If it takes three operands, it is called ternary operator.
© Oxford University Press 2018. All rights reserved.
Precedence and Associativity of Operators
• Rules that determine the order of operations when operators of
equal precedence appear within the same expression.
• In an expression that has several operators, the precedence level
determines the operator’s turn for operation.
• In an expression having several operators, the computer first
execute the operator with the highest precedence level, and
then, it will take the operator which is next (lower) in the
precedence level and so on.
• BODMAS is one of the easiest ways to remember the order of
precedence rule with respect to the basic arithmetic operators.
© Oxford University Press 2018. All rights reserved.
Precedence and Associativity of Operators
• According to BODMAS rule, operations in any given expression
should precede in the following order :
1. Brackets
2. Order
3. Division
4. Multiplication
5. Addition
6. Subtraction
© Oxford University Press 2018. All rights reserved.
Precedence and Associativity of Operators
• If the expression contains several operators with the same
precedence level, the expression is evaluated according to its
associativity.
• For example, in
Z = 6*4%5;
Both the operators * and % have same precedence level.
© Oxford University Press 2018. All rights reserved.
Assignment Operator ( = )
• The associativity of assignment operator is from right to left.
• On the left of =, we write the name of the variable to which a
value is to be assigned, and on its right, we write the value to be
assigned.
• The name or identifier on the left is also called l-value and the
value on the right of = is called r-value.
© Oxford University Press 2018. All rights reserved.
Basic Arithmetic Operators
© Oxford University Press 2018. All rights reserved.
Program Example
© Oxford University Press 2018. All rights reserved.
Combinational Arithmetic Operators
• Combinational assignment statements in Java are similar to those
used in C or C++.
• In a compound assignment operator, the operations by an
operator and assignment are combined in a single operator. As for
e.g.
int n = 8, m = 10;
n = n + m;
© Oxford University Press 2018. All rights reserved.
Program Example
© Oxford University Press 2018. All rights reserved.
Increment (++) and Decrement (−−) Operators
© Oxford University Press 2018. All rights reserved.
Program Example
© Oxford University Press 2018. All rights reserved.
Ternary Operator
• Ternary operator is also called conditional operator.
• In Java, there is only one ternary operator described by token (?:).
• It takes three operands for its operation.
expression1? expression2 : expression3
If expression1 is true, then expression2 is executed; otherwise,
expression3 is executed.
© Oxford University Press 2018. All rights reserved.
Relational Operator
© Oxford University Press 2018. All rights reserved.
Program Example
© Oxford University Press 2018. All rights reserved.
Boolean Logical Operators
© Oxford University Press 2018. All rights reserved.
Program Example
© Oxford University Press 2018. All rights reserved.
Short-circuit Logical Operators
• These are the secondary versions of Boolean AND and OR
operators.
• The short-circuit form of OR operator is represented by symbol ||
and that of AND operator is represented by symbol &&.
© Oxford University Press 2018. All rights reserved.
Combinational Boolean Operators
© Oxford University Press 2018. All rights reserved.
Program Example
© Oxford University Press 2018. All rights reserved.
Bitwise Logical Operators
• These operators are applied to individual bits of numbers.
• The bitwise operators are OR, AND, XOR, and bitwise
compliment/bitwise NOT operator.
© Oxford University Press 2018. All rights reserved.
Bitwise Logical Operators
© Oxford University Press 2018. All rights reserved.
Program Example
© Oxford University Press 2018. All rights reserved.
Application Program
© Oxford University Press 2018. All rights reserved.
Program Example
© Oxford University Press 2018. All rights reserved.