Professional Documents
Culture Documents
Javas History
Developed by Sun Microsystems (James Gosling)
Based on C/C++
Designed for easy Web/Internet applications
3
JVM
Source Code
Byte Code
(Java Run
Time
Environmen
t)
Output
Java Program
Java Compiler
Source code
Byte code
Virtual machine
Byte Code
Java Interpreter
Virtual Machine
Machine code
Real Machine
Byte Code
Translating a java program into bytecode helps
makes it much easier to run a program in a
wide variety of environments.
Byte Code is interpreted (which helps to make
it secure)
Interpretation is slower but the use of
bytecode enables the java run-time system to
execute programs much faster than you might
expect.
6
Source File
(.java)
ClassLoader
Compiler
(javac)
Bytecode Verifier
Interpreter
Class File
(.class)
Network
or
File System
= security gateway
Security Manager
Operating System
Java Features
Complied and interpreted
java compiler generate byte-codes, not native machine code
the compiled byte-codes are platform-independent
java byte codes are translated on the fly to machine readable
instructions in runtime (Java Virtual Machine)
Object oriented
focus on the data (objects) and methods manipulating the data
all functions are associated with objects
potentially better code organization and reuse
Simple
fixes some clumsy features of C++
no pointers
automatic garbage collection
Distributed
9
Reliable
extensive compile-time and runtime error checking
no pointers. Memory corruptions or unauthorized memory accesses
are impossible
automatic garbage collection tracks objects usage over time
Secure
No Pointers
Byte Code Verifier
10
Java and C
Java does not include the C unique statement
keywords sizeof and typedef
Does not contain data type struct and union
Does not define the type modifier keywords Auto,
extern, register
Does not support an explicit pointer type
Does not have a pre-processor and therefore we
can not use #define, #include statement
Java requires that the function with no arguments
must be declared with empty parentheses and not
with void keyword as done in C
11
C++
Java
13
Two ways of
using Java
Java Source
code
Java Compiler
Applet type
Application type
Java enabled
Web
browser
Java
Interpreter
Output
Output
14
Array types
Variables
dataType identifier [ = Expression]:
Example variable declarations and initializations:
int x; x=5;
boolean b = true;
String x = how are you?;
Primitive
(intrinsic))
Numeric
Non-Primitive
(Derived)
Classes)
Non
Numeric
Boolean
Integer
Arrays)
Interface)
Character
16
Comments
English text scattered through the code are
comments
JAVA supports 3 types of comments
/* */ - Usually used from multi-line
comments
// - Used for single line comments
/** */ - Documentation comments
while loop
while (squared <= MAX) {
squared = lo * lo; // Calculate square
System.out.println(squared);
lo = lo + 1; /* Compute the new lo value */
}
for loop
for (int i = 1; i < MAX; i++) {
System.out.println(i); // prints 1 2 3 4 5
}
do-while loop
do {
squared = lo * lo; // Calculate square
System.out.println(squared);
lo = lo + 1; /* Compute the new lo value */
} while (squared <= MAX);
if-else loop
if ( i < 10) {
System.out.println(i is less than 10 );
}
else {
System.out.println(i is greater than or equal to 10);
}
switch statement
switch (c) {
case a:
System.out.println ( The character is a );
break;
case b:
System.out.println ( The character is b );
break;
default:
System.out.println ( The character is not a or
b );
break;
}
Java
is CASE SENSITIVE!
24
S4:
S5:
27
Note that the command is java, not javac, and you refer to
HelloWorldApp, not HelloWorldApp.java or
HelloWorldApp. Class
28
29
int nextInt()
String next()
double nextDouble()
31
Arrays in java
myArray =
Declaring Arrays
int myArray[];
declares myArray to be an array of integers
myArray = new int[8];
sets up 8 integer-sized spaces in memory,
labelled myArray[0] to myArray[7]
int myArray[] = new int[8];
combines the two statements in one line
Assigning Values
refer to the array elements by index to
store values in them.
myArray[0] = 3;
myArray[1] = 6;
myArray[2] = 3; ...
Arrays of Objects
So far we have looked at an array of
primitive types.
integers
could also use doubles, floats, characters
2-Dimensional Arrays
Declaration
int myArray[] [];
myArray=new int[3][4];
or
int myarray[][]=new int[3][4];
Arrays Length
Arrays are fixed length
Length is specified at create time
In java, all arrays store the allocated size
in a variable named length.
We can access the length of arrays as
arrayName.length:
e.g. int x = students.length();
// x = 7
Strings in java
Example:
Char charArray[]=new char[4];
charArray*0+=j;
charArray*1+=a;
charArray*2+=v;
charArray*3+=a;
String arrays
We can create and use arrays that contain strings.
The statement:
String methods
String class defines a number of methods that allow
us to
accomplish a variety of string manipulation tasks
S2=s1.toLowerCase;
// converts string s1 to lowercase
S2=s1.toUpperCase;
// converts string s1 to uppercase
S2=s1.replace(x, y); // replace all appearance of x with y
S1.equals(s2);
// returns true if s1 is equal to s2
S1.equalsIgnoreCase(s2); // returns true if s1=s2 ignoring case
S1.length();
// gives length of s1
S1.charAt(n);
// gives nth character of s1
S1.compareTo(s2) // returns ve if s1<s2, +ve if s1>s2 and 0 if
s1=s2
S1.concat(s2)
// concatenates s1 and s2