Lec. 01: Java Fundamentals

Download Report

Transcript Lec. 01: Java Fundamentals

Fall. 2014
Java Programming
LEC. 02: DATA T YPES AND OPERATORS (1/2)
0
CONTENT
Fall. 2014
Data types
 Literals
 Variable declaration and initialization
 Scope rules
 Operators
 Expressions
 Type conversion and casting
 Wrapper classes for primitive types

Java Programming
1
DATA TYPES
Fall. 2014

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

Java Programming

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.
2
JAVA DATA TYPES
Fall. 2014

Two categories of Java data types


Java Programming
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.
3
SIZE AND RANGE OF PRIMITIVE TYPES
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
Java Programming
Ranges
Fall. 2014
Primitive data Length in
type
bits
4
INTEGRAL TYPES
Fall. 2014
Java provides four integral types for integers, including
both positive and negative.
 Four integral types





Java Programming

byte
short
int
long
The difference among these four types is the number of
bytes used to store an integer and the ranges.
5
FLOATING TYPES
Fall. 2014
Java provides two types for real numbers , including both
positive and negative.
 Two types of floating types



Java Programming

float
double
The difference between these two types is the number of
bytes used to store a real number and the ranges.
6
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.
Java Programming

Fall. 2014

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);
7
BOOLEAN
Fall. 2014
The boolean type represents true/false values.
 Java defines the values true and false using the reserved
words true and false.

Java Programming
8
DEMO PROGRAM FOR USING boolean
Fall. 2014
class BoolDemo {
public static void main(String args[]) {
Java Programming
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));
}
}
What is the result if you
remove the parentheses of
(10 > 9) ?
9
LITERALS
Fall. 2014
Java Programming
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.

10
LITERAL TABLE
comment
boolean
true, false
Only two literals
char
‘a’
‘\n’
‘\\’
‘\u03A6’
The letter a
The escape sequence for “new line”
Representing the back-slash
16-bit Unicode, using hexadecimals
byte
10, -30
short
227, -102
int
25
-031
0xA1F2
long
25L
077l
-0xA3CBL
Java Programming
Example
Fall. 2014
Primitive data
type
Integer in decimal
Integer in Octal form
Integer in Hexadecimal form (not case sensitive)
11
LITERAL TABLE
Fall. 2014
Example
float
52.56f, 32.9F
double
100.25,
100.345d,
100.0112D
12.34e2
comment
Java Programming
Primitive data
type
12
MORE ON LITERALS
By default, integer literals are of type int.

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.
Java Programming

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.
Fall. 2014

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.
13
HEXADECIMAL, OCTAL AND BINARY FORMS
In Java, an integer beginning with 0 is a octal number(八進位).


Java Programming
011 represents a octal number equal to 910.
 081 is illegal.
Fall. 2014

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.
14
CHARACTER ESCAPE SEQUENCES (跳脫字元)
Java Programming
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
TABLE OF CHARACTER ESCAPE SEQUENCES
Fall. 2014
Java Programming
16
STRING LITERALS
Fall. 2014
Java Programming
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""
17
VARIABLES
Fall. 2014
Java Programming
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
18
VARIABLES
Initializing variables
Fall. 2014

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
19
SCOPE RULES OF VARIABLES
Scope rules defines the association between the use of a
variable and the declaration of a variable.

Java allows variables to be declared within any code block.




It determines what variables are visible to other parts of your
program and the lifetime of variables.
Remember that the definitions of classes and methods are all code
block.
Java Programming

Fall. 2014

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.
20
EXAMPLE FOR SCOPE RULES
Fall. 2014
Java Programming
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);
}
}
21
LIFETIME OF VARIABLES
Java Programming

Fall. 2014
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.
22