SlideShare a Scribd company logo
CSE110
Principles of Programming
with Java
Lecture 04:
Primitive Data Types
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2
Summary
class
global
variables
methods statements
instructions
local
variables
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3
Previously …
§ There are exactly eight primitive data types in Java
§ Four of them represent integers:
byte, short, int, long
§ Two of them represent floating point numbers:
float, double
§ One of them represents characters:
char
§ And one of them represents boolean values:
boolean
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4
Data Types
The difference between the various numeric primitive
types is their size, and therefore the values they can
store:
Type Size Min Value Max Value
byte 8 bits -128 127
short 16 bits -32,768 32,767
int 32 bits -2^31 2^31 - 1
long 64 bits -2^63 2^63 - 1
float 32 bits +/- 3.4 x 1038 with 7 significant digits
double 64 bits +/- 1.7 x 10308 with 15 significant digits
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 5
char
• A char variable stores a single character from the
Unicode character set
• The Unicode character set uses 16 bits per
character, allowing for 65,536 unique characters
• It is an international character set, containing
symbols and characters from many world
languages
• Character literals are delimited by single quotes:
'a' 'X' '7' '$' ',' 'n'
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6
Escape Sequences
Escape
Sequence
Meaning
b backspace
t tab
n newline
” double
quote
’ single quote
 backslash
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7
boolean
• A boolean value – only 2 values, true or false
• The reserved words true and false are the only
valid values for a boolean type
boolean done = false;
boolean success;
success = true;
Arithmetic Expressions
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9
Summary
class
global
variables
methods statements
local
variables
instructions
expressions
arithmetic
expression
relational
expression
logical
expression
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10
Expressions
• An expression is a combination of one or more
operands and their operators
• Arithmetic expressions compute numeric results
and make use of the arithmetic operators:
o Addition +
o Subtraction –
o Minus Unary –
o Multiplication *
o Division /
o Remainder %
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 11
Integer Division and Remainder
• Dividend / Divisor = Quotient and Remainder
• Dividend = (Divisor x Quotient) + Remainder
• If both operands to the division operator (/) are
integers, the result is an integer (the fractional part is
discarded)
14 / 3 equals 4 14 % 3 equals 2
8 / 12 equals 0 8 % 12 equals 8
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12
Test Yourselves
• 17 % 4 =
• -20 % 3 =
• 10 % 5 =
• 3 % 8 =
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13
Operator Precedence
• Operators have a well-defined precedence which
determines the order in which they are evaluated
• Multiplication, division, and remainder are
evaluated prior to addition, subtraction, and string
concatenation
• Arithmetic operators with the same precedence
are evaluated from left to right
• Parentheses can be used to force the evaluation
order
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14
Operator Precedence
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 15
String Concatenation
• The plus operator (+) is used to concatenate
(append) strings:
“Hello” + “ World”
• To break a string into two parts in two lines, we need
to close with a double quote and use + sign to
concatenate (append):
System.out.println(“ASU is “
+ “in Arizona”);
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 16
+ operator
• If two operands of + are numbers, it performs an
arithmetic addition.
• If at least one of operands of + is a string, it performs
a string concatenation.
• Examples:
o 2 + 3 will be 5
o 2 + “ apples” will be “2 apples”
o “number ” + 5 will be “number 5”
o “we have “ + 2 + 3 will be “we have 23”
o “we have “ + (2 + 3) will be “we have 5”
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 17
Homework
Read Chapter 2
CSE110 - Principles of Programming
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2017
Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.

More Related Content

PDF
Programming C Part 02
PDF
201707 CSE110 Lecture 20
DOCX
Mettl prg
PPTX
Performing calculations using java script
PDF
201707 CSE110 Lecture 05
PPTX
Lecture 3 and 4.pptx
PPTX
Java chapter 2
PPT
Ap Power Point Chpt2
Programming C Part 02
201707 CSE110 Lecture 20
Mettl prg
Performing calculations using java script
201707 CSE110 Lecture 05
Lecture 3 and 4.pptx
Java chapter 2
Ap Power Point Chpt2

Similar to 201707 CSE110 Lecture 04 (20)

PDF
03 expressions.ppt
PPSX
Dr. Rajeshree Khande : Programming concept of basic java
PPSX
Dr. Rajeshree Khande : Java Basics
PPT
Java: Primitive Data Types
PPT
Chap02
PPT
ch02-primitive-data-definite-loops.ppt
PPT
ch02-primitive-data-definite-loops.ppt
PDF
BIT211_2.pdf
PPT
Numerical Data And Expression
PDF
java basics - keywords, statements data types and arrays
PPTX
Java basics variables
PDF
Week02
PPT
9781439035665 ppt ch02
PPTX
Java fundamentals
PPT
Ch02 primitive-data-definite-loops
PDF
Arithmetic instructions
PPTX
03 and 04 .Operators, Expressions, working with the console and conditional s...
PPTX
Fundamental programming structures in java
PPTX
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
PDF
03-Primitive-Datatypes.pdf
03 expressions.ppt
Dr. Rajeshree Khande : Programming concept of basic java
Dr. Rajeshree Khande : Java Basics
Java: Primitive Data Types
Chap02
ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.ppt
BIT211_2.pdf
Numerical Data And Expression
java basics - keywords, statements data types and arrays
Java basics variables
Week02
9781439035665 ppt ch02
Java fundamentals
Ch02 primitive-data-definite-loops
Arithmetic instructions
03 and 04 .Operators, Expressions, working with the console and conditional s...
Fundamental programming structures in java
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
03-Primitive-Datatypes.pdf
Ad

More from Javier Gonzalez-Sanchez (20)

PDF
201804 SER332 Lecture 01
PDF
201801 SER332 Lecture 03
PDF
201801 SER332 Lecture 04
PDF
201801 SER332 Lecture 02
PDF
201801 CSE240 Lecture 26
PDF
201801 CSE240 Lecture 25
PDF
201801 CSE240 Lecture 24
PDF
201801 CSE240 Lecture 23
PDF
201801 CSE240 Lecture 22
PDF
201801 CSE240 Lecture 21
PDF
201801 CSE240 Lecture 20
PDF
201801 CSE240 Lecture 19
PDF
201801 CSE240 Lecture 18
PDF
201801 CSE240 Lecture 17
PDF
201801 CSE240 Lecture 16
PDF
201801 CSE240 Lecture 15
PDF
201801 CSE240 Lecture 14
PDF
201801 CSE240 Lecture 13
PDF
201801 CSE240 Lecture 12
PDF
201801 CSE240 Lecture 11
201804 SER332 Lecture 01
201801 SER332 Lecture 03
201801 SER332 Lecture 04
201801 SER332 Lecture 02
201801 CSE240 Lecture 26
201801 CSE240 Lecture 25
201801 CSE240 Lecture 24
201801 CSE240 Lecture 23
201801 CSE240 Lecture 22
201801 CSE240 Lecture 21
201801 CSE240 Lecture 20
201801 CSE240 Lecture 19
201801 CSE240 Lecture 18
201801 CSE240 Lecture 17
201801 CSE240 Lecture 16
201801 CSE240 Lecture 15
201801 CSE240 Lecture 14
201801 CSE240 Lecture 13
201801 CSE240 Lecture 12
201801 CSE240 Lecture 11
Ad

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Spectroscopy.pptx food analysis technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
Teaching material agriculture food technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Understanding_Digital_Forensics_Presentation.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation theory and applications.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Programs and apps: productivity, graphics, security and other tools
Spectroscopy.pptx food analysis technology
Review of recent advances in non-invasive hemoglobin estimation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Teaching material agriculture food technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
Understanding_Digital_Forensics_Presentation.pptx
The AUB Centre for AI in Media Proposal.docx
NewMind AI Weekly Chronicles - August'25 Week I
Mobile App Security Testing_ A Comprehensive Guide.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Dropbox Q2 2025 Financial Results & Investor Presentation
The Rise and Fall of 3GPP – Time for a Sabbatical?
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Chapter 3 Spatial Domain Image Processing.pdf
Network Security Unit 5.pdf for BCA BBA.
Spectral efficient network and resource selection model in 5G networks
Encapsulation theory and applications.pdf

201707 CSE110 Lecture 04

  • 1. CSE110 Principles of Programming with Java Lecture 04: Primitive Data Types Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2 Summary class global variables methods statements instructions local variables
  • 3. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3 Previously … § There are exactly eight primitive data types in Java § Four of them represent integers: byte, short, int, long § Two of them represent floating point numbers: float, double § One of them represents characters: char § And one of them represents boolean values: boolean
  • 4. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4 Data Types The difference between the various numeric primitive types is their size, and therefore the values they can store: Type Size Min Value Max Value byte 8 bits -128 127 short 16 bits -32,768 32,767 int 32 bits -2^31 2^31 - 1 long 64 bits -2^63 2^63 - 1 float 32 bits +/- 3.4 x 1038 with 7 significant digits double 64 bits +/- 1.7 x 10308 with 15 significant digits
  • 5. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 5 char • A char variable stores a single character from the Unicode character set • The Unicode character set uses 16 bits per character, allowing for 65,536 unique characters • It is an international character set, containing symbols and characters from many world languages • Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' 'n'
  • 6. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6 Escape Sequences Escape Sequence Meaning b backspace t tab n newline ” double quote ’ single quote backslash
  • 7. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7 boolean • A boolean value – only 2 values, true or false • The reserved words true and false are the only valid values for a boolean type boolean done = false; boolean success; success = true;
  • 9. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9 Summary class global variables methods statements local variables instructions expressions arithmetic expression relational expression logical expression
  • 10. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10 Expressions • An expression is a combination of one or more operands and their operators • Arithmetic expressions compute numeric results and make use of the arithmetic operators: o Addition + o Subtraction – o Minus Unary – o Multiplication * o Division / o Remainder %
  • 11. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 11 Integer Division and Remainder • Dividend / Divisor = Quotient and Remainder • Dividend = (Divisor x Quotient) + Remainder • If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) 14 / 3 equals 4 14 % 3 equals 2 8 / 12 equals 0 8 % 12 equals 8
  • 12. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12 Test Yourselves • 17 % 4 = • -20 % 3 = • 10 % 5 = • 3 % 8 =
  • 13. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13 Operator Precedence • Operators have a well-defined precedence which determines the order in which they are evaluated • Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation • Arithmetic operators with the same precedence are evaluated from left to right • Parentheses can be used to force the evaluation order
  • 14. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14 Operator Precedence
  • 15. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 15 String Concatenation • The plus operator (+) is used to concatenate (append) strings: “Hello” + “ World” • To break a string into two parts in two lines, we need to close with a double quote and use + sign to concatenate (append): System.out.println(“ASU is “ + “in Arizona”);
  • 16. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 16 + operator • If two operands of + are numbers, it performs an arithmetic addition. • If at least one of operands of + is a string, it performs a string concatenation. • Examples: o 2 + 3 will be 5 o 2 + “ apples” will be “2 apples” o “number ” + 5 will be “number 5” o “we have “ + 2 + 3 will be “we have 23” o “we have “ + (2 + 3) will be “we have 5”
  • 17. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 17 Homework Read Chapter 2
  • 18. CSE110 - Principles of Programming Javier Gonzalez-Sanchez javiergs@asu.edu Summer 2017 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.