Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression...
Transcript of Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression...
![Page 1: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/1.jpg)
1 1
Variables and Types
Section 1.2
![Page 2: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/2.jpg)
2 2
Java in one slide Built-In Types
int
double
String
char
boolean
Flow Control
if
else
for
while
Printing
System.out.print()
System.out.println()
Assignment
=
Arrays
arr[i]
new
arr.length
Numeric Operations
+ - *
/ % ++
-- > <
== >= <=
(int) x (double) x (char) x
Integer.parseInt()
Double.parseDouble()
Math Library
Math.sin() Math.cos()
Math.log() Math.exp()
Math.sqrt() Math.pow()
Math.min() Math.max()
Math.abs() Math.PI
Boolean Operations
true false
|| &&
!
String Operations
+ ""
length() compareTo()
charAt() equals()
Objects
class static
public private
new this
Punctuation
{ }
( )
, ;
Section 1.2
![Page 3: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/3.jpg)
3
Variables and Types
Section 1.2
![Page 4: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/4.jpg)
4
Variables and Types
Section 1.2
![Page 5: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/5.jpg)
5
Variables and Types
Section 1.2
![Page 6: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/6.jpg)
6
Variables and Types
Section 1.2
![Page 7: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/7.jpg)
7
Variables and Types
Section 1.2
![Page 8: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/8.jpg)
8
Variables and Types
Section 1.2
![Page 9: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/9.jpg)
9
Assignment
Section 1.2
![Page 10: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/10.jpg)
10
Assignment
Section 1.2
![Page 11: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/11.jpg)
11
Assignment
Section 1.2
![Page 12: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/12.jpg)
12
Assignment
Section 1.2
![Page 13: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/13.jpg)
13
Assignment
Section 1.2
![Page 14: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/14.jpg)
14
Assignment
Section 1.2
![Page 15: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/15.jpg)
15
Assignment
Section 1.2
![Page 16: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/16.jpg)
16
int: Integers (whole numbers)
Section 1.2
Expression Result?
5 + 3
5 – 3
5 * 3
5 / 3
5 % 3
5 % -3
1 / 0
3 * 5 – 2
3 + 5 / 2
3 – 5 / 2
(3 – 5) / 2
3 – (5 – 2) / 2
Integer.parseInt("3")
Integer.parseInt(3)
![Page 17: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/17.jpg)
17
Integers: Example Program
Section 1.2
![Page 18: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/18.jpg)
18
Integers: Example Program
Section 1.2
![Page 19: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/19.jpg)
19
double: Floating-Point (fractions)
Section 1.2
Expression Result?
3.141 + 0.03
6.02e23 / 2.0
5.0 / 3
(int) 5.0 / 3
5.0 / (int) 3
10.0 % 3.141
1.0 / 0.0
-1.0 / 0.0
0.0 / 0.0
Math.sqrt(2)
Math.sqrt(-1)
Math.sqrt(2) * Math.sqrt(2)
Math.PI
Math.pi
![Page 20: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/20.jpg)
20
Doubles: Example Program
Section 1.2
![Page 21: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/21.jpg)
21
Java Math Library (Excerpts)
Section 1.2
![Page 22: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/22.jpg)
22
char: Single Characters
Section 1.2
Expression Result?
'A'
'A' + 0
(int) 'A'
(char) 65
(int) 'a'
(int) '0'
'3' – '0'
![Page 23: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/23.jpg)
23
char: Single Characters
Section 1.2
Expression Result?
'A'
'A' + 0
(int) 'A'
(char) 65
(int) 'a'
(int) '0'
'3' – '0'
![Page 24: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/24.jpg)
24
char: Single Characters
Section 1.2
Expression Result?
'A'
'A' + 0
(int) 'A'
(char) 65
(int) 'a'
(int) '0'
'3' – '0'
![Page 25: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/25.jpg)
25
boolean: True/False
Section 1.2
Expression Result?
true
!false
'A' == 'a'
Math.PI != 3.14
'a' > 'b
1.7 <= (17 / 10)
true && true
true && false
false && false
true || true
true || false
false || false
(1 < 3) && (3 == (6 / 2))
(1 >= 3) || !(3 == (6 / 2))
![Page 26: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/26.jpg)
26
Booleans: Example Program
Section 1.2
![Page 27: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/27.jpg)
27
Booleans: Example Program
Section 1.2
![Page 28: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/28.jpg)
28
String: Text
Section 1.2
Expression Result?
"This is a string literal."
"1" + "2"
1 + " + " + 2 + " = " + 3
'1' + "2"
0 + '1' + "2"
"" + Math.sqrt(2)
(String) Math.sqrt(2)
(string) Math.sqrt(2)
"A" == "A"
"A".equals("A")
"B" < "A"
"B".compareTo("A")
"B".compareTo("B")
"B".compareTo("C")
![Page 29: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/29.jpg)
29
Strings: Example Program
Section 1.2
![Page 30: Variables and Types - Penn Engineeringcis110/current/lectures/11types.pdfSection 1.2 Expression Result? 3.141 + 0.03 6.02e23 / 2.0 5.0 / 3 (int) 5.0 / 3 5.0 / (int) 3 10.0 % 3.141](https://reader033.fdocuments.us/reader033/viewer/2022060601/6054f4338068f0750d0fecf2/html5/thumbnails/30.jpg)
30
Data Types
• int, double, char, boolean, String, ...
• Help avoid errors and ambiguities
– What does a + b do?
• Not perfect:
Section 1.2
Ariane 5: Bad type conversion Mars Climate Orbiter: Bad unit conversion