Java Fund
Java Fund
Object-Oriented
Programming
Using JAVA
Course Overview
This course covers the following areas:
• Fundamentals of the Java programming
language.
• Object-oriented concepts in Java.
• Graphical user interface (GUI)
programming.
• Introduction to Multi-threading.
What is Java?
Java ByteCode
Filename.class
java filename
JVM
JAVA API
Class Class Loader Class
files files
bytecodes
Execution Engine
Native
functions/methods
Operating System
Class Loader
Execution Engine
import java.applet.*;
import java.awt.*;
J ava Tokens
I dentifiers
Keywords
Literals
Separators
Operators
Comments
Identifiers
- case sensitive.
Keywords
- are reserved words, meaning they cannot be used
in
any way other than how Java intends for them to
be
used.
- these special tokens are always in lowercase.
- these are used as application flow controls,
Reserved Words class
declarations, in Java
identifiers, and expressions.
Declaration Keywords
boolean float
byte int
char long
double short
Java Programming Language
University of the Philippines Cebu College
Loop Keywords
break
continue
do
for
while
Conditional Keywords
case
else
if
switch
Exception Keywords
catch
finally
throw
try
Java Programming Language
University of the Philippines Cebu College
Structure Keywords
abstract implements
class instanceof
default interface
extends
Miscellaneous Keywords
false return
import super
null this
package true
Byvalue inner
Cast operator
Const outer
Future rest
Generic var
goto
Literals
- represent data in Java, and are based on character and
number representations.
- types of literals : integer, floating-point, boolean,
character,
and string.
- every variable consists of a literal and a data type; the
difference between the two is that literals are entered
explicitly into the code, while data types are information
about how much room will be reserved in memory for
such
variable, as well as possible value ranges.
Types of Literals
1. Integer Literals
- whole numbers such as 8 and 1930.
- can either be decimal(base 10), octal (base 8) or
Java Programminghexadecimal
Language (base 16).
University of the Philippines Cebu College
b. Octal Literals
- start with 0 and can be followed by any number 0-7.
c. Hex Literals
- start with 0x or 0X, followed by one or more hex digits.
- letters A-F in hex integer can either be in upper or lower
case.
- values available : 1-9, A-F, and 1-f.
. Floating-Point Literals
- represents a number that has a decimal point in it such as 4.8
Range :
Single-Precision : ±1.40239846e-45f to ±3.40282347e+38f
Double-Precision: 4.94065645841246544e-324 to
±1.79769313486231570e+308
Boolean Literals
Character Literals
String Literals
- sequence of characters enclosed in double quotes
such
as “Hello World !!!”, or even a “” for a null
character string.
- can be concatenated.
Example :
“This is the beginning“ -- one string
“ of a new relationship.” -- another
string
Separators
Operators
- are symbols used for arithmetic and logical operations.
- operators except the plus sign (+), are used only for
arithmetic calculations. The + operator can be used
in strings literal concatenation.
Logical Operators
! for NOT
|| for OR
RightShift Operators >> and >>>
•Arithmetic or signed right shift (>>) is used as follows:
128 >> 1 returns 128/21 = 64
256 >> 4 returns 256/24 = 16
-256 >> 4 returns -256/24 = -16
•The sign bit is copied during the shift.
A logical or unsigned rightshift operator (>>>) is:
•Used for bit patterns.
•The sign bit is not copied during the shift.
LeftShift Operator (<<)
•Leftshift works as follows:
128 << 1 returns 128 * 21 = 256
16 << 2 returns 16 * 22 = 64
•The + operator:
•Performs String concatenation
•Produces a new String:
String salutation = "Dr.";
String name = "Pete" + " " + "Seymour";
String title = salutation + " " + name;
•One argument must be a String object.
•Nonstrings are converted to String objects
automatically.
Comment Indicators
double
- 64 bit floating-point number.
Casting
If information might be lost in an assignment, the
programmer must confirm the assignment with a cast.
•The assignment between long and int requires an explicit
cast.
long bigValue = 99L;
int squashed = bigValue; // Wrong, needs a cast
int squashed = (int) bigValue; // OK
Promotion and Casting of Expressions
•Variables are automatically promoted to a longer form
(such as int to long).
•Expression is assignmentcompatible if the variable type is
at least as large (the same number of bits) as the
expression type.
Sample Declarations
Integer types
byte byteVar; //8 bits
short shortVar; //16 bits
int intVar; //32 bits
long longVar; //64 bits
Floating-Point Types
float floatVar; //32 bits
double doubleVar; //64 bits
Character Types
char charVar1; //holds one character
char charVar2 = ‘y’; //declares variable
//
and assigns y to it
Java Programming Language
University of the Philippines Cebu College
Arrays
char myCharArray[]; //one-dimensional array
char twoDimArray[][]; //two-dimensional array
int integerArray[]; //one-dimensional array of integers
int[]integerArray; //equivalent to integerArray[];
Sample Operations
Unary Operations
myHeight++; //myHeight is incremented by 1
myWeight--; //myWeight is decremented by 1
Assignment Operations
a = 5; //assigns 5 to a
a -= 7; //assigns a-7 to a
a +=8 //assigns a+8 to a
a /=4 //assigns a/4 to a
Binary Operations
c = b + 5; // b+5 is the binary operation
Arithmetic Operations
a = c / b + (45*a/d) - 283
Java Programming Language
University of the Philippines Cebu College
Control Flow
Statement
Block
Conditional Expressions
if construct
if (expression) statement;
if (expression) {
statement(s);
}
Sample if Statement
int number=10;
if ( (number % 2) == 0) {
System.out.println(“even”);
}
else {
System.out.println(“odd”);
}
switch construct
- a variation of the if statement is the switch statement, which
performs a multi-way branch instead of a simple binary
branch.
Form :
switch (expression) {
case value : statement(s);
break;
case value : statement(s);
break;
. . . . . . .
default : statement(s);
break;
}
default :
System.out.println(“Syntax: (l)eft, (r)ight, (q)uit”);
System.out.println(“Please enter a valid character”);
break;
}
}
Java Programming Language
University of the Philippines Cebu College
Looping Expressions
while loops
Form:
while ( expression ) statement;
or
while ( expression ) {
statement(s);
}
do-while loops
Form:
or
do {
statement(s);
}while (expression) ;
for loops
Form:
or
statement(s);
break
Sample Code :
continue
Sample Code :
if (Array[index] < 0) {
System.out.println(“ERROR: Negative number, index =“ + ix);
continue;
}
ProcessArray(Array[index]);
}
Java Programming Language
University of the Philippines Cebu College
Using break with labels: Using continue with labels:
outer: test:
do do
{ {
statement; statement;
do { do{
statement; statement;
if (boolean expression) if (condition is true)
{ {
break outer; continue test;
} }
statement; statement;
} while (boolean expression); } while (condition is true);
statement; statement;
} while (boolean expression); } while (condition is true);