Download Primitive Data Types

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
3
Data Types and Operators
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Objectives
After completing this lesson, you should be able to
do the following:
•
•
•
3-2
Describe the primitive data types
Declare and initialize primitive variables
Use operators to manipulate the value in a
primitive variable
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Overview
•
•
Java provides primitives for basic data types.
•
Declaring and initializing primitives is the basis for
building user-defined types.
3-3
Primitives provide the foundation for storing and
using information.
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Overview
•
•
Operators manipulate data and objects.
•
•
Java provides 44 different operators.
3-4
Operators take one or more arguments and
produce a value.
Some operators change the value of the operand.
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Variables
•
•
•
A variable is a basic unit of storage.
•
Variables may be initialized.
Variables must be explicitly declared.
Each variable has a type, an identifier, and a
scope.
Type
int myAge;
boolean isAMovie;
float maxItemCost = 17.98;
3-5
Identifier
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Variable Names
•
Variable names must start with a letter of the
alphabet, an underscore, or a $.
•
Other characters may include digits.
a
itemCost
3-6
item#Cost
item*Cost
itemCost2
2itemCost

item$Cost
•
item_Cost
_itemCost

item-Cost
abstract
Use meaningful names for variables, such as
customerFirstName, ageNextBirthday.
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Reserved Keywords
boolean
byte
char
double
float
int
long
short
void
false
null
true
3-7
abstract
final
native
private
protected
public
static
synchronized
transient
volatile
break
case
catch
continue
default
do
else
finally
for
if
return
switch
throw
try
while
class
extends
implements
interface
throws
import
package
instanceof
new
super
this
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Variable Types
•
Eight primitive data types:
– Six numeric types
– A char type, for characters
– A boolean type, for truth values
•
User-defined types
– Classes
– Interfaces
– Arrays
3-8
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Primitive Data Types
Integer
3-9
Floating
Point
Character
True
False
byte
short
int
long
float
double
char
boolean
1,2,3,42
07
0xff
3.0
.3337
4.022E23
'a'
'\141'
'\u0061'
'\n'
true
false
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Declaring Variables
•
The basic form of variable declaration:
type identifier [ = value]
public static void main(String[] args) {
int itemsRented;
float itemCost;
int i, j, k;
double interestRate;
}
•
3-10
Variables can be initialized when declared.
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Declaring Variables
•
Local variables are contained only within a method
or code block.
•
Local variables must be initialized before use.
class Rental {
private int instVar;
// instance variable
public void addItem() {
float itemCost = 3.50F; // local variable
int numOfDays = 3;
// local variable
}
}
3-11
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Numeric Literals
Integer literals
Floating-point
literals
3-12
0 1 42 -23795
02 077 0123
0x0 0x2a 0X1FF
(decimal)
(octal)
(hex)
365L
(long)
077L
0x1000L
1.0 4.2 .47
1.22e19 4.61E-9
6.2f 6.21F
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Nonnumeric Literals
Boolean literals
Character literals
String literals
3-13
true
false
'a' '\n'
'\u006F'
'\t'
'\077'
"Hello, world\n"
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Guided Practice: Declaring Variables
Find the mistakes in this code and fix them.
3-14
1
2
3
byte sizeof = 200;
short mom = 43;
short hello mom;
4
5
6
7
8
int big = sizeof * sizeof * sizeof;
long bigger = big + big + big
// ouch
double old = 78.0;
double new = 0.1;
boolean consequence = true;
9
10
11
boolean max = big > bigger;
char maine = "New England state";
char ming = 'd';
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Operators
Five types of operators:
• Assignment
• Arithmetic
• Integer bitwise
• Relational
• Boolean
3-15
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
The Assignment Operator
•
The expression on the right is assigned to the
variable on the left:
int var1 = 0, var2 = 0;
var1 = 50;
// var1 now equals 50
var2 = var1 + 10; // var2 now equals 60
•
The expression on the right is always evaluated
before the assignment.
•
Assignments can be strung together:
var1 = var2 = var3 = 50;
3-16
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Arithmetic Operators
•
•
Perform basic arithmetic operations
Work on numeric variables and literals
int a, b, c, d;
a = 2 + 2;
// addition
b
c
d
e
3-17
=
=
=
=
a
b
b
b
*
/
%
3;
2;
2;
2;
// multiplication
// subtraction
// division
// returns the remainder of division
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Arithmetic Operators
Most operations result in int or long:
•
byte, char, and short values are promoted to
int before the operation.
•
If either argument is long, the other is promoted
to long, and result is long.
byte b1 = 1, b2 = 2, b3;
b3 = b1 + b2;
// error: result is an int
// b3 is byte
3-18
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Conversions and Casts
•
Java automatically converts a value of one
numeric type to a larger type.
byte
•
short
int
long
Java does not automatically “downcast.”
byte
3-19
short
int
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
long
Increment and Decrement
•
The ++ operator increments by 1:
int var1 = 3;
var1++;
•
// var1 now equals 4
The ++ operator can be used in two ways:
int var1 = 3, var2 = 0;
var2 = ++var1;
// Prefix:
//
var2 = var1++;
// Postfix:
//
•
3-20
Increment var1
then assign to
Assign to var2
then increment
The -- operator decrements by 1.
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
first,
var2.
first,
var1.
Comparisons
Relational and equality operators:
>
>=
<
<=
==
!=
greater than
greater than or equal to
less than
less than or equal to
equal to
not equal to
int var1 = 7, var2 = 13;
boolean res = true;
res = (var1 == var2);
res = (var2 > var1);
3-21
// res now equals false
// res now equals true
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Logical Operators
Results of Boolean expressions can be combined by
using logical operators:
&&
||
^
!
&
|
and (with / without short-circuit evaluation)
or (with / without short-circuit evaluation)
exclusive or
not
int var0 = 0, var1 = 1, var2 = 2;
boolean res = true;
res = (var2 > var1) & (var0 == 3);
res = !res;
3-22
// now false
// now true
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Compound Assignment
An assignment operator can be combined with any
conventional binary operator:
double total=0, num = 1;
double percentage = .50;
…
total = total + num;
// total is now
total += num;
total -= num;
total *= percentage;
3-23
1
// total is now 2
// total is now 1
// total is now .5
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Operator Precedence
Order
1
2
3
4
5
6
7
8
9
10
11
12
13
3-24
Operators
Comments
++ -- + - ~
!(type)
* / %
+ - +
<< >> >>>
< > <= >=
instanceof
==
!=
&
^
|
&&
||
?:
= op=
Unary operators
R
Multiply, divide, remainder
Add, subtract, add string
Shift (>>> is zero-fill shift)
Relational, type compare
L
L
L
L
Equality
Bit/logical AND
Bit/logical exclusive OR
Bit/logical inclusive OR
Logical AND
Logical OR
Conditional operator
Assignment operators
L
L
L
L
L
L
R
R
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Assoc.
More on Precedence
•
Operator precedence determines the order in
which operators are executed:
int var1 = 0;
var1 = 2 + 3 * 4;
var1 = (2 + 3) * 4;
•
// var1 now equals 14
// var1 now equals 24
Operators with same precedence are executed
from left to right (see note in text below):
int var1 = 0;
var1 = 12 - 6 + 3;
var1 = 12 - (6 + 3);
•
3-25
// var1 now equals 9
// var1 now equals 3
Use parentheses to override default order.
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
String Concatenation
The + operator creates and concatenates strings:
String
String
String
name =
name = "Jane ";
lastName = "Hathaway";
fullName;
name + lastName;
// name is now
//"Jane Hathaway"
//
OR
name += lastName ;
// same result
fullName = name;
3-26
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS
Summary
In this lesson, you should have learned the following:
• Java has eight primitive data types.
• A variable must be declared before it can be used.
• Java provides a comprehensive set of operators.
• Explicit casting may be necessary if you use data
types smaller than int.
•
3-27
The + and += operators can be used to create and
concatenate strings.
QUICK JAVA COURSE FOR DIPLOMA PROJECT STUDENTS