Data Types, Expressions, and Operators in Java

Welcome to CSE 121!
Simon Wu
Summer 2024
sli.do #cse121
TAs:
Lesson 2 - Summer 2024
1
Use this QR code as one
way to ask questions!
Announcements, Reminders
 due tonight (June 26) @ 11:59 PM
Programming Assignment 0 released later today (due
Tues, July 2)
IPL is open! - 
 can be found on course
website.
Reminder: please double-check all quiz and exam dates (let
Simon know ASAP if you can’t make it!)
Schedule and instructionsCreative Project 0
Lesson 2 - Summer 2024
2
PCM
 Recap: Data Types & Expressions
Types: int
, 
double
, 
String
, 
boolean
Expressions: Operators
Beware of precedence! (order of operations)
Lesson 2 - Summer 2024
3
(PCM)
 
Data Types in Java
In programming, you’re dealing with data…
int
s (whole numbers)
double
s (real numbers)
String
s
boolean
s (true or false)
Lesson 2 - Summer 2024
4
(PCM) 
Operators (for numerical & 
String
 values)
Numerical:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo or “Mod”
<, >, <=, >=, ==, != 
Strings
+ Concatenation
Booleans
! Logical Not
&& Logical And
|| Logical Or
Lesson 2 - Summer 2024
5
(PCM) 
Precedence
P
arentheses
M
ultiplication
, M
odulo
, D
ivision
A
ddition (and Concatenation)
, 
S
ubtraction
Lesson 2 - Summer 2024
If multiple operators at the same level?
Evaluate subexpressions from 
left to right
!
6
Example
1 + 2 * 3
(1 + 2) * 3
Lesson 2 - Summer 2024
7
Work on Expressions/Types Practice Problems
Part 1
Ed lesson linked from the course
calendar
Work with the folks around you!
TAs and I will be walking around
to help
Lesson 2 - Summer 2024
8
5 + 2 * 4
1 + 2 / 3
6 * 5 % 7
Questions?
Lesson 2 - Summer 2024
9
(PCM) 
Mixing Types
Lesson 2 - Summer 2024
When mixing types in an expression,
Java will convert one type to the other
and then perform the operation
“normally”
int
s can be converted to 
double
s
Both 
int
s and 
double
s can be
converted to 
String
s
10
Example 2
2 + 2 + "hello" + 3 * 5 + 10
Lesson 2 - Summer 2024
11
Work on Expressions/Types Practice Problems 
Part 2
Lesson 2 - Summer 2024
12
Ed lesson linked from the course
calendar
Work with the folks around you!
TAs and I will be walking around
to help
Can always refer back to PCM!
5 * 3 + 1.0
8 / 3 * 2.0
8.0 / 3 * 2
"Hello" + "world"
1 + "2" + 3
1 + 2 + "3"
1 + "2" + (3 + 4)
Questions?
Lesson 2 - Summer 2024
13
(PCM) 
Boolean Operators
Lesson 2 - Summer 2024
!
 Logical Not
< > <= >=
 Relational Operators
== !=
 Relational Operators (equality)
&&
 Logical And
||
 Logical Or
14
(PCM) 
Precedence (updated)
P
arentheses
Logical not
M
ultiplication
, M
odulo
, D
ivision
A
ddition (and Concatenation)
, 
S
ubtraction
Relational operators
Equality operators
Logical and
Logical or
Lesson 2 - Summer 2024
15
Example 3
1 + 2 * 3 != (1 + 2) * 3
Lesson 2 - Summer 2024
16
Work on Expressions/Types Practice Problems 
Part 3
Lesson 2 - Summer 2024
17
Ed lesson linked from the course
calendar
Work with the folks around you!
TAs and I will be walking around to
help
Can always refer back to PCM!
5 * 3 < 12
10 % 3 == 10 / 3
5 < 9 || (7 != 7)
!(1 + 2 == 3 && 10 % 4 > 2)
Questions?
Lesson 2 - Summer 2024
18
(PCM) 
Variables
Now that we know about different
types and data, we can learn about
how to store it!
Java allows you to create variables
within a program. A variable has
A type
A name
(Potentially) a value it is storing
 
Declaration:
    int
 
x;
Initialization:
   x
 = 
30
;
 
Or all in one line:
 
int
 
x
 = 
30
;
19
(PCM) 
Variables
 
 
 
 
 
 
T
h
e
y
r
e
 
m
a
d
e
 
t
o
 
b
e
 
m
a
n
i
p
u
l
a
t
e
d
,
 
m
o
d
i
f
i
e
d
,
 
r
e
-
u
s
e
d
!
i
n
t
 
m
y
F
a
v
o
r
i
t
e
N
u
m
b
e
r
 
=
 
7
;
i
n
t
 
d
o
u
b
l
e
F
V
 
=
 
m
y
F
a
v
o
r
i
t
e
N
u
m
b
e
r
 
*
 
2
;
m
y
F
a
v
o
r
i
t
e
N
u
m
b
e
r
 
=
 
m
y
F
a
v
o
r
i
t
e
N
u
m
b
e
r
 
+
 
3
;
Notice – this doesn't
really make any
mathematical sense!
That's because, in
Java, = is 
assignment
,
not equality!
20
(PCM) 
Strings and chars
String = sequence of characters treated
as one, yet can be indexed to get
individual parts
Zero-based indexing 💣
Side note
: new data type! 
char
, represents a single character,
so we
 use single quotes
Strings are made up of 
char
s! 
21
(PCM) 
String Methods 
 
Usage: 
<string variable>
.
<method>
(
)
22
Slide Note
Embed
Share

This resource provides information on data types, expressions, and operators in Java. It covers topics such as data types (int, double, String, boolean), operator precedence, and practice problems to work on expressions and types. The content includes announcements, reminders, a recap of data types, and examples to reinforce learning. Whether you're a beginner or looking to strengthen your Java fundamentals, this resource offers valuable insights for understanding and applying these concepts effectively.

  • Java Fundamentals
  • Data Types
  • Expressions
  • Operators
  • Programming Concepts

Uploaded on Oct 03, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Welcome to CSE 121! Simon Wu Summer 2024 way to ask questions! Use this QR code as one Trey Hannah Mia Vivian Jolie Colton Ziao TAs: sli.do #cse121 Lesson 2 - Summer 2024 1

  2. Announcements, Reminders Creative Project 0 due tonight (June 26) @ 11:59 PM Programming Assignment 0 released later today (due Tues, July 2) IPL is open! - Schedule and instructions can be found on course website. Reminder: please double-check all quiz and exam dates (let Simon know ASAP if you can t make it!) Lesson 2 - Summer 2024 2

  3. PCM Recap: Data Types & Expressions Types: int, double, String, boolean Expressions: Operators Beware of precedence! (order of operations) Lesson 2 - Summer 2024 3

  4. (PCM) Data Types in Java In programming, you re dealing with data ints (whole numbers) doubles (real numbers) Strings booleans (true or false) Lesson 2 - Summer 2024 4

  5. (PCM) Operators (for numerical & String values) Strings + Concatenation Numerical: + Addition - Subtraction * Multiplication / Division % Modulo or Mod Booleans ! Logical Not && Logical And || Logical Or <, >, <=, >=, ==, != Lesson 2 - Summer 2024 5

  6. (PCM) Precedence Parentheses Multiplication, Modulo, Division Addition (and Concatenation), Subtraction If multiple operators at the same level? Evaluate subexpressions from left to right! Lesson 2 - Summer 2024 6

  7. Example 1 + 2 * 3 (1 + 2) * 3 Lesson 2 - Summer 2024 7

  8. Work on Expressions/Types Practice Problems Part 1 Ed lesson linked from the course calendar Work with the folks around you! TAs and I will be walking around to help 5 + 2 * 4 1 + 2 / 3 6 * 5 % 7 Lesson 2 - Summer 2024 8

  9. Questions? Lesson 2 - Summer 2024 9

  10. (PCM) Mixing Types When mixing types in an expression, Java will convert one type to the other and then perform the operation normally ints can be converted to doubles Both ints and doubles can be converted to Strings Lesson 2 - Summer 2024 10

  11. Example 2 2 + 2 + "hello" + 3 * 5 + 10 Lesson 2 - Summer 2024 11

  12. Work on Expressions/Types Practice Problems Part 2 Ed lesson linked from the course calendar Work with the folks around you! TAs and I will be walking around to help Can always refer back to PCM! 5 * 3 + 1.0 8 / 3 * 2.0 8.0 / 3 * 2 "Hello" + "world" 1 + "2" + 3 1 + 2 + "3" 1 + "2" + (3 + 4) Lesson 2 - Summer 2024 12

  13. Questions? Lesson 2 - Summer 2024 13

  14. (PCM) Boolean Operators ! Logical Not < > <= >= Relational Operators == != Relational Operators (equality) && Logical And || Logical Or Lesson 2 - Summer 2024 14

  15. (PCM) Precedence (updated) Parentheses Logical not Multiplication, Modulo, Division Addition (and Concatenation), Subtraction Relational operators Equality operators Logical and Logical or Lesson 2 - Summer 2024 15

  16. Example 3 1 + 2 * 3 != (1 + 2) * 3 Lesson 2 - Summer 2024 16

  17. Work on Expressions/Types Practice Problems Part 3 Ed lesson linked from the course calendar Work with the folks around you! TAs and I will be walking around to help Can always refer back to PCM! 5 * 3 < 12 10 % 3 == 10 / 3 5 < 9 || (7 != 7) !(1 + 2 == 3 && 10 % 4 > 2) Lesson 2 - Summer 2024 17

  18. Questions? Lesson 2 - Summer 2024 18

  19. (PCM) Variables Now that we know about different types and data, we can learn about how to store it! Declaration: Initialization: int x; x = 30; Java allows you to create variables within a program. A variable has A type A name (Potentially) a value it is storing Or all in one line: int x = 30; 19

  20. (PCM) Variables Notice this doesn't really make any mathematical sense! That's because, in Java, = is assignment, not equality! They re made to be manipulated, modified, re-used! int myFavoriteNumber = 7; int doubleFV = myFavoriteNumber * 2; myFavoriteNumber = myFavoriteNumber + 3; 20

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#