Top Banner
1 1 Variables and Types Section 1.2
30

Variables and Typescis110/19su/lectures/11types.pdf · 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

Jul 20, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

1 1

Variables and Types

Section 1.2

Page 2: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

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 Typescis110/19su/lectures/11types.pdf · 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 /

3

Variables and Types

Section 1.2

Page 4: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

4

Variables and Types

Section 1.2

Page 5: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

5

Variables and Types

Section 1.2

Page 6: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

6

Variables and Types

Section 1.2

Page 7: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

7

Variables and Types

Section 1.2

Page 8: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

8

Variables and Types

Section 1.2

Page 9: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

9

Assignment

Section 1.2

Page 10: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

10

Assignment

Section 1.2

Page 11: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

11

Assignment

Section 1.2

Page 12: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

12

Assignment

Section 1.2

Page 13: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

13

Assignment

Section 1.2

Page 14: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

14

Assignment

Section 1.2

Page 15: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

15

Assignment

Section 1.2

Page 16: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

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 Typescis110/19su/lectures/11types.pdf · 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 /

17

Integers: Example Program

Section 1.2

Page 18: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

18

Integers: Example Program

Section 1.2

Page 19: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

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 Typescis110/19su/lectures/11types.pdf · 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 /

20

Doubles: Example Program

Section 1.2

Page 21: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

21

Java Math Library (Excerpts)

Section 1.2

Page 22: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

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 Typescis110/19su/lectures/11types.pdf · 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 /

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 Typescis110/19su/lectures/11types.pdf · 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 /

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 Typescis110/19su/lectures/11types.pdf · 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 /

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 Typescis110/19su/lectures/11types.pdf · 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 /

26

Booleans: Example Program

Section 1.2

Page 27: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

27

Booleans: Example Program

Section 1.2

Page 28: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

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 Typescis110/19su/lectures/11types.pdf · 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 /

29

Strings: Example Program

Section 1.2

Page 30: Variables and Typescis110/19su/lectures/11types.pdf · 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 /

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