Presentation is loading. Please wait.

Presentation is loading. Please wait.

Primitive and Reference Data Values

Similar presentations


Presentation on theme: "Primitive and Reference Data Values"— Presentation transcript:

1 Primitive and Reference Data Values
byte short int double long float boolean String Applet MessageBox HiLo InputBox etc. char primitive reference Data Type A constant data value is represented graphically by a lock icon to convey the fact that the value is locked and cannot be changed. primitive variables contain values Reference variables point at objects

2 Variable and Constant Data Values
Account SV129 Account A variable whose value can change over time. minimum balance 100.00 current balance 908.55 A constant whose value must remain fixed over time. account prefix 6427 opening balance 100.00 Constants are indicated by the final modifier Non-final public static data can give you warts Floorry … out to reality … MetricConversion.java A constant data value is represented graphically by a lock icon to convey the fact that the value is locked and cannot be changed.

3 Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne, numberTwo; long bigInteger; double bigNumber; In the same way that you can initialize variables at the time you declare them, you can declare and create an object at the same time. For example, the declaration MainWindow mainWindow = new MainWindow(); is equivalent to MainWindow mainWindow; mainWindow = new MainWindow();

4 Primitive Data Declaration and Assignments
int firstNumber, secondNumber; firstNumber = 234; secondNumber = 87; firstNumber secondNumber A. Variables are allocated in memory. B. Values are assigned to variables. 234 87 A int firstNumber, secondNumber; int firstNumber, secondNumber; firstNumber = 234; secondNumber = 87; B firstNumber = 234; secondNumber = 87; Code State of Memory

5 Assigning Numeric Data
int number; number = 237; number = 35; number A. The variable is allocated in memory. B. The value 237 is assigned to number. 237 C. The value 35 overwrites the previous value 237. 35 int number; number = 237; number = 35; A int number; B number = 237; C number = 35; See the note page on the next slide. Code State of Memory

6 Numeric Data Types At the time a variable is declared, it also can be initialized. For example, we may initialize the integer variables count and height to 10 and 34 as int count = 10, height = 34; Ping … out to reality … NumericVariables.java In the same way that you can initialize variables at the time you declare them, you can declare and create an object at the same time. For example, the declaration MainWindow mainWindow = new MainWindow(); is equivalent to MainWindow mainWindow; mainWindow = new MainWindow();

7 Data Type Precisions The six data types differ in the precision of values they can store in memory.

8 Numeric Literal Values
Integer types can be Positive or negative Decimal, e.g., -5653 Octal, e.g., (but not 09876) Hexidecimal, e.g., 0x5A1 Long, e.g., L Real types can be Float, e.g., 1.23f Double, e.g., -2.34 Scientific, e.g., 2.17e-27f or e19

9 Arithmetic Operators The following table summarizes the arithmetic operators available in Java. Operation In Java Example int x = 23; int y= 5; double z = 5.2; Addition + x + y 28 Subtraction - x - y 18 Multiplication * x * y 115 Integer division / x / y 4 Real division x / z 4.423 Modulo % x % y 3 Increment ++ x++ OR ++x x = 24 (but …) Decrement -- x-- OR --x x = 22 (but …)

10 Arithmetic Expressions
Examples: sum = firstNumber + secondNumber; avg = (one + two + three) / 3.0; total++; Assignment operators, e.g., += /= etc. Tral-la-la, out to reality … NumericOperators.java You need to write the expression as (x + 3) * y if you want to multiply y to the sum of x and 3.

11 Arithmetic Expressions
How does the expression x + 3 * y get evaluated? Answer: x is added to 3*y. We determine the order of evaluation by following the precedence rules. A higher precedence operator is evaluated before the lower one. If two operators are the same precedence, then they are evaluated left to right for most operators. You need to write the expression as (x + 3) * y if you want to multiply y to the sum of x and 3.

12 Precedence Rules -x(y + z) 4w
How would be the result of the following expression? 4 * 3 / 2 + 5 Express the following formula in Java. -x(y + z) 4w

13 Example a * (b + -(c / d) / e) * (f - g % h)
a * -b - (d + e * f) * (-g + h) a = 10; b = 5; c = 23; d = 4; e = 2; f = 5; g = 8; h = 3;

14 Example Program Flonk … out to reality … NumericPrecedence.java

15 Type Casting If x is a float and y is an int, what will be the data type of the following expression? x * y The answer is float. The above expression is called a mixed expression. The data types of the operands in mixed expressions are converted based on the promotion rules. The promotion rules ensure that the data type of the expression will be the same as the data type of an operand whose type has the highest precision. When an arithmetic expression consists of variables and constants of the same data type, then the result of the expression will be that data type also. When the data types of variables and constants in an arithmetic expression are of different data types, then a casting conversion will take place. A casting conversion, or type casting, is a process that converts a value of one data type to another data type. Two types of casting conversions in Java are implicit and explicit. An implicit conversion called numeric promotion is applied to the operands of an arithmetic operator.

16 Promotion Rules -x(y + z) 4w
How would be the result of the following expression? 4 * 3 / 2 + 5 Express the following formula in Java. -x(y + z) 4w

17 Assignment Conversion
If a lower precision value is assigned to a higher precision variable, type casting occurs Example double number; number = 25; is legal but int number; number = 23.45; is not

18 Explicit Type Casting Instead of relying on the promotion rules, we can make an explicit type cast by prefixing the operand with the data type using the following syntax: ( <data type> ) <expression> Example (float) x / 3 (int) (x / y * 3.0) Type case x to float and then divide it by 3. Type cast the result of the expression x / y * 3.0 to int.

19 Example Program Fuuuurtang … out to reality … NumericCasting.java

20 Constants We can change the value of a variable. If we want the value to remain the same, we use a constant. final double PI = ; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060; These are constants, also called named constant. The reserved word final is used to declare constants. These are called literal constant. The data type of literal constant 3.45 is double and the data type of literal constant 398 is int. To designate other data types for literal constants, we suffix the literal constants with the designators F or f for float and L or l for long. For example, 2.45f 1453L

21 Use of Constants Very often class variables that are initialized
May be private or public Local and instance variables may be declared final so they can be assigned a value only once Yabbadabba … out to reality … FinalVariables.java When an arithmetic expression consists of variables and constants of the same data type, then the result of the expression will be that data type also. When the data types of variables and constants in an arithmetic expression are of different data types, then a casting conversion will take place. A casting conversion, or type casting, is a process that converts a value of one data type to another data type. Two types of casting conversions in Java are implicit and explicit. An implicit conversion called numeric promotion is applied to the operands of an arithmetic operator.

22 The Math Class The Math class in the java.lang package includes many common and useful mathematical functions such sin, cos, tan, square root, exponentiation, and others. The mathematical formula is expressed in Java as Math.abs( Math.sin( Math.PI / 4.0) * x ) Splodge … out to reality … MathFunctions.java


Download ppt "Primitive and Reference Data Values"

Similar presentations


Ads by Google