Sie sind auf Seite 1von 33

Data Structures, Algorithems &

Programming in Java

Code:ITM 311
Java

Write Once, Run Anywhere


Object-Oriented
Programming

Partly adapted with permission from Eran Toch, Technion


JVM

JVM stands for


Java Virtual Machine

Unlikeother languages, Java


executables are executed on a CPU
that does not exist.
Platform Dependent

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!");
}
}

Note that String is built in


println is a member function for the
System.out class
Comments are almost like C++
/*Thiskindofcommentcanspanmultiplelines
*/
//Thiskindistotheendoftheline
/**
*Thiskindofcommentisaspecial
*javadocstylecomment
*/
Primitive data types are like C
Main data types are int,double,
boolean,char
Also have byte,short,long,float
boolean has values true and false
Declarations look like C, for example,
doublex,y;
intcount=0;
Expressions are like C

Arithmetic uses the familiar + - * / %


Java also has ++ and --
Java has boolean operators && || !
Java has comparisons < <= == != >= >
Java does not have pointers or pointer arithmetic
Java isn't C!

In C, almost everything is in functions


In Java, almost everything is in classes
There is often only one class per file
There must be only one public class per file
The file name must be the same as the
name of that public class, but with a .java
extension
What is a class?
Early languages had only arrays
all elements had to be of the same type
Then languages introduced structures (called
records, or structs)
allowed different data types to be grouped
Then Abstract Data Types (ADTs) became
popular
grouped operations along with the data
So, what is a class?

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

Java is case-sensitive; maxval, maxVal, and


MaxVal are three different names
Class names begin with a capital letter
All other names begin with a lowercase letter
Subsequent words are capitalized:
theBigOne
Underscores are not used in names
These are very strong conventions!
An example of a class

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.

Java bytecode - a compiler, such as javac,


transforms the Java language source code to bytecode
that runs in the JVM.

Java Virtual Machine (JVM) a program, such as


java, that runs on a given platform and takes the
bytecode programs as input and interprets them.)
Basic features of Java
Java has some interesting features:

Automatic garbage collection (Memory


saving),
Simplified network access,
Multi-threading
Loads classes dynamically, i.e., at the time
they are actually needed.(dynamically=at
run time)
How it works!
Compile-time Environment Compile-time Environment

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 Operating System


Bytecode
(.class )
Hardware
How it works!
Java is independent only for one reason:
Only depends on the Java Virtual Machine
(JVM),
code is compiled to bytecode, which is
interpreted by the resident JVM,
JIT (just in time) compilers attempt to
increase speed.
Security

* 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)

Automatic Garbage collection has the following


characteristics:
Checks for and frees memory no longer
needed, automatically.
Provides a system-level thread to track
memory allocation.
Object-Oriented

Java supports OOD


Polymorphism
Inheritance
Encapsulation
Objects
Classes
Java Advantages
Portable - Write Once, Run Anywhere
Security has been well thought through
Robust memory management
Designed for network programming
Multi-threaded (multiple simultaneous tasks)
Dynamic & extensible (loads of libraries)
Classes stored in separate files
Loaded only when needed
How to Run a Java File

Compile
javac Hello.java
Run
java Hello
Dissassemble
javap -c Hello
Advanced Features of JAVA

JAR Files (Jar Command Like Zip command)


Graphics2D(a sub class of Graphics class)
Javadoc (A tool for documentation in java)
Internationalization(JVM having default locale for different
country )
Pluggable Look and Feel (PLAF)(javax.swing.UlManager class
manage the PLAF i.e on every plateform java application look
same).
JavaBeans: Component (like Buttons, listbox some time not
GUI)

Servlets: Server-side applications

Das könnte Ihnen auch gefallen