Download presentation

Presentation is loading. Please wait.

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!), = (≥) logical operators – boolean operands, boolean result & (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 ? : must be boolean result is if is true result is if 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 = 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; 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 with 0x 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 Character in 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

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google