primitive data types. identifiers what word does it sound like?
TRANSCRIPT
![Page 1: Primitive Data Types. Identifiers What word does it sound like?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/1.jpg)
Primitive Data Types
![Page 2: Primitive Data Types. Identifiers What word does it sound like?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/2.jpg)
Identifiers
What word does it sound like?
![Page 3: Primitive Data Types. Identifiers What word does it sound like?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/3.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/4.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/5.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/6.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/7.jpg)
Java is Case Sensitive
Person ≠ person ≠ perSon
![Page 8: Primitive Data Types. Identifiers What word does it sound like?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/8.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/9.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/10.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/11.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/12.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/13.jpg)
Invalid Numbers
Don’t do this $5.06 #3.0 86%
![Page 14: Primitive Data Types. Identifiers What word does it sound like?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/14.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/15.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/16.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/17.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/18.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/19.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/20.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/21.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/22.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/23.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/24.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/25.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/26.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/27.jpg)
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?](https://reader035.vdocument.in/reader035/viewer/2022062722/56649f2a5503460f94c43eca/html5/thumbnails/28.jpg)
Mod (%)
Remember how you learned division? 13/4 = 3R1
Mod just means give me the remainder from dividing.
13%4 = 1