1 / 10

Primitive Types

Primitive Types. Java supports two kinds of types of values objects , and values of primitive data types variables store either references to objects (address in memory) or directly primitive values (bit patterns) bit patterns is interpreted according to the primitive type

Télécharger la présentation

Primitive Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Primitive Types • Java supports two kinds of types of values • objects, and • values of primitive data types • variables store • either references to objects (address in memory) • or directly primitive values (bit patterns) • bit patterns is interpreted according to the primitive type • primitive types • int, byte, short, long • integer numbers only different precision (32, 8, 16, 64 bits) • double, float • floating point numbers only different precision (64, 32 bits) • char • characters (letters, digits, special characters) • boolean • logical values true or false

  2. Operators • primitive types support operators • typically binary, but also unary and even ternary • used in expressions • arithmetic operators • numeric operands, result is the more precise number type • byte, short, int, long, float, double • +, -, *, /, % (modulo) • +, -(sign change, unary) • relational operators • primitive types operands, boolean result • == (=),!= (≠); (works also with objects!) • <,>,<= (≤),>= (≥) • logicaloperators • boolean operands, booleanresult • & (and), | (or)), ^ (xor) • && (and), || (or) • short circuit, i.e. second operand is evaluated only if necessary • ! (not, unary)

  3. Other Operators • + (String concatenation) • instanceof (tests whether an object has certain type) • <<,>>,>>>,~,&,|,^ • (bit pattern operations with numeric operands) • there are overloaded operators • e.g., +: unary sign, binary addition, String concatenation • operand type or usage decide meaning • ternary operator <op1> ? <op2> : <op3> • <op1> must be boolean • result is <op2> if <op1> is true • result is <op3> if <op1> is false • e.g., int minimum = i < j ? i : j;

  4. Assignment Operators • increment and decrement operators • x++; means x = x + 1; • x--; means x = x - 1; • there is also ++x and --x; don't use it! • most operators can be combined with an assignment • meaning: x <op>= y; means x = x <op> y • x += y; means x = x + y; • x -= y; means x = x - y; • x *= y; means x = x * y; • x /= y; means x = x / y; • x %= y; means x = x % y; • don't ever use assignment operators within an expression • Java allows is, but it's only source of errors • x++ + y; if different from ++x + y • x += y -= z; what the heck does it?

  5. Operator Precedence • precedence = the order in which operators are evaluated: • () • +, -, !(unary) • *, /, % • +, - • <,>,<=,>= • ==,!= • & • | • ^ • && • || • on the same level, operators are evaluated left to right

  6. Numeric Types • integer types • byte (8 bits) • short (16 bits) • int (32 bits) • long (64 bits) • result of division by zero is an error • overflow is not an error! • byte,short stored in one 32-bit word • long stored in two 32-bit words • long constants have L suffix, e.g., long m = 1000L; • hex constants start with0x prefix, e.g., byte hex = 0x2f3a; • floating point types • float (32 bits; 24 bits mantissa, 8 bits exponent) • double (48 bits mantissa, 16 bits exponent) • special value NaN (undefined) • result of division by zero is NaN • overflow is an error • float constants have F suffix, e.g., float x = 1000F; • Additional support in package java.lang • classes Byte,Short, Integer, Long, Float, Double

  7. Type char • represents characters • constants are enclosed in single quotes, e.g., char digit = '9'; • constants can be defined numerically with C suffix, e.g., char ch = 0C; • is ordered, but the order is crazy • first some special characters • then lowercase 'a'..'z' • then more special characters • then uppercase 'A'..'Z' • then other special characters.. • then digits '0'..'9' • then other characters • Unicode standard • 16 bits, but stored in one 32-bit word • supports most at languages • Chinese, Kanji, Hiragana, Katakana, Korean, Thai, Arabic, … • compatible with integer types • e.g., int lowercase = 'q'; • char uppercase = (char) (lowercase + 'A' - 'a'); • Additional support in class Characterin package java.lang

  8. Type boolean • two values true and false • operand ! (not) • ! true is true • ! false is false • operands &, && (and) • are both operands true? • true & true is true • true & false is false • false& true is false • false & false is false • also: only true & true is true • also: both operands must be true so that result is true • also: if one of the operands is false the result is false • &: evaluate both operands then compare • &&: evaluate first operand, if it's false then don't evaluate second • only if it's true evaluate second operand and compare

  9. Type boolean • operands |, || (or) • is at least one operand true? • true | true is true • true | false is true • false| true is true • false | false is false • also: only false | false is false • also: both operands must be false so that result is false • also: if one of the operands is true the result is true • |: evaluate both operands then compare • ||: evaluate first operand, if it's true then don't evaluate second • only if it's false evaluate second operand and compare • operands ^ (xor) • are both operands different? • true ^ true is false • true ^ false is true • false^ true is true • false ^ false is false • also: both operands must have the same value so that result is false • also: operands must have different values so that result is true

  10. Use of boolean • computation with logical values • variables of type boolean • e.g. boolean done = false; • to return result from a method • asking existence, e.g., hasOutline() • asking for truth of a property isVisible() • conditional decisions • if a condition is true do something • otherwise do something else • repeat doing something while a condition is true • special branching and looping statements • if statement • if-else statement • switch statement • while statement • for statement

More Related