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 Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int numberOfStudents; float weight, height; long secondsPerLightYear; double expectedRateOfReturn; 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();

3 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

4 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

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

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

7 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

8 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 …)

9 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.

10 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.

11 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

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

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

14 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.

15 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

16 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

17 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.

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

19 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.

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

23 Relational Operators Gluuub … out to reality MakeBooleans.java
Chapter 6 Relational Operators < //less than <= //less than or equal to == //equal to != //not equal to > //greater than >= //greater than or equal to testScore < 80 testScore * 2 >= 350 30 < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius <= One very common error in writing programs is mixing up the assignment and equality operators. We frequently make a mistake of writing if (x = 5) ... when we actually wanted to say if (x == 5) ... Gluuub … out to reality MakeBooleans.java Intro to OOP w/Java--Wu

24 Boolean Expressions and Variables
Chapter 6 Boolean Expressions and Variables A B A && B A || B !A false true The AND operation results in true only if both A and B are true. The OR operation results in true if either A or B is true. The NOT operation is true if A is false and false if A is true. Combining boolean operators with relational and arithmetic operators, we can come up with a long boolean expression such as (x + 150) == y || x < y && !(y < z && z < x) In mathematics, we specify the range of values for a variable as 80  x < 90 In Java, to test that the value for x is within the specified lower and upper bounds, we express it as 80 <= x && x < 90 You cannot specify it as 80 <= x < 90 which is a syntax error because the relational operators (<, <=, etc.) are binary operators whose operands must be numerical values. Notice that the result of the subexpression 80 <= x is a boolean value, which cannot be compared to the numerical value 90. Their data types are not compatible. Bbbblarp … out to reality CombineBooleans.java Intro to OOP w/Java--Wu

25 Short-Circuit Evaluation
Chapter 6 Short-Circuit Evaluation Consider x > y || x > z This is evaluated left to right. If x > y is true, then there’s no need to evaluate x > z because the whole expression will be true whether x > z is true or not. To stop the evaluation once the result of the whole expression is known is called short-circuit evaluation. What would happen if the short-circuit evaluation is not done for the following expression? z == 0 || x / z > 20 Doodlingting … out to reality ShortCircuit.java Intro to OOP w/Java--Wu

26 Conditional Expressions
Chapter 6 Conditional Expressions Boolean values are used to form conditional expressions, whose value is determined by a Boolean expression. Lattteeee … out to reality Conditional.java Intro to OOP w/Java--Wu

27 Chapter 8 Characters In Java single characters are represented using the data type char. Character constants are written as symbols enclosed in single quotes, for example, 'a', 'X', and '5'. To represent characters in computer, U. S. computer manufacturers devised several schemes for encoding characters as integers. One coding scheme widely used today is ASCII (American Standard Code for Information Interchange). Intro to OOP w/Java--Wu

28 Chapter 8 ASCII Table For example, character 'O' is 79 (row value 70 + col value 9 = 79). O 9 70 Intro to OOP w/Java--Wu

29 Character Translation
Chapter 8 Character Translation Declaration and initialization char ch1, ch2 = ‘X’; Type conversion between int and char. System.out.println("ASCII of X is " + (int) 'X' ); System.out.println("Char 88 is " + (char)88 ); Erf … out to reality ASCIITranslate.java Intro to OOP w/Java--Wu

30 Character Comparison Spling … out to reality CharOrder.java
Chapter 8 Character Comparison This comparison returns true because ASCII value of 'A' is 65 while that of 'c' is 99. ‘A’ < ‘c’ Spling … out to reality CharOrder.java Intro to OOP w/Java--Wu

31 Special Characters These are also known as “escape characters”
Chapter 8 Special Characters These are also known as “escape characters” Written using a \ prefix \t \n \b \g \’ \\ Spling … out to reality EscapeCharacters.java Intro to OOP w/Java--Wu

32 The Character Class The Character class in the java.lang package includes methods for manipulating character values. Example, use isLetter to check if a character is a-z or A-Z Zzzznicka … out to reality … CharGames.java Zzzznicka … out to reality … CharGamesCompressed.java

33 Chapter 8 Unicode To accommodate the character symbols of non- English languages, the Unicode Consortium established the Unicode Worldwide Character Standard, commonly known as Unicode. Intro to OOP w/Java--Wu


Download ppt "Primitive and Reference Data Values"

Similar presentations


Ads by Google