Primitive Types CSE 115 Spring 2006 April 3 & 7 2006

advertisement
Primitive Types
CSE 115
Spring 2006
April 3 & 7 2006
Type
 A set of values and the operations we
can do with those values.
Object Types vs. Primitive
Types
 We have been using object types all
semester.
 Primitive types are another type built into
Java.
 The distinction between these two types
has been significantly blurred by Java 5.
Primitive Types
 Primitive Types ≠ Objects
 The way we create them is different.
 Primitive types have a value.
 Objects have instance variables.
 Primitive types have operations we can
perform on them.
 Objects have methods we can call on them.
Types of Primitives
 Numbers
 Booleans
 Characters
Numbers





Whole Numbers
byte – 1 byte (-128 to 127)
short – 2 bytes
int – 4 bytes (-2147483648 to 2147483647)
long – 8 bytes
Operations on Numbers
(Unary)
+
 Promotes a byte, short or char to an int
 Unary negation
++
 Increment
- Decrement
Operations on Numbers
(Binary)
+
 Addition
 Subtraction
*
 Multiplication
/
 Division
%
 Modulus
Important Note
 In Java, operations on integers are
closed. This means that an operation
performed on an integer returns an
integer, or likewise, an operation
performed on two integers returns an
integer.
More Numbers
 Real/floating point numbers
 float
 double
 Operations: Same as for whole numbers
Important note
 Operations on two real numbers return a
real number.
 Operations on a real number and a whole
number return a real number.
How to get the “real” answer
to the division of two
integers?
 Typecasting – we can force Java to
believe that we are doing floating point
arithmetic.
int a = 1;
int b = 2;
double realAnswer = (double)1/2;
Booleans
 Two-valued algebra system
 True
 False
 The boolean values in Java are true
and false (not 0 and 1).
Operations that return
boolean values
<
 Less than
>
 Greater than
<=
 Less than or equal to
>=
 Greater than or equal to
==
 Equals
!=
 Not equals
Operations performed on
booleans (Unary)
 Negation (not)
 Symbol: !
 Meaning:
p
!p
true
false
false
true
Operations performed on
booleans (Binary)
 Conjunction (and)
 Symbol: &&
 Meaning:
p
true
true
false
false
q
true
false
true
false
p && q
true
false
false
false
 Disjunction (or)
 Symbol: ||
 Meaning:
p
true
true
false
false
q
true
false
true
false
p || q
true
true
true
false
Short circuit boolean
evaluation
 Java supports short circuit boolean
evaluation.
 This means that if there is a false in a
conjunction or a true in a disjunction,
Java stops evaluating the expression and
produces the result.
Characters
 Single letter, digit, or symbol
 Character literals are surrounded by ‘’ in
code:
char aCharacter = ‘x’;
 Character class has methods of interest
when working with characters.
String
 Not a primitive type
 Sequence of characters
 String literals are surrounded by “” in
code
String greeting = “Hi there!”;
String concatenation
 Can combine strings together using the +
operator.
String one = “light”;
String two = “house;
String result = one + two;
 result now has the value “lighthouse”
String concatenation
 You can also combine a string with a primitive
type.
int a = 5;
int b = 6;
int result = a * b;
String output = “The result of
multiplying 5 and 6 is: ” + result;
String Class
 There are many useful methods for
String available in the String class.
Download