30
1 1 Variables and Types Section 1.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

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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