Java Programming Fundamental
Java Programming Fundamental
Programming
Using JAVA
What is Java?
Java is just a small, simple, safe, object-
oriented, interpreted or dynamically
optimized, byte-coded, architecture-
neutral, garbage-collected, multithreaded
programming language with a strongly
typed exception-handling mechanism for
writing distributed, dynamically extensible
programs.
Small, simple, safe and garbage collected
• C++ minus (pointers and memory
management have been omitted; operator
overloading and multiple inheritance have
been omitted)
• garbage collector automatically frees unused
memory
• strict type checking mechanism (most errors
are detected at compile time)
Interpreted, byte-coded, architecture neutral
Java Source
filename.java
javac filename.java
Java ByteCode
Filename.class
java filename
NT Unix OS/2
The Java Virtual Machine
• Provides hardware platform specifications
• Reads compiled byte codes that are platform
independent
• Is implemented as software or hardware
• Is implemented in a Java technology
development tool or a Web browser
The Java Virtual Machine
• JVM provides definitions for the:
Instruction set
Register set
Class file format
Stack
Garbage-collected heap
Memory area
JVM
JAVA API
Class files Class Loader
Class files
bytecodes
Execution Engine
Native functions/methods
Operating System
Two types of Java programs
import java.applet.*;
import java.awt.*;
Identifiers
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,
declarations, class identifiers, and expressions.
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
hexadecimal (base 16).
- can be positive, zero or negative.
Integer Literal Types
a. Decimal Literals
- decimal literals cannot start with 0, as in 02364, because
numbers beginning with 0 are reserved for octal and
hex literals.
- decimal literals : any combination from 0-9.
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
- can be concatenated.
Example :
“This is the beginning“ -- one string
“ of a new relationship.” -- another string
! for NOT
|| for OR
· The + operator:
· Performs String concatenation
· Produces a new String:
String salutation = "Dr.";
String name = "Pete" + " " + "Seymour";
String title = salutation + " " + name;
double
- 64 bit floating-point number.
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
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
Control Flow
Statement
Block
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;
}
Sample switch Statement
default :
System.out.println(“Syntax: (l)eft, (r)ight, (q)uit”);
System.out.println(“Please enter a valid character”);
break;
}
}
Looping Expressions
while loops
Form:
while ( expression ) statement;
or
while ( expression ) {
statement(s);
}
Sample while loop
Form:
or
do {
statement(s);
}while (expression) ;
Sample do-while loop
Form:
or
statement(s);
}
Sample for loop
Sample Code :
Sample Code :
if (Array[index] < 0) {
System.out.println(“ERROR: Negative number, index =“ + ix);
continue;
}
ProcessArray(Array[index]);
}
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);