2 1 data
DESCRIPTION
TRANSCRIPT
Primitive Data Types
• Represent single values
• Practically unlimited access
• Integers types:• 2010• -40
• Floating point (decimals) numbers types:– Big integers
– Decimals: 3.14159
– Scientific notation: 6.03e23
• Boolean type: true or false
• Character type: ‘c’
• Strings: “Vote for Pedro”1
2
Variables
• To store or remember data Java uses variables. • Variables contain primitive values or object
references.• To be used, variables have to be declared and set• Declaring a variable:
– Announces is existence to the rest of the block
– Sets aside a section of memory big enough to store the value
– Determines what kind of value can be stored in the variable
– Determines how the number is to be interpreted
3
Variables
• Variable names should be describe the value they hold• Names with multiple words:
– Lower case first word, capitalize subsequent words
– Separate words with underscore.
• Declaring a variable:– Type name followed by the name and optional initialization
int studentAge = 18;
double pi = 3.14159;
boolean isValid = false;
char marks_the_spot = ‘x’;
String vote4 = “Pedro”;
4
Variable Scope
• Variables are known inside the block in which they are declared.class Foo {
int x = 0;
void someMethod() {
int y;
y = x + 1;
}
void anotherMethod() {
x = y + 1;
}
}
5
Integers
• Integers are 32 bits long• -2,147,483,648 <= int <= 2,147,483,647• Example declarations and values
int m;
m = 0;
int n = 23;
int o = -40;
6
Floating Point
• The type is called double• 64 bits long
– Some bits used for the exponent, some for the mantissa
• +/- 1.7 x 10±308 with 15 significant digits
double pi;
pi = 3.14159;
double x = 10.0;
double y = 6.02e23;
double z = -40;
7
Boolean
• A boolean value represents true or false
• A boolean also can be used to represent any value with two states, such as a light bulb being on or off.
• The reserved words true and false are the only valid values for a boolean typeboolean done = false;
boolean isMale = true;
8
Characters
• A char variable stores the numeric representation of a character
• The Unicode character set lists the characters and their corresponding numeric representation– The Unicode character set uses sixteen bits per character,
allowing for 65,536 unique symbols and characters from many world languages
• Character literals are delimited by single quotes:'a' 'X' '7' '$' ',' '\n‘
• char marksTheSpot = ‘X';
9
Escape Sequences
• The unicode set recognizes ‘characters’ that are not printable
• These unprintable characters are referred to with escape sequences
EscapeSequence
\b\t\n\r
Unprintable character
backspacetabnewlinecarriage return
10
Escape sequences
• Printing unprintable characters moves the terminal window cursor– Printing a backspace character moves the cursor
backwards on the same line
– Printing a carriage return moves the cursor to the front of the current line
– Printing a new line drops the cursor to the beginning of the next line
– Print a tab moves the cursor to ahead to the next tab stop
11
Escape sequences
• Special characters must be escaped when using them as just a character
char singleQuote = ‘\’’;
char doubleQuote = ‘\”’;
char backslash = ‘\\’;
• In other words, the back-slash tells Java to ignore the normal meaning of the very next character
12
Character Strings
• A character string is a sequence of characters delimited by double-quotes:
“To be, or not to be”
– (Actually, character strings are not a primitive data type, but an object.)
String eagles;
eagles = “Hell, no!!”;
String ssn = “147431904”;
13
Escape Sequences
• What if we wanted to print a double quote character?System.out.println ("I said "Hello".");
• An escape sequence is a series of characters that represents a special character
• An escape sequence begins with a backslash character (\), which indicates that the character(s) that follow should be treated in a special way:System.out.println ("I said \"Hello\".");
14
Constants
• A constant is a variable that can only be assigned a value once.
• The final modifier to declares a constantfinal int MIN_HEIGHT = 60;
final int MAX_HEIGHT;
…MAX_HEIGHT = 74;
• Constants:– give names to otherwise unclear literal values
– facilitate updates of values used throughout a program
– prevent inadvertent attempts to change a value
15
Assignment
• An assignment statement changes the value of a variable• The assignment operator is the = sign
total = 55;
• The expression on the right is evaluated and the result is stored in the variable on the left
• The value that was in total is overwritten (and lost)
• You can only assign a value to a variable that is consistent with the variable's declared type
16
Keyboard input
• Reads typing from the console– Terminal window or DOS window
• Based on the concept of the token– Tokens are string or printable characters separated by
delimiters– E.g., “Vote for Pedro” has three tokens separated by a
white-space delimiter.– E.g., “09/15/2010” has three tokens separated by a slash
delimiter
17
Reading Streams
• The scanner object extracts tokens from the input stream.
• Input stream can be the keyboard, a file, or a socket.• For example, creating the scanner
// Create a variable of type Scanner
// Connect the scanner to the keyboard
Scanner scan = new Scanner(System.in);
• Read the scannerString data = scan.nextLine();
Reading Streams
• Read the scanner, again// get the next token, hope it’s an integer
int i = scan.nextInt();
18