numeric data types there are six numeric data types: byte, short, int, long, float, and double....

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

Upload: jessica-lloyd

Post on 04-Jan-2016

232 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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,

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;

Page 2: 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,

Data Type Precisions

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

Page 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,

Primitive Data Declaration and Assignments

Code State of Memory

int firstNumber, secondNumber;firstNumber = 234;secondNumber = 87;

AAint firstNumber, secondNumber;int firstNumber, secondNumber;

BBfirstNumber = 234;firstNumber = 234;secondNumber = 87;secondNumber = 87;

int firstNumber, secondNumber;int firstNumber, secondNumber;firstNumber = 234;firstNumber = 234;secondNumber = 87;secondNumber = 87;

firstNumber

secondNumber

A. A. Variables are allocated in memory.

A. A. Variables are allocated in memory.

B. B. Values are assigned to variables.

B. B. Values are assigned to variables.

234

87

Page 4: 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,

Assigning Numeric Data

Code State of Memory

int number;int number;number = 237;number = 237;number = 35;number = 35; number

A. A. The variable is allocated in memory.

A. A. The variable is allocated in memory.

B. B. The value 237237 is assigned to numbernumber.

B. B. The value 237237 is assigned to numbernumber.

237

int number;int number;

number = 237;number = 237;

number = 35;number = 35;

AAint number;int number;

BBnumber = 237;number = 237;

CCnumber = 35;number = 35;

C. C. The value 3535 overwrites the

previous value 237.237.

C. C. The value 3535 overwrites the

previous value 237.237.

35

Page 5: 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,

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

Page 6: 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,

Numeric Literal Values

Integer types can be

Positive or negative

Decimal, e.g., -5653

Octal, e.g., 05653 (but not 09876)

Hexidecimal, e.g., 0x5A1

Long, e.g., 14084591234L

Real types can be

Float, e.g., 1.23f

Double, e.g., -2.34

Scientific, e.g., 2.17e-27f or -456.2345e19

Page 7: 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,

Arithmetic Operators

The following table summarizes the arithmetic operators available in Java.

++ and -- for integer type variables This is an integer division where the fractional part is truncated.

This is an integer division where the fractional part is truncated.

Page 8: 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,

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

Page 9: 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,

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.

Page 10: 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,

Precedence Rules

Page 11: 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,

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;

Page 12: 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,

Example Program

Flonk … out to reality … NumericPrecedence.java

Page 13: 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,

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.

Page 14: 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,

Promotion Rules

Page 15: 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,

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

Page 16: 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,

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 case x to float and then divide it by 3.

Type cast the result of the expression x / y * 3.0 to int.

Type cast the result of the expression x / y * 3.0 to int.

Page 17: 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,

Example Program

Fuuuurtang … out to reality … NumericCasting.java

Page 18: 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,

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;

These are constants, also called named constant.

These are constants, also called named constant.

The reserved word final is used to declare constants.

The reserved word final is used to declare constants.

These are called literal constant.

These are called literal constant.

Page 19: 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,

Use of Constants

Very often class variables that are initializedMay 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

Page 20: 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,

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