0% found this document useful (0 votes)
51 views38 pages

Lesson 04 - Primitive Data Types I

The document discusses primitive data types and operations in Java programming. It covers variables and data types, declaring variables, assignment statements, expressions, and provides examples of writing simple Java programs to calculate mathematical equations and the area of a circle. The objectives are to understand variables, data types, naming conventions, operators, and writing Java code involving assignments, expressions, and basic calculations.

Uploaded by

Shvan Jaff
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
51 views38 pages

Lesson 04 - Primitive Data Types I

The document discusses primitive data types and operations in Java programming. It covers variables and data types, declaring variables, assignment statements, expressions, and provides examples of writing simple Java programs to calculate mathematical equations and the area of a circle. The objectives are to understand variables, data types, naming conventions, operators, and writing Java code involving assignments, expressions, and basic calculations.

Uploaded by

Shvan Jaff
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 38

1

Lesson 04: Primitive Data Types


and Operations I

SOLAF ALI HUSSAIN


University of Sulaimani
College of Science
Department of Computer

22 Dec. 2021 Problem Solving and Programming Concepts


Objectives
2

 To know about:
 Data and data types.
 Naming variables, constants, methods, and classes.
 Using of variables to store data.
 Using constants to store permanent data.
 Programs with assignment statements and expressions.
 Using Java operators to write expressions.
 Being familiar with Java documentation, programming
style.
 Naming conventions in java.
Fundamental Building Blocks of
3
Programs
 There are two basic aspects of programming:
 Data: To work with data, you need to understand
variables and types.
 Instructions: to work with instructions, you need to
understand control structures and subroutines.
Variables and Data Types (1)
4

 Variables are used to store data in a program.


 Variables represents a value stored in computer’s memory.
 A variable is just a memory location that has been given a
name.
 Variables are for representing data of a certain type.
Variables and Data Types (2)
5

 Type: A category or set of data values.


 The term data type refers to the type of data that can be
stored.
Java's Primitive Types and Strings
6
Type Description Size Examples
byte Integer 1 byte (up to 27 - 1)
short Integer 2 byte (up to 215 - 1)
(up to 231 - 1)
int Integer 4 byte
42, -3, 0, 926394
long Integer 8 byte (up to 263 – 1)
Primitive (up to 10308)
data types double Real number 8 byte
3.1, -0.25, 9.4e3
float Real number 4 byte
2 byte for
char Single text character Unicode 'a‘ , 'X‘ ,'?‘ , ‘4’, '\n'
characters
boolean Logical value 1 bit of memory ? true , false
Non- “program”,
Primitive
type
String
Sequence of
characters ? “Computer”, “science”,
“a”, “4”
Declaring Variables
7

 Every variable must be declared, before it can be used.


 The syntax for declaring a variable is:
datatype variableName;
Memory
 Examples:
o int mark; mark
o double radius;
radius
o double interestRate;
o char firstChar;
interestRate

firstChar
Assignment Statement
8

 After a variable is declared, you can assign a value to it


by using an assignment (=) statement.
 The syntax for assignment statements is as follows:
variable = expression;

mark 67
 Examples:
 int mark = 67; radius 4.4
 double radius = 4.4;
 float interestRate = 0.3; interestRate 0.3
 char firstChar = ‘a’;

firstChar a
Variable Type, Size and Value
9

long b = 4

int z = 4 4
short y = 4 4
byte x = 4
4
4 z
b

y
x
byte short int long
1 byte 2 byte 4 byte 8 byte
Expressions
10

 Expression: A computation involving values, variables,


and operators that together evaluates to a value.
 Examples:
 num = 1 + 4 * 5
 num = (7 + 2) * 6 / 3
Example 1: Writing a Simple Java Program (1)
11

 Write a program to calculate result of the following


equation:

 The algorithm for calculating the equation can be described as


follows:
Start
1. Give value of x.
2. Calculate the equation using value of x.
Give value of x
3. Display f.
4. End. Calculate

Output f

Stop
Example 1: Writing a Simple Java Program (2)
12

 Every java program must start with class keyword and have a
main method.

public class ComputeEquation {


public static void main(String[] args) {

// Step1: give value of x

// Step 2: Calculate the equation using value of x

// Step 3: Display f

}
}
Example 1: Writing a Simple Java Program (3)
13

 The program needs to read x.


 In order to store value of x, the program needs to declare a
variable and assign a value to x.

public class ComputeEquation {


public static void main(String[] args) {

// Step1: give value of x


int x = 7;// declare and assign x

// Step 2: Calculate the equation using value of x

// Step 3: Display f

}
}
Example 1: Writing a Simple Java Program (4)
14

 Calculate result using the equation.


 The result must be stored in the program, so we need a
variable for the result called f.

public class ComputeEquation {


public static void main(String[] args) {

// Step1: give value of x


int x = 7;

// Step 2: Calculate the equation using value of x


int f = 2 * x * x + 4 * x + 8;

// Step 3: Display f

}
}
Example 1: Writing a Simple Java Program (5)
15

 Now we can display the result.

public class ComputeEquation {


public static void main(String[] args) {

// Step1: give value of x


int x = 7;

// Step 2: Calculate the equation using value of x


int f = 2 * x * x + 4 * x + 8;

// Step 3: Display f
System.out.println(f);
}
}
Example 2 (1)
16

 Write a program for calculating area of circle.


 The algorithm for calculating area of circle can be
described as follows:
1. Read the circle’s radius.
2. Compute the area using the following formula:

3. Display the result.


Example 2 (2)
17

public class ComputeArea {


public static void main(String[] args) {
double radius;
double area;

radius = 20;

area = radius * radius * 3.14159;

System.out.println("The area for the circle of radius "


+ radius + " is " + area);
}
}
Example 2: Trace a Program Execution (1)
18

allocate memory
for radius
public class ComputeArea {
public static void main(String[] args) {
double radius; radius no value
double area;

radius = 20;

area = radius * radius * 3.14159;

System.out.println("The area for the circle of radius "


+ radius + " is " + area);
}
}
Example 2: Trace a Program Execution (2)
19

public class ComputeArea {


public static void main(String[] args) {
double radius;
radius no value
double area;
area no value
radius = 20;

area = radius * radius * 3.14159; allocate memory


for area
System.out.println("The area for the circle of radius "
+ radius + " is " + area);
}
}
Example 2: Trace a Program Execution (3)
20

Assign 20 to
public class ComputeArea { radius
public static void main(String[] args) {
double radius;
radius 20
double area;
area no value
radius = 20;

area = radius * radius * 3.14159;

System.out.println("The area for the circle of radius "


+ radius + " is " + area);
}
}
Example 2: Trace a Program Execution (4)
21

Compute area, and assign it


public class ComputeArea { to variable area
public static void main(String[] args) {
double radius; radius 20
double area;
area 1256.636
radius = 20;

area = radius * radius * 3.14159;

System.out.println("The area for the circle of radius "


+ radius + " is " + area);
}
}
Example 2: Trace a Program Execution (5)
22

Print the message in


public class ComputeArea { the console
public static void main(String[] args) {
double radius;
double area;

radius = 20;

area = radius * radius * 3.14159;

System.out.println(“Area of circle with radius "


+ radius + " is " + area);
}
}
Constants
23

 A constant represents permanent data that never


changes during the course of the program.
 The general syntax for declaring a constant:
final datatype CONSTANTNAME = VALUE;

 Examples:
final double PI = 3.14159;
final int SIZE = 3;
Arithmetic Operators (1)
24

 operator: Combines multiple values or expressions.


Arithmetic Operators (2)
25

 Examples:
 1 + 1 evaluates to 2
 20 % 3 evaluates to 2
 5 / 2 yields an integer 2.
 5.0 / 2 result is 2.5
 5 % 2 is equal to 1
 System.out.println(3 * 4); prints 12
 Q: What is the result of (3 * 4 % 5) ?
 Q: 1 * 2 + 3 * 5 % 4 = ?
Operator Precedence
26

 Java applies the operators in a precise sequence


determined by the rules of operator precedence.
Algebraic and Java Expressions &
Operator Precedence examples (1)
27

Algebra: y = mx + b
Java: y = m * x + b;
3 1 2

Algebra: m =

Java: m = (a + b + c + d) / 5;
5 1 2 3 4
Precedence examples (1)
28
Precedence examples (2)
29

 1 * 2 + 3 * 5 % 4  1 + 8 % 3 * 2 - 9
 \_/  \_/
| |
2 + 3 * 5 % 4 1 + 2 * 2 - 9
 \_/
 \___/
| |
2 + 15 % 4 1 + 4 - 9
 \____/
 \______/
| |
2 + 3 5 - 9
 \_________/
 \________/ |
| -4
5
Precedence Questions
30

 What values result from the following expressions?


 9 / 5
 695 % 20
 7 + 6 * 5
 7 * 6 + 5
 248 % 100 / 5
 6 * 3 - 9 / 4
 (5 - 7) * 4
 6 + (18 % (17 - 12))
Real Numbers (Type double or float)
31

 Examples: 6.022 , -42.0 , 2.143e17

 Placing .0 or . after an integer makes it a double.

 Example: 15 / 2 is 7, but
15.0 / 2.0 is 7.5
Real Number Example
32

 2.0 * 2.4 + 2.25 * 4.0 / 2.0


 \___/
|
4.8 + 2.25 * 4.0 / 2.0
 \___/
|
4.8 + 9.0 / 2.0
 \_____/
|
4.8 + 4.5
 \____________/
|
9.3
Identifiers
33

 An identifier is a sequence of characters that consist of


letters, digits, underscores (_), and dollar signs ($).
 Avoid using the dollar sign.
 An identifier cannot start with a digit.
 An identifier cannot be a reserved word.
 An identifier cannot be true, false, or null.
 An identifier can be of any length.
 Avoid using long identifiers.
Naming Conventions
34

 Sticking with the Java naming conventions makes your programs easy
to read and avoids errors.
 Choose meaningful and descriptive names.
a) Variables and method names:
 With single word variable names, all characters are lower case
 Example: grades, radius, area.
 If the name consists of several words, use lowercase for the first word, and
capitalize the first letter of each subsequent word in the name.
 Example: computeArea, studentName .
b) Constants:
 Capitalize all letters in constants, with more than one word constant use
underscore (_) between the words.
 Example: PI, MAX_VALUE.
c) Class names:
 Capitalize the first letter of each word in the name.
 Example: ComputeArea.
Why Variables?
35

 What's bad about the following code which calculate ?


Assume x = 40.
public class Equation{
public static void main(String[] args) {
System.out.println( 40 * 40 + 8 * 40 + 5);
}
}

 Suppose changing number 40 to 35.


 Improve the program using variable.
public class Equation{
public static void main(String[] args) {
int x = 40;
System.out.println( x * x + 8 * x + 5);
}
}
References
36

 http://introcs.cs.princeton.edu/java/home/
 www.prenhall.com/liang
 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/data
types.html
 http://docs.oracle.com/javase/tutorial/index.html
 http://math.hws.edu/javanotes/c1/s4.html
 cs.nyu.edu/courses/spring07/V22.0101-002/02slide.ppt
 Herbert Schildt, “Java A Beginner’s Guide: Create,
Compile, and Run Java Programs Today,” 5th Ed, page 15.
 Deitel & Deitel, “Java How to Program”, 9th ed, chapter 2,
page 37.
References
37

 Liang, “Introduction to Java Programming”, 10th ed.,


chapter 2, page 33.
 For more information about double and float
numbers see the following references:
 https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.
html#jls-4.3.3
 https://docs.oracle.com/javase/tutorial/java/nutsandbolt
s/datatypes.html

You might also like