0% found this document useful (0 votes)
5 views

Chapter 2

Uploaded by

6w6j8swbpt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chapter 2

Uploaded by

6w6j8swbpt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

College of Computing

Department of Computer Science and


Artificial Intelligence

CS1211: Computer Programming

2nd Trimester : 2023/24

Lecture 2
Data Types, Variables, Arithmetic
Operations, and Console Input/Output

Original Slides: Dr.Abdulbaset Gaddah


Modified by Dr. Malak AlJabri and Dr. Areej Althobiti 1
Revision

✔ Programs and Programming Languages.


✔ Java Programming Language.
✔ Programming Basics.

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) {

[ Body of Main Function ]


}
}

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

• A data type specifies a set of values and their operations.

• Java has two main kinds of 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

• A primitive type is used for simple, non-decomposable values such


as an individual number or individual character.
• Example: int, double, and char are primitive types.

• A non-primitive type are used to store a group of values


• They have both data and method
• Example: String, array, and user-defined classes

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)

Primitive types have a value Non-primitive types could be null

Primitive types have NO methods Non-primitive types have mathods

Primitive types start with a Non-primitive types start with an


lowercase letter uppercase letter

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!

• In Java, String class could be found in java.lang package


• It is always imported in your Java classes, so no need for
an import statement

• A String variable’s value is a sequence of characters treated as a


single item (string)

• Also, String objects contain subroutines/functions


• Example: length ( ) to know how many characters in a given
string

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.

• Identifiers may contain only


• Letters
• Digits (0 through 9)
• The underscore character (_)
• The first character cannot be a digit.

• You may “punctuate” multiword names by using uppercase


letters
• Example: inputStream, YourClass, CarWash

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

•Identifiers may not contain any spaces, dots (.), asterisks


(*), or other characters:
7-11 oracle.com util.* (not allowed)

•Identifiers can be arbitrarily long.


•There is no limit to the length of an identifier

•Java is case-sensitive, which means stuff, Stuff, and


STUFF are different 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

•Words such as data primitive type and other words in Java


such as if are called keywords or reserved words and have
special, predefined meanings in Java
•Cannot be used as identifiers!

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

•Variables store data such as numbers and letters.


•They are implemented as memory locations.

•The data stored by a variable is called its value.


•The value is stored in the memory location.

•Its value can be changed!

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

• When the variable is given a value, the value is encoded as a string of


0s and 1s and is placed in the variable’s memory location.

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

• Variable’s name is an identifier


• Hence, all the identifier’s rules are applied to variables naming Choose

• Variable name should be meaningful such as count or speed, but


not c or s.

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

•At the beginning of the section of your program that is


enclosed in {}.
public static void main(String[] args)
{ /* declare variables here */
. . .
}

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.

• Uninitialized primitive variables may have a default value.

• It's good practice not to rely on a default value.

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

•To protect against an uninitialized variable (and to


keep the compiler happy), assign a value at the time
the variable is declared.

•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

•An assignment statement is used to assign a value


to a variable.
answer = 42;

•The "equal sign" is called the assignment operator.

•We say, "The variable named answer is assigned a


value of 42," or more simply, "answer is assigned
42."

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;

• where expression or value can be another variable, a literal or


constant (such as a number), or something more complicated which
combines variables and literals using operators
(such as + and -)
• Example
amount = 3.99;
firstInitial = 'W';
score = numberOfCards + handicap;
eggsPerBasket = eggsPerBasket - 2;
name = “Ali”;

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

• The third line changes the value of a, but it does


not change the value of b, so they are no longer a = 3
equal!

• The variables in a program and their current


values make up the program’s state.
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 31
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Assignment Compatibilities

•Java is said to be strongly typed.


•You can't, for example, assign a floating point value to a
variable declared to store an integer.

•Sometimes conversions between numbers are


possible.

•Example: double doubleVariable = 7;

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.

•Constants do not change their value, but they can be used


in an assignment statement to change the value of a
variable of type char.

•Java provides mechanism to …


• Define a constant
• Initialize it
• Fix the value so it cannot be changed

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

• The convention for naming constants is to use all uppercase letters,


with an underscore symbol (_) between words.
• Example: public static final int DAYS_PER_WEEK = 7;

• Integer constants can be preceded by a + or - sign, but cannot


contain commas.

• Floating-point constants can be written


• With digits after a decimal point or
• Using e notation
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 37
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Declaring Constants
• Declare constants near the beginning of the program

• 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

•You may want to use constants when you need to


use an explicit value that doesn’t change over the
course of the program.

•While variables are placeholders for data that can


change during execution.

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

You might also like