1 Java Fundamentals PDF
1 Java Fundamentals PDF
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 files Class Loader Class files
bytecodes
Execution Engine
Native functions/methods
Operating System
Class Loader
Execution Engine
import java.applet.*;
import java.awt.*;
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,
declarations, class identifiers, and expressions.
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
hexadecimal (base 16).
- can be positive, zero or negative.
Java Programming Language Version1.0
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.
2. 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
·The + operator:
·Performs String concatenation
·Produces a new String:
String salutation = "Dr.";
String name = "Pete" + " " + "Seymour";
String title = salutation + " " + name;
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
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
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 Version1.0
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 Version1.0
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 Version1.0
University of the Philippines Cebu College