primitive types cse 115 spring 2006 april 3 & 7 2006
Post on 21-Dec-2015
225 views
TRANSCRIPT
Primitive TypesPrimitive Types
CSE 115 CSE 115
Spring 2006Spring 2006
April 3 & 7 2006April 3 & 7 2006
TypeType
A set of values and the operations we A set of values and the operations we can do with those values.can do with those values.
Object Types vs. Object Types vs. Primitive TypesPrimitive Types
We have been using object types all We have been using object types all semester.semester.
Primitive types are another type built into Primitive types are another type built into Java.Java.
The distinction between these two types The distinction between these two types has been significantly blurred by Java 5.has been significantly blurred by Java 5.
Primitive TypesPrimitive Types
Primitive Types Primitive Types ≠ Objects≠ Objects The way we create them is different.The way we create them is different. Primitive types have a value.Primitive types have a value.
Objects have instance variables.Objects have instance variables.
Primitive types have operations we can Primitive types have operations we can perform on them.perform on them. Objects have methods we can call on them.Objects have methods we can call on them.
Types of PrimitivesTypes of Primitives
NumbersNumbers BooleansBooleans CharactersCharacters
NumbersNumbers
Whole NumbersWhole Numbers byte – 1 byte (-128 to 127)byte – 1 byte (-128 to 127) short – 2 bytes short – 2 bytes int – 4 bytes int – 4 bytes (-2147483648 to 2147483647)(-2147483648 to 2147483647) long – 8 byteslong – 8 bytes
Operations on Numbers Operations on Numbers (Unary)(Unary)
++ Promotes a byte, short or char to an intPromotes a byte, short or char to an int
-- Unary negationUnary negation
++++ IncrementIncrement
---- DecrementDecrement
Operations on Numbers Operations on Numbers (Binary)(Binary)
++ AdditionAddition
-- SubtractionSubtraction
** MultiplicationMultiplication
// DivisionDivision
%% ModulusModulus
Important NoteImportant Note
In Java, operations on integers are In Java, operations on integers are closed. This means that an operation closed. This means that an operation performed on an integer returns an performed on an integer returns an integer, or likewise, an operation integer, or likewise, an operation performed on two integers returns an performed on two integers returns an integer.integer.
More NumbersMore Numbers
Real/floating point numbersReal/floating point numbers floatfloat doubledouble
Operations: Same as for whole numbersOperations: Same as for whole numbers
Important noteImportant note
Operations on two real numbers return a Operations on two real numbers return a real number.real number.
Operations on a real number and a whole Operations on a real number and a whole number return a real number.number return a real number.
How to get the “real” How to get the “real” answer to the division of answer to the division of two integers?two integers?
Typecasting – we can force Java to Typecasting – we can force Java to believe that we are doing floating point believe that we are doing floating point arithmetic.arithmetic.
int a = 1;int a = 1;
int b = 2;int b = 2;
double realAnswer = (double)1/2;double realAnswer = (double)1/2;
BooleansBooleans
Two-valued algebra systemTwo-valued algebra system TrueTrue FalseFalse
The boolean values in Java are The boolean values in Java are truetrue and and falsefalse (not 0 and 1). (not 0 and 1).
Operations that return Operations that return boolean valuesboolean values
<< Less thanLess than
>> Greater thanGreater than
<=<= Less than or equal toLess than or equal to
>=>= Greater than or equal toGreater than or equal to
==== EqualsEquals
!=!= Not equalsNot equals
Operations performed on Operations performed on booleans (Unary)booleans (Unary)
Negation (not)Negation (not) Symbol: !Symbol: !
Meaning:Meaning:
pp !p!p
truetrue falsefalse
falsefalse truetrue
Operations performed on Operations performed on booleans (Binary)booleans (Binary)
Conjunction (and)Conjunction (and) Symbol: &&Symbol: && Meaning:Meaning:
Disjunction (or)Disjunction (or) Symbol: ||Symbol: || Meaning:Meaning:
pp qq p && qp && q
truetrue truetrue truetrue
truetrue falsefalse falsefalse
falsefalse truetrue falsefalse
falsefalse falsefalse falsefalse
pp qq p || qp || q
truetrue truetrue truetrue
truetrue falsefalse truetrue
falsefalse truetrue truetrue
falsefalse falsefalse falsefalse
Short circuit boolean Short circuit boolean evaluationevaluation
Java supports short circuit boolean Java supports short circuit boolean evaluation. evaluation.
This means that if there is a false in a This means that if there is a false in a conjunction or a true in a disjunction, conjunction or a true in a disjunction, Java stops evaluating the expression and Java stops evaluating the expression and produces the result.produces the result.
CharactersCharacters
Single letter, digit, or symbolSingle letter, digit, or symbol Character literals are surrounded by ‘’ in Character literals are surrounded by ‘’ in
code:code:char aCharacter = ‘x’;char aCharacter = ‘x’;
Character class has methods of interest Character class has methods of interest when working with characters.when working with characters.
StringString
Not a primitive typeNot a primitive type Sequence of charactersSequence of characters String literals are surrounded by “” in String literals are surrounded by “” in
codecodeString greeting = “Hi there!”;String greeting = “Hi there!”;
String concatenationString concatenation
Can combine strings together using the + Can combine strings together using the + operator.operator.
String one = “light”;String one = “light”;String two = “house;String two = “house;String result = one + two;String result = one + two;
resultresult now has the value “lighthouse” now has the value “lighthouse”
String concatenationString concatenation
You can also combine a string with a primitive You can also combine a string with a primitive type.type.
int a = 5;int a = 5;
int b = 6;int b = 6;
int result = a * b;int result = a * b;
String output = “The result of String output = “The result of multiplying 5 and 6 is: ” + result;multiplying 5 and 6 is: ” + result;
String ClassString Class
There are many useful methods for There are many useful methods for String available in the String class.String available in the String class.