2 3 Variables A variable must be declared
2. 3 - Variables Ø A variable must be declared by specifying the variable's name and the type of information that it will hold • Data Types: int, double, char data type variable name int total; int count, temp, result; Multiple variables can be created in one declaration © 2006 Pearson Education 1
2. 3 – Variables Ø A variable can be given an initial value in the declaration int sum = 0; int base = 32, max = 149; Ø A variable can be given an initial value or a different value anywhere in the program Ø When a variable is used in a program, its current value is used © 2006 Pearson Education 2
Ø public class Variables { Ø public static void main (String[] args) { Ø int apple = 13; Ø System. out. println (“The number of apples is " + apple); Ø apple = 25; Ø System. out. println (“The number of apples is " + apple); Ø } Ø What happens if you put an int before apple = 25 ? Ø What happens if you don’t give an initial value? © 2006 Pearson Education 3
2. 3 - Assignment Operator (=)/Statement Ø An assignment statement changes the value of a variable Ø The assignment operator is the = sign int total; 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 just like in the Variables program. © 2006 Pearson Education 4
2. 3 - Constants Ø A constant is an identifier that is similar to a variable except that it holds one value while the program is active Ø The compiler will issue an error if you try to change the value of a constant during execution Ø In Java, we use final before the data type to declare a constant final int dozen = 12; © 2006 Pearson Education 5
2. 4 - Primitive Data Ø There are exactly eight primitive data types in Java Ø int : represents integers Ø double : represents floating point numbers Ø char : represents characters Ø boolean : represents characters (true or false) © 2006 Pearson Education 6
2. 4 - Numeric Primitive Data Ø The difference between the numeric primitive types is their size and the values they can store. Ø The int type stores only integer numbers while double includes a decimal place. Type Storage Min Value Max Value int 32 bits -2, 147, 483, 648 2, 147, 483, 647 double 64 bits +/- 1. 7 x 10308 with 15 significant digits © 2006 Pearson Education 7
2. 4 - Boolean Ø A boolean value represents a true or false condition Ø The reserved words true and false are the only valid values for a boolean type boolean done = false; © 2006 Pearson Education 8
2. 4 - Characters Ø A char variable stores a single character from the Unicode character set Ø A character set is an ordered list of characters, and each character corresponds to a unique number Ø Character literals are delimited by single quotes: 'a' 'X' '7' '$' ', ' 'n‘ Ø The Unicode character set has 65, 536 unique characters Ø It is an international character set, containing symbols and characters from many world languages © 2006 Pearson Education 9
2. 4 - Characters Ø The ASCII character set is older and smaller than Unicode, but is still quite popular Ø The ASCII characters are a subset of the Unicode character set, including: uppercase letters lowercase letters punctuation digits special symbols control characters © 2006 Pearson Education A, B, C, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, , … carriage return, tab, . . . 10
2. 5 - Arithmetic Expressions Ø What is an arithmetic expression? Ø An expression is a combination of one or more operands and their operators Ø Arithmetic expressions compute numeric results and make use of the arithmetic operators: Addition Subtraction Multiplication Division Remainder + * / % Ø If either or both operands associated with an arithmetic operator are floating point, the result is a floating point © 2006 Pearson Education 11
2. 5 - Division and Remainder Ø If both operands to the division operator (/) are integers, the result is an integer (the remainder is discarded) 14 / 3 equals? 4 8 / 12 equals? 0 Ø The remainder operator (%) returns the remainder after dividing 14 % 3 equals? 2 8 % 12 equals? 8 © 2006 Pearson Education 12
2. 5 - Operator Precedence Ø Operators can be combined into complex expressions result = total + count / max - offset; Ø How does PEMDAS work? Ø Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation Ø Arithmetic operators with the same precedence are evaluated from left to right Ø Parentheses can be used to force the evaluation order © 2006 Pearson Education 13
2. 5 - Operator Precedence Ø What is the order of evaluation in the following expressions? a + b + c + d + e 1 2 3 4 a + b * c - d / e 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1 © 2006 Pearson Education 14
2. 5 - Assignment Operator Revisited Ø The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated answer = 4 sum / 4 + MAX * lowest; 1 3 2 Then the result is stored in the variable on the left hand side © 2006 Pearson Education 15
2. 5 - Assignment Operator (=) Revisited Ø The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count = count + 1; Then the result is stored back into count (overwriting the original value) © 2006 Pearson Education 16
2. 5 - Data Conversions Ø Sometimes we might want to convert data from one type to another Ø Widening conversions are safest because they usually do not lose information (int to double) • Example: The integer 5 as a double is _____ Ø Narrowing conversions can lose information (double to int) • Example: The double 6. 7 as an integer is _____ © 2006 Pearson Education 17
2. 5 - Data Conversions In Java, data conversions can occur in three ways: Ø Assignment conversion occurs when a value of one type is assigned to a variable of another (only for widening conversions) Ø Arithmetic promotion happens automatically when operators in expressions convert their operands Ø Casting is when the type is put in parentheses in front of the value being converted © 2006 Pearson Education 18
2. 5 - Data Conversions Ø Assignment int total = 2; double result = total; Ø Arithmetic int total; double count; result 1 = total / count; result 2 = count / total; Ø Casting result = (double) total / count; © 2006 Pearson Education 19
Assignment Textbook Assignment • M. C. (2, 4 – 6) • T/F (4 - 7) • S. A. (6, 7) © 2006 Pearson Education 20
- Slides: 20