DOC PREVIEW
UB CSE 115 - Primitive Types

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 2006TypeType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. Primitive Object Types vs. Primitive TypesTypes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CharactersCharactersNumbersNumbers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 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 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 numbersImportant 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 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;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 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 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 =


View Full Document

UB CSE 115 - Primitive Types

Download Primitive Types
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Primitive Types and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Primitive Types 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?