Download floating

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Data Types
BCIS 5110 Dr Andy Wu
Overview
Variables
Primitive data types



Data Types



Numeric types



Whole number types
Floating number types
Data type precision
Casting
Non-numeric types


boolean
char
Operators


2
Integer division
Variables
A variable holds one value at a time, but that value can change
as the program executes.
Before you can use it, you must declare a variable to give it:




A name (identifier)
A data type
Naming convention for variable names:




3
Use meaningful names.
The name of a variable (and method too) starts with a lowercase letter.
Then capitalize the first letter in all words after the first word.
Data Types
You must declare a data type for each variable.
Data type tells the compiler:





How much memory to allocate
How to store the data
The types of operations you will perform on the data
Compiler monitors use of data


4
Java is a strongly typed language
Variable Declaration
Variable Declarations take the following form:


DataType VariableName;
byte inches;
short month;
int speed;
long timeStamp;
float salesCommission;
double distance;
5
Variable Declaration
Two-Step Approach

type variableName; // Declare a variable
int scoreCounter;
variableName = value; // Assign a value to variable
scoreCounter = 1;
One-Step Approach

type variableName = value; // Declare a variable and
initialize it
double orderTotal = 95.0;
6
Variable Declaration
7
Variable Assignment
8
Variable Assignment
In order to store a value in a variable, an assignment statement
must be used.
The assignment operator is the equal (=) sign.
The operand on the left side of the assignment operator must be
a variable name.
The operand on the right side must be either a literal or
expression that evaluates to a type that is compatible with the
type of the variable.





Examples:
x = 20;
y = x;
9
Assign Value to Variable

X=X+1




10
Mathematically this is impossible (there is no solution for X).
Programmatically it’s totally fine; actually it’s used all the time.
It means taking the original value of X, adding one to it, and then
assigning the result back to the variable X.
Now X has a new value.
Data Type Length

If a variable were 2 bits long, the number of possible combinations of
0s and 1s contained in those two bits would be 22 = 4.


Likewise, for a variable that is n bits long, the number of possible
values to be able to fit into those bits is 2n.


When converted to decimal system, the range of the combinations is from 0
to 3 (0, 1, 2, and 3).
A range of these values can also be determined.
In practice, the measurement unit of the length is “byte” instead of
“bit” and 1 Byte = 8 Bits.

11
n-byte means 2(n*8) possible combinations.
Primitive Data Types

There are eight primitive data types in Java.


They are called “primitive” because they’re not objects.
The range and types of data that a data type can
accommodate depend on:



12
The length of the data type (how many bytes)
Whether it allows the decimal point
The type of data it allows, e.g, positive and negative, true or false.
Primitive Data Types
13
Numeric Data Types
14
Whole Number (Integer) Types




byte, short, int, and long are all integer data types.
They can hold whole numbers such as 5, 10, 23, 89, etc.
Integer data types cannot hold numbers that have a decimal
point in them.
Integers embedded into Java source code are called integer
literals.
15
Examples: byte and short

The byte data type is exactly 1 byte long.



It can accommodate 28 = 256 possible values (from -128 to 127).
The values are not from 0 to 256 because the high-order bit is used
for signs.
The short data type is 2 bytes long.


16
It can accommodate 216 = 65536 possible values.
They are integers ranging from -32768 to 32767.
Floating Point Types

Data types that allow fractional values are called floating-point
numbers.


1.7 and -45.316 are floating-point numbers.
In Java there are two data types that can represent floating-point
numbers.


17
float - also called single precision (7 decimal points).
double - also called double precision (15 decimal points).
Floating Point Types

The default type for floating point literals is double.


29.75, 1.76, and 31.51 are double data types.
A double value is not compatible with a float variable
because of its size and precision.
float number;
number = 23.5; // Error!

A double can be forced into a float by appending the
letter F or f to the literal.
float number;
number = 23.5F; // This will work.
18
Floating Point Types

Literals cannot contain embedded currency symbols or
commas.
grossPay = $1,257.00; // ERROR!
grossPay = 1257.00;
// Correct.

Floating-point literals can be represented in scientific
notation.


47,281.97 == 4.728197 x 104.
Java uses E notation to represent values in scientific
notation.

19
4.728197X104 == 4.728197E4.
Data Type Precision



Since short can hold a wider range of numbers, it is said to be
“more precise” than byte.
A short variable can contain, without problem, a value that could
be of the byte type.
But if you would want to store a short value in a byte variable,
you may or may not be able to store its exact value.

20
For example, if it is greater than 127, you might have to make the
compromise and just say everything in that range would be stored as
127. In other words, you lose “precision”.
Data Type Precision
short
long
byte
21
int
Casting


Conversion of data from one data type to another is called
casting.
Sometimes you may write statements that, without explicit
saying so, try or require converting a variable’s value from one
data type to another.

If your statement attempts to convert data from a less precise type to a
more precise type, Java will automatically do the conversion for you.



Otherwise, the conversion is one from a more precise type to a less
precise type and you will have to do it yourself.


22
Such a conversion is a widening conversion.
What Java does is an implicit cast.
Such a conversion is a narrowing conversion.
What you will have to do is an explicit cast.
Casting


The order of precision (from less
precise to more precise):
Syntax


(target type)
variableName or
expression
Casting is done before
arithmetic operations.
23
Data
Type
Length in
Bytes
Has
Fraction?
byte
1
No
Short
2
No
Int
4
No
Long
8
No
Float
4
Yes
Double
8
Yes
boolean Type

The Java boolean data type can have two possible values.



true
false
The value of a boolean variable may only be copied into a
boolean variable.
24
char Type


The Java char data type provides access to single
characters.
char literals are enclosed in single quote marks.


‘a’, ‘Z’, ‘\n’, ‘1’
Don’t confuse char literals with string literals.


25
char literals are enclosed in single quotes.
String literals are enclosed in double quotes.
Arithmetic Operators
26
Arithmetic Operators





The operators are called binary operators because they must have
two operands.
Each operator must have a left and right operator.
The arithmetic operators work as one would expect.
It is an error to try to divide any number by zero.
Be careful when working with two integer operands.
27
Integer Division




If your division operation involves two integers, it’s an “integer
division”.
Such division can be tricky.
The result of an integer division is different from that of a division
where the numbers are floating-point numbers (which is what we
normally see in our daily life, e.g., division done by a calculator).
In a Java program, what is the value of 1/2?





You might think the answer is 0.5…
But, that’s wrong.
The answer is simply 0.
And you get 2 from 5/2.
Integer division will truncate any decimal remainder.
28
Integer Division


The result of the integer division (dividend / divisor) shows how
many of “whole” divisors we can obtain from the dividend, until
what remains of the dividend is not enough to make a “whole”
divisor.
For example, we can obtain two entire 5s out of 12.




Therefore, 12 / 5 = 2 (but 12.0 / 5.0 = 2.4).
Since this is in fact a count, the result of integer division is an integer.
What is remaining is 2 ( 12 – 2 * 5 = 2); it is called the remainder.
The operation to figure out the remainder is “modulus”.

29
The remainder is also an integer.
Combined Assignment Operators



Java has some combined assignment operators.
These operators allow the programmer to perform an arithmetic
operation and assignment with a single operator.
Although not required, these operators are popular since they
shorten simple equations.
30
Combined Assignment Operators
31
Increment and Decrement

Increment (++)





Take the original value of the variable,
Add 1 to it,
Assign the result back to the variable.
In other words, X++ is the same as X = X+1
Decrement (--)




32
Take the original value of the variable,
Subtract 1 from it,
Assign the result back to the variable.
In other words, X-- is the same as X = X-1
Prefix vs. Postfix


When the entire statement contains only an
increment/decrement operation, the result will be the same
regardless of where you place the operators.
However, if the increment/decrement operation is only part of a
statement, it makes a difference whether you put the ++ (or --)
sign:

In front of the variable name (prefix)


Or behind the variable name (postfix)

33
The increment/decrement is done before the rest of the statement is executed.
The rest of the statement is executed before the increment/decrement is done.
Arithmetic Operators
34
Order of Precedence

Order of precedence






Increment and decrement
Positive and negative
Multiplication, division, and remainder
Addition and subtraction
Unless parentheses are used, the operations in an expression take
place from left to right in the order of precedence.
To change this, you can use parentheses.
35
Constants





Many programs have data that does not need to be changed.
Littering programs with literal values can make the program hard do
read and maintain.
Replacing literal values with constants remedies this problem.
Constants allow the programmer to use a name rather than a value
throughout the program.
Constants also give a singular point for changing those values when
needed.
36
Constants

A constant stores a value that cannot (or should not) be
changed.

The naming convention is to capitalize each word and have an
underscore(s) between words.
final dataType CONSTANT_NAME = value;
final double SALES_TAX_RATE = .0825d;
final float PI = 3.14159f;
37