Download Primitive Data Types

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Location arithmetic wikipedia , lookup

Arithmetic wikipedia , lookup

Law of large numbers wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
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
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])
Example Identifiers
 Are these okay?
 myPerson
 m_person
 person1
 my
Person
 1person
 person#1
Example Identifiers
 These are fine
 myPerson
 m_person
 person1
 These are NOT
 my Person
 1person
 person#1
Java is Case Sensitive
 Person ≠ person ≠ perSon
Good Identifiers
 Don’t use single letters
 Make

them descriptive
grades instead of g
 Variable names should be meaningful
but concise
 gpa
instead of
gradePointAverageForStudentsAtThisScho
ol
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
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?
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.
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’
Invalid Numbers
 Don’t do this
 $5.06
 #3.0
 86%
You Might Also see
 long and short are like int
 float is like double
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.
Ascii
 The characters are secretly stored as
integer values. Thus ascii value 65 is
the captial ‘A’
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
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 = 5
letter = E
average = 3.95
done = false
The End!
What does + mean?
 Inside
+
System.out.println("number = " + number);
means add the words “number = “ to the
value of number
Escape Characters
Character
Java Escape Sequence
Newline
Horizontal tab
Backslash
Single quote
Double quote
Null character
'\n'
'\t'
'\\'
'\''
'\"'
'\0'
System.out.println(“This is a\ntest and only\’ a test.”);
Run output:
This is a
test and only’ a test.
Interesting Differences
System.out.println( 2 + 2);
//Output: 4
System.out.println(“2 + 2”);
//Output: 2 + 2
System.out.println(“2” + “2”);
//Output: 22
Casting
char 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 = A
its ASCII value = 65
ASCII value 75 = K
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?
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;
You can do math
 a = 5 + 3;
 Adding
is +
 Subtracting is –
 Multiplication is *
 Division is /
 Modulus is %
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
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;
Mod (%)
 Remember how you learned division?
 13/4
= 3R1
 Mod just means give me the remainder
from dividing.
 13%4 = 1