Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
out of 10
Post on 21-Dec-2015
<ul><li> Slide 1 </li> <li> 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 </li> <li> Slide 2 </li> <li> 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) </li> <li> Slide 3 </li> <li> 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; </li> <li> Slide 4 </li> <li> 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? </li> <li> Slide 5 </li> <li> Operator Precedence precedence = the order in which operators are evaluated: () +, -, ! (unary) *, /, % +, -, = ==, != & | ^ && || on the same level, operators are evaluated left to right </li> <li> Slide 6 </li> <li> 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 </li> <li> Slide 7 </li> <li> 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 </li> <li> Slide 8 </li> <li> 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 </li> <li> Slide 9 </li> <li> 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 </li> <li> Slide 10 </li> <li> 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 </li> </ul>
View more >
CompSci 6 5.1 Primitive Types Primitive Types (base types) Built-in data types; native to most hardware Note: not objects (will use mostly first.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style.
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
Primitive Data Types Primitive Data Types: ahchinaei/courses/2010jan/icom4036/slides/11icom4036... ·…
Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references.
3.1 Data Types: Using Objects. Data type. Set of values and operations on those values. Primitive types. n “Built-in” for a language. Basic “building.
Java Data Types Everything is an Object Except Primitive Data Types For efficiency Platform independent Portable “slow” Objects are often.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
C++ Primitive Types C++ Basic Types and I/O cs2605/spring08/.../PDF/L02.C++ Structures OO Development I C++ Primitive Types 1 Standard C++ provides a plethora of primitive types. These store single values, and are
Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.