Data Structures, Algorithems & Programming in Java: Code:ITM 311
Data Structures, Algorithems & Programming in Java: Code:ITM 311
Programming in Java
Code:ITM 311
Java
myprog.c myprog.exe
gcc machine code
C source code
OS/Hardware
Platform Independent
myprog.java myprog.class
javac bytecode
Java source code
JVM
OS/Hardware
Primitive types
int 4 bytes
short 2 bytes
long 8 bytes
Behaviors is
exactly as in
byte 1 byte
C++
float 4 bytes
double 8 bytes
char Unicode encoding (2 bytes)Note:
Primitive type
boolean {true,false} always begin
with lower-case
Why Java?
Its the current hot language
Its almost entirely object-oriented
It has a vast library of predefined objects
and operations
Its more platform independent
this makes it great for Web programming
Its more secure
It isnt C++
Applets, Servlets and
Applications
An applet is designed to be embedded in
a Web page, and run by a browser
Applets run in a sandbox with numerous
restrictions; for example, they cant read
files and then use the network
A servlet is designed to be run by a web
server
An application is a conventional program
Building Standalone JAVA
Programs (on UNIX)
Prepare the file foo.java using an
editor
Invoke the compiler: javac foo.java
This creates foo.class
Run the java interpreter: java foo
Java Virtual Machine
The.class files generated by the compiler
are not executable binaries
so Java combines compilation and interpretation
Instead,
they contain byte-codes to be
executed by the Java Virtual Machine
other languages have done this, e.g. UCSD
Pascal
This
approach provides platform
independence, and greater security
HelloWorld (standalone)
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
A class consists of
a collection of fields, or variables, very
much like the named fields of a struct
all the operations (called methods) that can
be performed on those fields
can be instantiated
A classdescribes objects and
operations defined on those objects
Name conventions
classPerson{
Stringname;
intage;
voidbirthday(){
age++;
System.out.println(name+'is
now'+age);
}
}
Another example of a class
classDriverextendsPerson{
longdriversLicenseNumber;
DateexpirationDate;
}
Creating and using an object
Personjohn;
john=newPerson();
john.name="JohnSmith";
john.age=37;
Personmary=newPerson();
mary.name="MaryBrown";
mary.age=33;
mary.birthday();
An array is an object
Personmary=newPerson();
intmyArray[]=newint[5];
or:
intmyArray[]={1,4,9,16,
25};
Stringlanguages[]=
{"Prolog","Java"};
Introduction
Java is:
Platform Independent programming
language.
Similar to C++ in syntax.
Compiler and runtime are free.
Free IDE (Integrated Development
Environment ): Eclipse, Netbeans.
Java Technology is used for developing
both applets and applications.
What is Java
A high level language -the Java language is a high
level one that at a glance looks very similar to C and
C++ but offers many unique features of its own.
Class
Loader Java
Class
Bytecode Libraries
Java Verifier
Source
(.java)
Just in Java
Java Java
Time Virtual
Bytecodes Interpreter
Compiler Machine
move locally
or through (Compiler/
Java network Interpreter
Compiler combo)
Runtime System
* Java.Lang.SecurityManager is a class
which is used for Security.
Class defines check methods called by
system
No memory Pointer.
No preprocessor like in c and c++.
Automatic Memory Management
Garbage Collection through JVM
(Java Virtual Machine)
Compile
javac Hello.java
Run
java Hello
Dissassemble
javap -c Hello
Advanced Features of JAVA