2 1 data

18

Click here to load reader

Upload: ken-kretsch

Post on 14-Dec-2014

263 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 2 1  data

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

Page 2: 2 1  data

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

Page 3: 2 1  data

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”;

Page 4: 2 1  data

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;

}

}

Page 5: 2 1  data

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;

Page 6: 2 1  data

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;

Page 7: 2 1  data

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;

Page 8: 2 1  data

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';

Page 9: 2 1  data

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

Page 10: 2 1  data

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

Page 11: 2 1  data

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

Page 12: 2 1  data

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”;

Page 13: 2 1  data

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\".");

Page 14: 2 1  data

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

Page 15: 2 1  data

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

Page 16: 2 1  data

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

Page 17: 2 1  data

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();

Page 18: 2 1  data

Reading Streams

• Read the scanner, again// get the next token, hope it’s an integer

int i = scan.nextInt();

18