variables and types - information and computer sciencecis110/18su/lectures/11types.pdf · data...

15
1 Variables and Types Section 1.2

Upload: others

Post on 09-Sep-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

11

Variables and Types

Section 1.2

Page 2: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

22

Java in one slideBuilt-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 - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

3

Variables and Types

Section 1.2

Page 4: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

4

Assignment

Section 1.2

Page 5: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

5

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 6: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

6

Integers: Example Program

Section 1.2

Page 7: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

7

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 8: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

8

Doubles: Example Program

Section 1.2

Page 9: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

9

Java Math Library (Excerpts)

Section 1.2

Page 10: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

10

char: Single Characters

Section 1.2

Expression Result?

'A'

'A' + 0

(int) 'A'

(char) 65

(int) 'a'

(int) '0'

'3' – '0'

Page 11: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

11

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 12: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

12

Booleans: Example Program

Section 1.2

Presenter
Presentation Notes
image from http://upload.wikimedia.org/wikipedia/commons/c/ce/George_Boole_color.jpg
Page 13: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

13

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 14: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

14

Strings: Example Program

Section 1.2

Presenter
Presentation Notes
ruler from http://www.myonlineruler.com/
Page 15: Variables and Types - Information and Computer Sciencecis110/18su/lectures/11types.pdf · Data Types • int, double, char, boolean, String, ... • Help avoid errors and ambiguities

15

Data Types

• int, double, char, boolean, String, ...• Help avoid errors and ambiguities

– What does a + b do?

• Not perfect:

Section 1.2Ariane 5: Bad type conversion Mars Climate Orbiter: Bad unit conversion

Presenter
Presentation Notes
Ariane images from Princeton slides; Mars Climate Orbiter from http://upload.wikimedia.org/wikipedia/commons/thumb/1/19/Mars_Climate_Orbiter_2.jpg/264px-Mars_Climate_Orbiter_2.jpg