Primitive and Reference Data Values Data Type reference

  • Slides: 34
Download presentation
Primitive and Reference Data Values Data Type reference primitive byte String short int long

Primitive and Reference Data Values Data Type reference primitive byte String short int long float char double Message. Box Applet Hi. Lo boolean primitive variables contain values Reference variables point at objects Input. Box etc.

Variable and Constant Data Values There are two types of data values: Account A

Variable and Constant Data Values There are two types of data values: Account A variable whose value can change over time. minimum balance 100. 00 account prefix 6427 A constant whose value must remain fixed over time. SV 129 Account current balance 908. 55 opening balance 100. 00 Constants are indicated by the final modifier Non-final public static data can give you warts • Ping … out to reality … Metric. Conversion. java

Numeric Data Types There are six numeric data types: byte, short, int, long, float,

Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int number. Of. Students; float weight, height; long seconds. Per. Light. Year; double expected. Rate. Of. Return;

Primitive Data Declaration and Assignments int first. Number, second. Number; first. Number = 234;

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

Assigning Numeric Data int number; number = 237; number = 35; number 35 237

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

Numeric Data Types At the time a variable is declared, it also can be

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 … Numeric. Variables. java

Data Type Precisions The six data types differ in the precision of values they

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

Numeric Literal Values Integer types can be Positive or negative Decimal, e. g. ,

Numeric Literal Values Integer types can be Positive or negative Decimal, e. g. , -5653 Octal, e. g. , 05653 (but not 09876) Hexidecimal, e. g. , 0 x 5 A 1 Long, e. g. , 14084591234 L Real types can be Float, e. g. , 1. 23 f Double, e. g. , -2. 34 Scientific, e. g. , 2. 17 e-27 f or -456. 2345 e 19

Arithmetic Operators The following table summarizes the arithmetic operators available in Java. Operation In

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

Arithmetic Expressions Examples: sum = first. Number + second. Number; avg = (one +

Arithmetic Expressions Examples: sum = first. Number + second. Number; avg = (one + two + three) / 3. 0; total++; Assignment operators, e. g. , += /= etc. Tral-la-la, out to reality … Numeric. Operators. java

Arithmetic Expressions How does the expression x + 3 * y get evaluated? Answer:

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.

Precedence Rules

Precedence Rules

Example a * (b + -(c / d) / e) * (f - g

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

Example Program Flonk … out to reality … Numeric. Precedence. java

Example Program Flonk … out to reality … Numeric. Precedence. java

Type Casting If x is a float and y is an int, what will

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.

Promotion Rules

Promotion Rules

Assignment Conversion If a lower precision value is assigned to a higher precision variable,

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

Explicit Type Casting Instead of relying on the promotion rules, we can make an

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.

Example Program Fuuuurtang … out to reality … Numeric. Casting. java

Example Program Fuuuurtang … out to reality … Numeric. Casting. java

Variable and Constant Data Values Account A variable whose value can change over time.

Variable and Constant Data Values Account A variable whose value can change over time. minimum balance 100. 00 A constant whose value must remain fixed over time. account prefix 6427 SV 129 Account current balance 908. 55 opening balance 100. 00 Constants are indicated by the final modifier Non-final public static data can give you warts Floorry … out to reality … Metric. Conversion. java

Constants We can change the value of a variable. If we want the value

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 = 3. 14159; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060; The reserved word final is used to declare constants. These are constants, also called named constant. These are called literal constant.

Use of Constants Very often class variables that are initialized May be private or

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 … Final. Variables. java

The Math Class The Math class in the java. lang package includes many common

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 … Math. Functions. java

Relational Operators < //less than <= //less than or equal to == //equal to

Relational Operators < //less than <= //less than or equal to == //equal to != //not equal to > //greater than >= //greater than or equal to test. Score < 80 test. Score * 2 >= 350 30 < w / (h * h) x + y != 2 * (a + b) 2 * Math. PI * radius <= 359. 99 Gluuub … out to reality Make. Booleans. java

Boolean Expressions and Variables A B A && B A || B !A false

Boolean Expressions and Variables A B A && B A || B !A false true false true true false Bbbblarp … out to reality Combine. Booleans. java

Short-Circuit Evaluation Consider x > y || x > z This is evaluated left

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 Short. Circuit. java

Conditional Expressions Boolean values are used to form conditional expressions, whose value is determined

Conditional Expressions Boolean values are used to form conditional expressions, whose value is determined by a Boolean expression. Lattteeee … out to reality Conditional. java

Characters In Java single characters are represented using the data type char. Character constants

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

ASCII Table 9 70 O For example, character 'O' is 79 (row value 70

ASCII Table 9 70 O For example, character 'O' is 79 (row value 70 + col value 9 = 79).

Character Translation char ch 1, ch 2 = ‘X’; Declaration and initialization System. out.

Character Translation char ch 1, ch 2 = ‘X’; Declaration and initialization System. out. println("ASCII of X is " + (int) 'X' ); System. out. println("Char 88 is " + (char)88 ); Type conversion between int and char. Erf … out to reality ASCIITranslate. java

Character Comparison ‘A’ < ‘c’ Spling … out to reality Char. Order. java This

Character Comparison ‘A’ < ‘c’ Spling … out to reality Char. Order. java This comparison returns true because ASCII value of 'A' is 65 while that of 'c' is 99.

Special Characters These are also known as “escape characters” Written using a  prefix

Special Characters These are also known as “escape characters” Written using a prefix t n b g ’ \ Spling … out to reality Escape. Characters. java

The Character Class The Character class in the java. lang package includes methods for

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

Unicode To accommodate the character symbols of non. English languages, the Unicode Consortium established

Unicode To accommodate the character symbols of non. English languages, the Unicode Consortium established the Unicode Worldwide Character Standard, commonly known as Unicode.