Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

22
Primitive Types Primitive Types CSE 115 CSE 115 Spring 2006 Spring 2006 April 3 & 7 2006 April 3 & 7 2006
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    225
  • download

    3

Transcript of Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

Page 1: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

Primitive TypesPrimitive Types

CSE 115 CSE 115

Spring 2006Spring 2006

April 3 & 7 2006April 3 & 7 2006

Page 2: Primitive Types CSE 115 Spring 2006 April 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.

Page 3: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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.

Page 4: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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.

Page 5: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

Types of PrimitivesTypes of Primitives

NumbersNumbers BooleansBooleans CharactersCharacters

Page 6: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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

Page 7: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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

Page 8: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

Operations on Numbers Operations on Numbers (Binary)(Binary)

++ AdditionAddition

-- SubtractionSubtraction

** MultiplicationMultiplication

// DivisionDivision

%% ModulusModulus

Page 9: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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.

Page 10: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

More NumbersMore Numbers

Real/floating point numbersReal/floating point numbers floatfloat doubledouble

Operations: Same as for whole numbersOperations: Same as for whole numbers

Page 11: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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.

Page 12: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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;

Page 13: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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).

Page 14: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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

Page 15: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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

Page 16: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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

Page 17: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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.

Page 18: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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.

Page 19: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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!”;

Page 20: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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”

Page 21: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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;

Page 22: Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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.