Presentation is loading. Please wait.

Presentation is loading. Please wait.

L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall. 2014 0 Java Programming.

Similar presentations


Presentation on theme: "L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall. 2014 0 Java Programming."— Presentation transcript:

1 L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall. 2014 0 Java Programming

2 C ONTENT  Data types  Literals  Variable declaration and initialization  Scope rules  Operators  Expressions  Type conversion and casting  Wrapper classes for primitive types Fall. 2014 1 Java Programming

3 D ATA 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. Fall. 2014 2 Java Programming

4 J AVA 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. Fall. 2014 3 Java Programming

5 S IZE AND RANGE OF PRIMITIVE TYPES Fall. 2014 4 Java Programming Primitive data type Length in bits Ranges booleanN/Atrue and false char1616-bit Unicode characters byte8-2 7 ~ 2 7 -1 short16-2 15 ~ 2 15 -1 int32-2 31 ~ 2 31 -1 long64-2 63 ~ 2 63 -1 float32Based on IEEE 754 double64Based on IEEE 754

6 I NTEGRAL 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. Fall. 2014 5 Java Programming

7 F LOATING 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. Fall. 2014 6 Java Programming

8 C HARACTERS  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); Fall. 2014 7 Java Programming

9 B OOLEAN  The boolean type represents true/false values.  Java defines the values true and false using the reserved words true and false. Fall. 2014 8 Java Programming

10 D EMO 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)); } Fall. 2014 9 Java Programming What is the result if you remove the parentheses of (10 > 9) ?

11 L ITERALS  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. Fall. 2014 10 Java Programming

12 L ITERAL TABLE Fall. 2014 11 Java Programming Primitive data type Examplecomment booleantrue, falseOnly two literals char‘a’ ‘\n’ ‘\\’ ‘\u03A6’ The letter a The escape sequence for “new line” Representing the back-slash 16-bit Unicode, using hexadecimals byte10, -30 short227, -102 int25 -031 0xA1F2 Integer in decimal Integer in Octal form Integer in Hexadecimal form (not case sensitive) long25L 077l -0xA3CBL

13 L ITERAL TABLE Fall. 2014 12 Java Programming Primitive data type Examplecomment float52.56f, 32.9F double100.25, 100.345d, 100.0112D 12.34e2

14 M ORE 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 short as 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. Fall. 2014 13 Java Programming

15 H EXADECIMAL, OCTAL AND BINARY FORMS  In Java, an integer beginning with 0 is a octal number ( 八進位 ).  011 represents a octal number equal to 9 10.  081 is illegal.  In Java, an integer beginning with 0x or 0X is a hexadecimal number ( 十六進位 ).  0x11 represents a hexadecimal number equal to 17 10.  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 3 10.  0b21 is illegal. Fall. 2014 14 Java Programming

16 C HARACTER 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. Fall. 2014 15 Java Programming

17 T ABLE OF C HARACTER E SCAPE S EQUENCES Fall. 2014 16 Java Programming

18 S TRING 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"" Fall. 2014 17 Java Programming

19 V ARIABLES  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. ;  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 Fall. 2014 18 Java Programming

20 V ARIABLES  Initializing variables Fall. 2014 19 Java Programming 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

21 S COPE 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. Fall. 2014 20 Java Programming

22 E XAMPLE 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); } Fall. 2014 21 Java Programming

23 L IFETIME 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. Fall. 2014 22 Java Programming


Download ppt "L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall. 2014 0 Java Programming."

Similar presentations


Ads by Google