230 likes | 393 Views
Lec . 02: Data Types and Operators (1/2). Content. Data types Literals Variable declaration and initialization Scope rules Operators Expressions Type conversion and casting Wrapper classes for primitive types. Data types. A data type is a collection of two sets:
E N D
Lec. 02: Data Types and Operators(1/2) Java Programming
Content • Data types • Literals • Variable declaration and initialization • Scope rules • Operators • Expressions • Type conversion and casting • Wrapper classes for primitive types Java Programming
Data types • A data type is a collection of two sets: • Value set – all the possible and legal values of the data type • Operation set – all the possible and legal operations applicable on the values in the value set • Example: Integer data type • Value set: { …, -2, -1, 0, 1, 2, …} • Operation set: {+, –, *, /, mod} • In computer, the data type determines how to represent a value, such as the encoding, and how many bytes needed. Java Programming
Java data types • Two categories of Java data types • Object-oriented • Non-object-oriented • Object-oriented • Each class is a data type. • Object-oriented types are designed by programmers to represent problem-oriented types, such as the type for student records, also called user-defined data type. • All the possible states of a class object form the value set. • All the methods defined in a class form the operation set. • Non-object-oriented • Java provides 8 primitive types. • Each primitive type is associated with a specified value range and set of operations. Java Programming
Size and range of primitive types Java Programming
Integral types • Java provides four integral types for integers, including both positive and negative. • Four integral types • byte • short • int • long • The difference among these four types is the number of bytes used to store an integer and the ranges. Java Programming
Floating types • Java provides two types for real numbers , including both positive and negative. • Two types of floating types • float • double • The difference between these two types is the number of bytes used to store a real number and the ranges. Java Programming
Characters • Java uses Unicode to represent characters. • Unicode defines a encoding for character set that can represent all of the characters found in all human languages. • In Java, char is an unsigned 16-bit type having a range of 0 to 65,535. • The standard 8-bit ASCII character set is a subset of Unicode and ranges from 0 to 127. • Since char is an unsigned 16-bit type, it is possible to perform various arithmetic manipulations on a char variable. • Example • The following program segment prints Y char ch; ch = ‘X’; System.out.print(++ch); Java Programming
Boolean • The boolean type represents true/false values. • Java defines the values true and false using the reserved words true and false. Java Programming
Demo program for using boolean class BoolDemo { public static void main(String args[]) { boolean b; b = false; System.out.println("b is " + b); b = true; System.out.println("b is " + b); if(b)System.out.println("This is executed."); b = false; if(b) System.out.println("This is not executed."); System.out.println("10 > 9 is " + (10 > 9)); } } Java Programming What is the result if you remove the parentheses of (10 > 9) ?
Literals • In Java, literals refer to fixed values that are represented in their human-readable form. • A literal is a string representing a value by itself. • The literal forms of different types are distinct, except for byte, short and int. Java Programming
Literal table Java Programming
Literal table Java Programming
More on literals • By default, integer literals are of type int. • If you want to specify a long literal, append an l or an L, e.g. 12 is an int, but 12L is a long. • Although integer literals create an int value by default, they can still be assigned to variables of type char, byte, or shortas long as the value being assigned can be represented by the target type. • byte b; b= 12; are legal. • byte b; b= 128; are illegal. • Beginning with JDK 7, you can embed one or more underscores into an integer or floating point literal. • Example: 123_45_1234 is equal to 123451234 • By default, floating-point literals are of type double. • If you want to specify a float literal, append an f or an F, e.g. 12.5 is a double, but 12.5F is a float. Java Programming
Hexadecimal, octal and binary forms • In Java, an integer beginning with 0 is a octal number(八進位). • 011 represents a octal number equal to 910. • 081 is illegal. • In Java, an integer beginning with 0x or 0X is a hexadecimal number(十六進位). • 0x11 represents a hexadecimal number equal to 1710. • 0xG1 is illegal. • Beginning with JDK 7, an integer beginning with 0b or 0B is a binary number(二進位). • 0b11 represents a hexadecimal number equal to 310. • 0b21 is illegal. Java Programming
Character escape sequences(跳脫字元) • Java provides escape sequences, also referred to as backslash character constants, to represent some characters which are not printable or have special meanings, such as the carriage return, the single and double quotes. Java Programming
Table of Character Escape Sequences Java Programming
String literals • A string is a sequence of zero or more characters enclosed by double quotes. • In Java, strings are represented by a standard built-in class String. • In Java, a string literal is a sequence of characters enclosed by a pair of double quotes. • Example • Legal: "", "a", "abc123", "a\"hello", "a\t\uA123hello“ • Illegal: ‘X’, "a"" Java Programming
Variables • A variable is a named memory space used to save value. • All variables in Java must be declared prior to their use by using the following form. <type> <variable_name>; • The capabilities of a variable are determined by its type. • The type of a variable cannot be changed once it is declared. • There are 4 types of variables • Class fields • Instance fields • Local variables • Parameters Java Programming
Variables • Initializing variables byte count = 10; // give count an initial value of 10 char ch = 'X'; // initialize ch with the letter X float f = 1.2F; // f is initialized with 1.2 int a, b = 8, c = 19, d; // b and c have initializations double radius = 4, height = 5; double volume = 3.1416 * radius * radius * height; //dynamic Java Programming
Scope rules of variables • Scope rules defines the association between the use of a variable and the declaration of a variable. • It determines what variables are visible to other parts of your program and the lifetime of variables. • Java allows variables to be declared within any code block. • Remember that the definitions of classes and methods are all code block. • A block defines a scope. • A variables declared inside a scope are NOT visible (that is, accessible) to codes which are defined outside that scope. • For nested scopes, a variable declared in a outer scope will be visible to codes within inner scopes. • Nested scopes mean a scope is enclosed by another scope, such as a method’s scope is enclosed by a class’ scope. Java Programming
Example for scope rules class ScopeDemo { public static void main(String args[]) { int x; // known to all code within main x = 10; if(x == 10) { // start new scope int y = 20; // known only to this block // x and y both known here. // x cannot be re-defined here System.out.println("x and y: " + x + " " + y); x = y * 2; } // y = 100; // Error! y not known here // x is still known here. System.out.println("x is " + x); } } Java Programming
Lifetime of variables • The lifetime of a variable describes if there is a memory space associated with the variable. • The variables are created, associated with memory space, when their scope is entered/activated, and destroyed, disassociated with memory space, when their scope is left/inactivated. • Variables declared within a method will not hold their values between calls to that method. Java Programming