l ec. 02: d ata t ypes and o perators (1/2) fall. 2014 0 java programming
TRANSCRIPT
Java Program
ming
1
LEC. 02: DATA TYPES AND OPERATORS (1/2)
Fall. 2014
2
Java Program
ming
CONTENT
Data types Literals Variable declaration and initialization Scope rules Operators Expressions Type conversion and casting Wrapper classes for primitive types
Fall. 2014
3
Java Program
ming
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.
Fall. 2014
4
Java Program
ming
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.
Fall. 2014
5
Java Program
ming
SIZE AND RANGE OF PRIMITIVE TYPES
Fall. 2014
Primitive data type
Length in bits
Ranges
boolean N/A true and false
char 16 16-bit Unicode characters
byte 8 -27 ~ 27-1
short 16 -215 ~ 215-1
int 32 -231 ~ 231-1
long 64 -263 ~ 263-1
float 32 Based on IEEE 754
double 64 Based on IEEE 754
6
Java Program
ming
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.
Fall. 2014
7
Java Program
ming
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.
Fall. 2014
8
Java Program
ming
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);
Fall. 2014
9
Java Program
ming
BOOLEAN
The boolean type represents true/false values. Java defines the values true and false using the reserved
words true and false.
Fall. 2014
10
Java Program
ming
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));
}
}
Fall. 2014
What is the result if you remove the parentheses of
(10 > 9) ?
11
Java Program
ming
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.
Fall. 2014
12
Java Program
ming
LITERAL TABLE
Fall. 2014
Primitive data type
Example comment
boolean true, false Only two literals
char ‘a’‘\n’‘\\’‘\u03A6’
The letter aThe escape sequence for “new line”Representing the back-slash16-bit Unicode, using hexadecimals
byte 10, -30
short 227, -102
int 25-0310xA1F2
Integer in decimalInteger in Octal formInteger in Hexadecimal form (not case sensitive)
long 25L077l-0xA3CBL
13
Java Program
ming
LITERAL TABLE
Fall. 2014
Primitive data type
Example comment
float 52.56f, 32.9F
double 100.25, 100.345d,100.0112D12.34e2
14
Java Program
ming
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 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
15
Java Program
ming
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.
Fall. 2014
16
Java Program
ming
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.
Fall. 2014
17
Java Program
ming
TABLE OF CHARACTER ESCAPE SEQUENCES
Fall. 2014
18
Java Program
ming
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""
Fall. 2014
19
Java Program
ming
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
Fall. 2014
20
Java Program
ming
VARIABLES
Initializing variables
Fall. 2014
byte count = 10; // give count an initial value of 10char ch = 'X'; // initialize ch with the letter Xfloat f = 1.2F; // f is initialized with 1.2int a, b = 8, c = 19, d; // b and c have initializationsdouble radius = 4, height = 5;double volume = 3.1416 * radius * radius * height; //dynamic
21
Java Program
ming
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.
Fall. 2014
22
Java Program
ming
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);
}
}
Fall. 2014
23
Java Program
ming
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.
Fall. 2014