Chapter 2
Chapter 2
Lecture 2
Data Types, Variables, Arithmetic
Operations, and Console Input/Output
2
Revision: Basic Java Program Structure
•There are 5 components that we usually include in a Java
program, which are:
[ Documentation Statement(s) ]
[ Package Statement ]
[ Import Statement(s) ]
public class className {
public static void main(String[] args) {
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 3
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Revision: Simple Java Application
Figure 1: Head First Java, 3rd Edition. (2022, May). Oreilly. https://www.oreilly.com/library/view/head-first-java/9781492091646/
4
Objectives
• Describe the Java data types used for simple
data
• Write Java statements to declare variables,
define named constants
• Write assignment statements, expressions
containing variables and constants
• Define strings of characters
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 5
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Data Types
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 6
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Data Types
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 7
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Primitive Vs. Non-Primitive/Class Types
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 8
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Primitive vs Non-Primitive
Primitive Non-Primitive
Primitive types are predefined in Non-primitive types are created by
Java programmers (except String)
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 9
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Java's Primitive Types
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 10
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Examples of Primitive Values
•Integer types
0 -1 365 12000
•Floating-point types
0.99 -22.8 3.14159 5.0
•Character type
'a' 'A' '#' ' '
•Boolean type
true false
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 11
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Java’s Non-Primitive Type: String
• String is a class!
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 12
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Identifiers
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 13
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Java Identifiers
• An identifier is the technical term for a name in a programming
language, such as the name of a variable.
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 14
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Java Identifiers
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 15
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Keywords or Reserved Words
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 16
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Variables
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 17
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Variables
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 18
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Variables
variable name
datatype int sum = 0; variable value
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 19
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Naming Variables
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 20
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Declaring Variables
• In Java, a variable must be declared before it is used.
• Syntax
datatype variable_1, variable_2, …;
• Examples
int styleChoice, numberOfChecks;
double balance, interestRate;
char jointOrIndividual;
• A variable's type determines what kinds of values it can hold (int,
double, char, etc.).
• The computer must know the type of a variable so it knows how to
store and retrieve the value of the variable from the memory.
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 21
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Where to Declare Variables
•Declare a variable
•Just before it is used or
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 22
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Declaring Variable of Type String
• Syntax
String objectName;
Note:
OR • String( ) is called a
String objectName = new String(); constructor which is a type of
methods that is used to create
and initialize objects of type
String
• Example: • new is a Java operator when it
String firstName; is used with a constructor, it
returns a reference to the newly
//firstName has an initial value created object in our example is
greeting
//equals to null
String lastName = new String();
//lastName is an empty String
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 23
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Initializing Variables
• A variable that has been declared, but no yet given a value is said
to be uninitialized.
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 24
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Initializing Variables
•Syntax
datatype variable_1 = expression_1, variable_2 =
expression_2, …;
•Examples:
int count = 0;
char grade = 'A';
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 25
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Initializing Variables of Type String
• Syntax
String objectName= "string";
OR
String objectName = new String("string");
• Example
String greeting = "Hello!";
or
String greeting = new String("Hello!");
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 26
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Assignment Statements
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 27
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Assignment Statements
•Syntax
variable = value or expression;
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 28
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
A Simple Java Program
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 29
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Assignment Evaluation
•The expression on the right-hand side of the assignment operator
(=) is evaluated first.
•The result is used to set the value of the variable on the left-hand
side of the assignment operator.
•Examples
score = numberOfCards + handicap;
eggsPerBasket = eggsPerBasket - 2;
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 30
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Memory Diagram
• Memory diagrams are used to show the state of
the program Memory diagram of the
variables a and b
• Example:
a = 5
int a = 5;
int b = a; // a and b are now equal
b = 5
a = 3; // a and b are no longer equal
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 32
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Assignment Compatibilities
•A value of one type can be assigned to a variable of any type
further to the right but not to a variable of any type further to
the left.
byte --> short --> int --> long --> float --> double
Figure 2: Head First Java, 3rd Edition. (2022, May). Oreilly. https://www.oreilly.com/library/view/head-first-java/9781492091646/
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 33
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Type Casting
• A type cast temporarily changes the value of a variable from the declared
type to some other type.
• Syntax:
(Type_Name)Expression ;
• Example:
double distance;
distance = 9.0;
int points;
points = (int)distance;
• Illegal without (int)
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 34
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Type Casting
•You can assign a value of type char to a variable of type
int
•Example:
char symbol = '7';
System.out.println((int)symbol);
//it displays 55!
//which is the real value of symbol 7 in ASCII
code
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 35
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Constants
•Literal expressions such as 2, 3.7, or 'y' are called
constants.
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 36
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Constants
• Constant’s name is an identifier
• Hence, all the identifier’s rules are applied to constants naming
• Syntax
public static final Type VariableName = Constant;
• Example:
public static final double PI = 3.14159;
public static final String MOTTO = "The
customer is always right.";
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 38
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Variables VS Constant
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 39
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Named Constants Example
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 40
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Named Constants Example
Sample
Screen
Output
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 41
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Suppose we have int i,j,k;
Which of the following assignment statements is
incorrect?
A. i = j = k = 1;
B. i = 1; j = 1; k = 1;
C. i = 1 = j = 1 = k = 1;
42
If x is an int and y is a float,
which assignment statement is illegal
(wrong)?
A. y = x;
B. y = (float) x;
C. x = y;
D. x = (int) y;
43
Summary
• You have become familiar with Java primitive
types (numbers, characters, etc.).
• You have learned about assignment
statements and expressions.
• You have learned how to declare a variable of
String type
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 44
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Reading
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 45
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved