primitive data types. identifiers what word does it sound like?

28
Primitive Data Types

Upload: clarissa-mosley

Post on 05-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Primitive Data Types. Identifiers What word does it sound like?

Primitive Data Types

Page 2: Primitive Data Types. Identifiers What word does it sound like?

Identifiers

What word does it sound like?

Page 3: Primitive Data Types. Identifiers What word does it sound like?

Identifiers

Name that will be used to describe anything a programmer is required to define. classes, methods, constants, variables;

Examples Name of objects

marker, pencil

Methods turnRight, forward, move

Page 4: Primitive Data Types. Identifiers What word does it sound like?

Rules for Identifiers

Must start with a letter After the letter, can be any combination

of letters, numbers, or _ No SPACES!!! Cannot be a reserved word (words with

special meanings in java [see handout])

Page 5: Primitive Data Types. Identifiers What word does it sound like?

Example Identifiers

Are these okay? myPerson m_person person1 my Person 1person person#1

Page 6: Primitive Data Types. Identifiers What word does it sound like?

Example Identifiers

These are fine myPerson m_person person1

These are NOT my Person 1person person#1

Page 7: Primitive Data Types. Identifiers What word does it sound like?

Java is Case Sensitive

Person ≠ person ≠ perSon

Page 8: Primitive Data Types. Identifiers What word does it sound like?

Good Identifiers

Don’t use single letters Make them descriptive

grades instead of g

Variable names should be meaningful but concise gpa instead of

gradePointAverageForStudentsAtThisSchool

Page 9: Primitive Data Types. Identifiers What word does it sound like?

Some Conventions

Class names start with capitals DrawingTool

Variable names start with lowercase marker

Multiple word names have a capital letter at the beginning of each new word turnRight

Constants (value never changes) are in all capitals MAXSCORE

Page 10: Primitive Data Types. Identifiers What word does it sound like?

Data Types

Depending on what you want to store in java, you need to tell it what type it is.

Why do you think it matters if something is a number or a letter?

Page 11: Primitive Data Types. Identifiers What word does it sound like?

Type Matters

Math You can’t add the number 5 to the word

“Happy”

Depending on the type, java has to make a given amount of space for it.

Page 12: Primitive Data Types. Identifiers What word does it sound like?

Primitive Data Types

int – integers – whole numbers -5 0 86

double – decimals 3.14 5.0 -1.2 6.02e23

scientific notation - 6.02e23 = 6.02x10^23

boolean – true or false char – holds one character

‘a’ ‘%’ ‘6’

Page 13: Primitive Data Types. Identifiers What word does it sound like?

Invalid Numbers

Don’t do this $5.06 #3.0 86%

Page 14: Primitive Data Types. Identifiers What word does it sound like?

You Might Also see

long and short are like int float is like double

Page 15: Primitive Data Types. Identifiers What word does it sound like?

Declaring variables

Remember me? DrawingTool marker;

Other variables are the same: int number; number = 86; int number = 86;

You only declare the type once! First time> DrawingTool marker; After> marker.

Page 16: Primitive Data Types. Identifiers What word does it sound like?

Ascii

The characters are secretly stored as integer values. Thus ascii value 65 is the captial ‘A’

Page 17: Primitive Data Types. Identifiers What word does it sound like?

System.out

One way to print out to the screen System.out.print

Print and don’t skip a line System.out.print(“Hello”);System.out.print(“World”);

– prints HelloWorld

System.out.println Print and skip a line

System.out.println(“Hello”);System.out.println(“World”);

– prints Hello World

Page 18: Primitive Data Types. Identifiers What word does it sound like?

Examples int number = 5;

char letter = 'E'; double average = 3.95; boolean done = false;  System.out.println("number = " + number); System.out.println("letter = " + letter); System.out.println("average = " + average); System.out.println("done = " + done); System.out.print("The "); System.out.println("End!"); Run output: number = 5letter = Eaverage = 3.95done = falseThe End!

Page 19: Primitive Data Types. Identifiers What word does it sound like?

What does + mean?

Inside System.out.println("number = " + number); + means add the words “number = “ to the

value of number

Page 20: Primitive Data Types. Identifiers What word does it sound like?

Escape CharactersCharacterJava Escape Sequence Newline '\n'Horizontal tab '\t'Backslash '\\'Single quote '\''Double quote '\"'Null character '\0' System.out.println(“This is a\ntest and only\’ a test.”);  Run output: This is atest and only’ a test.

Page 21: Primitive Data Types. Identifiers What word does it sound like?

Interesting Differences

System.out.println( 2 + 2);

//Output: 4

System.out.println(“2 + 2”);

//Output: 2 + 2

System.out.println(“2” + “2”);

//Output: 22

Page 22: Primitive Data Types. Identifiers What word does it sound like?

Castingchar letter = 'A';int number = 75;System.out.println("letter = " + letter); System.out.print("its ASCII value = ");System.out.println((int)letter); System.out.print("ASCII value 75 = ");System.out.println((char)number); Run output: letter = Aits ASCII value = 65 ASCII value 75 = K

 

Page 23: Primitive Data Types. Identifiers What word does it sound like?

Assignment (=)

The = sign works RIGHT to LEFT only! a = 5;

Means the variable a gets the value 5 5 = a; DOES NOT WORK!!! a = 3;

b = 5;

a = b;

a now equals?

b now equals?

Page 24: Primitive Data Types. Identifiers What word does it sound like?

Variables with =

On the LEFT side, mean save the answer here a = 5 + 3;

On the RIGHT side, means look up the value b = 6; a = b + 2;

Page 25: Primitive Data Types. Identifiers What word does it sound like?

You can do math

a = 5 + 3; Adding is + Subtracting is – Multiplication is * Division is / Modulus is %

Page 26: Primitive Data Types. Identifiers What word does it sound like?

Careful with division

If you divide by an integer, java will round down 15/4 = 3!

If you divide by a decimal, java will give you an exact answer 15/4.0 = 3.75

Page 27: Primitive Data Types. Identifiers What word does it sound like?

Careful How you Save

If you save a decimal in to an int, the decimal part of the number will be lost int x = 3.14;

Page 28: Primitive Data Types. Identifiers What word does it sound like?

Mod (%)

Remember how you learned division? 13/4 = 3R1

Mod just means give me the remainder from dividing.

13%4 = 1