Chapter 02
Chapter 02
Accessing Variables
Literals
<modifier> <dataType>
<variableName> = <initialValue>;
int id;
Once you declare a variable, you can access it
by referring to it by its name:
x = y;
Variables
can be classified into three
categories:
• Local variables
• Instance variables
• Static variables
A literal is a value assigned to a variable in the
source code
int id = 10;
The boolean Literals: true or false
The char Literals: ‘L’, ‘\u4567’, ‘\n’
The Integral Literals: 43, 43L, 053, 0x2b
The Floating-Point Literals: 12.33,
1.25E+8, 1.2534f
Onlythe instance variables acquire the default
values if not explicitly initialized
Data-Related Concepts
Working with Primitive Data Types
Declaring and Initializing Primitive Variables
Working with Nonprimitive Data Types
Understanding Operations on Data
Arithmetic Operators
Relational Operators
Logical Operators
Using Assignment Operators
Advanced Operators
Equality of Two Objects or Two Primitives
All non-primitive data types in Java are objects
You create an object by instantiating a class
When you declare a variable of a non-primitive
data type, you actually declare a variable that
is a reference - reference variable / object
reference - to the memory where an object
lives
Objects
Arrays
Example:
int[] scores;
int scores [];
Student[] students;
create an array with the new operator
An array of primitives is created and assigned
to an already declared array variable:
scores = new int[3];
Anarray of a non-primitive data type is
created and assigned to an already declared
array variable:
students = new Student[3];
Each element of an array needs to be assigned
a value (primitive type / object reference):
scores[0] = 75;
scores[1] = 80;
scores[2] = 100;
students[0] = new Student();
students[1] = new Student();
students[2] = new Student();
use enums any time you need a fixed set of
constants such as days of the week
define an enum variable in two steps:
1. Define the enum type with a set of named values.
2. Define a variable to hold one of those values.
Example:
enum AllowedCreditCard {VISA,
MASTER_CARD, AMERICAN_EXPRESS};
AllowedCreditCard visa =
AllowedCreditCard.VISA;
Data-Related Concepts
Working with Primitive Data Types
Declaring and Initializing Primitive Variables
Working with Nonprimitive Data Types
Understanding Operations on Data
Arithmetic Operators
Relational Operators
Logical Operators
Using Assignment Operators
Advanced Operators
Equality of Two Objects or Two Primitives
Unary operators: Require only one operand.
For example, ++ increments the value of its
operand by one.
Binary operators: Require two operands. For
example, + adds the values of its two
operands.
Ternary operators: Operate on three operands.
The Java programming language has one
ternary operator, ?:
Data-Related Concepts
Working with Primitive Data Types
Declaring and Initializing Primitive Variables
Working with Nonprimitive Data Types
Understanding Operations on Data
Arithmetic Operators
Relational Operators
Logical Operators
Using Assignment Operators
Advanced Operators
Equality of Two Objects or Two Primitives
The Sign Unary Operators: + and –
The Increment and Decrement Operators: ++
and –
operate on all primitive numeric types and the type
char
The result of dividing an integer by another integer will
be an integer
In case of integer types, division by zero would
generate an ArithmeticException at execution
time
Division by zero in case of float and double types would
generate IPOSITIVE_INFINITY or
NEGATIVE_INFINITY
The square root of a negative number of float or double
type would generate an NaN (Not a Number) value:
Float.NaN, and Double.NaN.
gives the value that is the remainder of a
division
The sign of the result is always the sign of the
first (from the left) operand
perform arithmetic addition and subtraction
If the result overflows, the truncation of bits
happens the same way as in multiplication
The + operator is overloaded in the Java
language to concatenate strings
Data-Related Concepts
Working with Primitive Data Types
Declaring and Initializing Primitive Variables
Working with Nonprimitive Data Types
Understanding Operations on Data
Arithmetic Operators
Relational Operators
Logical Operators
Using Assignment Operators
Advanced Operators
Equality of Two Objects or Two Primitives
alsocalled a comparison operator, compares
the values of two operands and returns a
boolean value: true or false
Data-Related Concepts
Working with Primitive Data Types
Declaring and Initializing Primitive Variables
Working with Nonprimitive Data Types
Understanding Operations on Data
Arithmetic Operators
Relational Operators
Logical Operators
Using Assignment Operators
Advanced Operators
Equality of Two Objects or Two Primitives
used to combine more than one condition that
may be true or false
deal with connecting the boolean values
operate at bit level
two kinds of logical operators:
• bitwise logical operators
• short-circuit logical operators
manipulate the bits of an integer (byte, short,
char, int, long) value
operate on the boolean types
The outcome of these operators is a boolean
Data-Related Concepts
Working with Primitive Data Types
Declaring and Initializing Primitive Variables
Working with Nonprimitive Data Types
Understanding Operations on Data
Arithmetic Operators
Relational Operators
Logical Operators
Using Assignment Operators
Advanced Operators
Equality of Two Objects or Two Primitives
used to set
(or reset)
the value of
a variable:
x = 7;
shortcut
assignment
operators:
involves binary operation between two
operands of different types or of types
narrower in size than int
the compiler may convert the type of one
operand to the type of the other operand, or
the types of both operands to entirely
different types
Arithmetic promotion is performed before any
calculation is done
If both the operands are of a type narrower than
int (that is byte, short, or char), then both of
them are promoted to type int before the
calculation is performed.
If one of the operands is of type double, then
the other operand is converted to double as
well.
If none of the operands is of type double, and
one of the operands is of type float, then the
other operand is converted to type float as well.
If none of the operands is of type double or
float, and one of the operands is of type
long, then the other operand is converted to
type long as well.
If none of the operands is of type double,
float, or long, then both the operands are
converted to type int, if they already are not.
Data-Related Concepts
Working with Primitive Data Types
Declaring and Initializing Primitive Variables
Working with Nonprimitive Data Types
Understanding Operations on Data
Arithmetic Operators
Relational Operators
Logical Operators
Using Assignment Operators
Advanced Operators
Equality of Two Objects or Two Primitives
The cast operator: (<type>) explicitly converts
a value to the specified type
byte z = (byte) (x/y);
The new operator: instantiate a class and to
create an array
The instanceof operator: determines if a
given object is of the type of a specific class
<op1> instanceof <op2>
Data-Related Concepts
Working with Primitive Data Types
Declaring and Initializing Primitive Variables
Working with Nonprimitive Data Types
Understanding Operations on Data
Arithmetic Operators
Relational Operators
Logical Operators
Using Assignment Operators
Advanced Operators
Equality of Two Objects or Two Primitives
Three kinds of elements that can be compared
to test the equality:
Primitive variables:
• hold the same value
• can be tested with the == operator
Reference variables:
• can be compared by using the == operator
• hold the same value
Objects:
• tested with the equals() method of the Object class.