Primitive TypesTypeObject Types vs. Primitive TypesSlide 4Types of PrimitivesNumbersOperations on Numbers (Unary)Operations on Numbers (Binary)Important NoteMore NumbersImportant noteHow to get the “real” answer to the division of two integers?BooleansOperations that return boolean valuesOperations performed on booleans (Unary)Operations performed on booleans (Binary)Short circuit boolean evaluationCharactersStringString concatenationSlide 21String ClassPrimitive TypesPrimitive TypesCSE 115 CSE 115 Spring 2006Spring 2006April 3 & 7 2006April 3 & 7 2006TypeTypeA 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. Primitive Object Types vs. Primitive TypesTypesWe 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 TypesPrimitive Types Primitive Types ≠ Objects≠ ObjectsThe 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 PrimitivesNumbersNumbersBooleansBooleansCharactersCharactersNumbersNumbersWhole NumbersWhole Numbersbyte – 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 bytesOperations 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----DecrementDecrementOperations on Numbers Operations on Numbers (Binary)(Binary)++AdditionAddition--SubtractionSubtraction**MultiplicationMultiplication//DivisionDivision%%ModulusModulusImportant NoteImportant NoteIn 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 NumbersReal/floating point numbersReal/floating point numbersfloatfloatdoubledoubleOperations: Same as for whole numbersOperations: Same as for whole numbersImportant noteImportant noteOperations on two real numbers return a Operations on two real numbers return a real number.real number.Operations on a real number and a Operations on a real number and a whole number return a real number.whole 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;BooleansBooleansTwo-valued algebra systemTwo-valued algebra systemTrueTrueFalseFalseThe 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 equalsOperations performed on Operations performed on booleans (Unary)booleans (Unary)Negation (not)Negation (not)Symbol: !Symbol: !Meaning:Meaning:pp!p!ptruetruefalsefalsefalsefalsetruetrueOperations 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:ppqqp && qp && qtruetruetruetruetruetruetruetruefalsefalsefalsefalsefalsefalsetruetruefalsefalsefalsefalsefalsefalsefalsefalseppqqp || qp || qtruetruetruetruetruetruetruetruefalsefalsetruetruefalsefalsetruetruetruetruefalsefalsefalsefalsefalsefalseShort circuit boolean Short circuit boolean evaluationevaluationJava 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.CharactersCharactersSingle letter, digit, or symbolSingle letter, digit, or symbolCharacter 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.StringStringNot a primitive typeNot a primitive typeSequence of charactersSequence of charactersString literals are surrounded by “” in String literals are surrounded by “” in codecodeString greeting = “Hi there!”;String greeting = “Hi there!”;String concatenationString concatenationCan 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 concatenationYou 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 =
View Full Document