Title: Introduction to Java
1Introduction to Java
- Huseyin Ergun
- CS204 Advanced Programming
2What do we do with Java?
3OBSS
- The company i work is an Enterprise Java Company
- Provides solutions and services to banks, large
insurance companies, gsm operators ... - What i will present is the core technology of all
projects - We have an open source framework? A-ha ?
4History of Java
- Founded by Michael Java at 1945
- ? joking
- Just to know, it is an object oriented easy to
use language - On the market for around 10 years
5Pay attention (the thing to remember)
6Architecture of Java (Client) Applications
- Java applications are compiled and run on a
machine just like any other general programming
language such as C/C. No web server or network
are required although Java applications may also
use network connections for distributed
computing. - One can download dynamically for applets or
servlets - There are also native (conventional) Java
compilers
Java code is compiled to produce byte
code run by Java Virtual Machine (JVM) to
produce results
7Java Applications in a Nutshell
- Java programs written in a file with extension
.java. - Applications are .java files with a main()
method. This is called by the Java system. - Compile and run a Java application (using
bytecodes) - Run the compiler on a .java file
- javac MyProgram.java
- producing a file of Java byte code,
MyProgram.class - Run the interpreter on a .class file
- java MyProgram
- which executes the byte code
- The tools javac and java are part of JDK.
8The Simplest Java Application Hello,World!
- Since Java is object-oriented, programs are
organized into modules called classes, which may
have data in variables called fields, and
subroutines called methods.
Each program is enclosed in a class definition.
main() is the first method that is run.
class HelloWorld public static void main
(String args) System.out.println(Hello
World!)
The notation class.method or package.class.method
is how to refer to a public method (with some
exceptions).
Syntax is similar to C - braces for blocks,
semicolon after each statement.
9Hands on Experience
- Now will write the same in Eclipse
10Java vs. JavaScript
- Despite the name, JavaScript is a different
language from Java, albeit with some
similarities. - A JavaScript program is written directly in the
HTML page, and executed by the JavaScript
interpreter, so also allows dynamic web page
content in the browser window. - JavaScript is special purpose - it is an
object-based language that deals directly with
browser entities like windows, text fields,
forms, frames and documents. - JavaScript can respond to browser events like
mouse clicks and user-typed text. - JavaScript is fast to write, but not as powerful
as Java.
11Multi-tier Architecture
- Distributed applications on the web naturally
have a multi-tier architecture. - Java plays a role at all three levels
- Graphical User Interface and client side analysis
systems, including visualization - Middle layer servers and software integration,
including web servers, distributed object servers
and other application servers. - Less important for back end client software,
which may be legacy code.
Internet
Client user interface running through browser
Internet or proprietary network
Middle level servers
Backend computing or databases
12Java FeaturesSimple and Familiar
- Familiar as it looks like C, but simpler to
program. - omits several confusing features of C including
operator overloading, multiple inheritance,
pointers and automatic type coercions - Adds automatic garbage collection to make dynamic
memory management much easier than in C or C. - No more frees or deletes. No more memory leaks.
- Adds Interface construct, similar to Objective C
concept, to compensate for the lack of multiple
inheritance. - Small kernel is suitable for Java ports to
consumer electronic devices. - But need customization J2ME to be effective
(remove unnecessary features and support special
capabilities of PDAs etc.)
13Java FeaturesArchitecture-Neutral
- C/C programming in a heterogeneous network
environment demands compatibility across several
vendor platforms and their compilers. - Solved in Java by designing platform-independent
binary representation called Java
bytecodecomparable to P-code in UCSD Pascal. - Java compiler reads Java source and generates
Java bytecode, which is shipped to user. - Each client must have a Java Virtual Machine
program, which interprets (runs) Java
bytecodes.
14Java FeaturesRobust
- Java enforces compile-time type checking and this
eliminates some error prone constructs of C/C. - Pointer arithmetic is eliminated which allows
for, e.g., runtime checking of array subscripts,
and enforces security of the Java model. - Explicit declarations are always required
argument types of methods are always checked
(unlike C). This allows the Java complier to
perform early error detection. - Java is most secure of popular languages because
it is strict and security was built in
15The Java 2 Platform
- Sun are now offering 3 editions
- Java 2 platform, Standard Edition (J2SE)
- Refines earlier JDKs
- Available in version 1.4.
- Java 2 platform, Enterprise Edition (J2EE)
- Incorporates multiple technologies for
server-side and multi-tier applications. - Java 2 platform, Micro Edition (J2ME)
- Optimized run-time environment for consumer
products.
16Installing Java on Your Comp
- Become administrator (for Windows) or root (for
Linux, etc.) - This is recommended though probably not
essential. - Go to http//java.sun.com/j2se/1.4
- Select Download Java 2 SDK, standard edition,
v1.4.x, for Windows or Linux, etc. - Download the software, then read
http//java.sun.com/j2se/1.4/install-windows.html
or http//java.sun.com/j2se/1.4/install-linux.html
, etc. - Pay attention to instructions for setting PATH.
- These give a command line interface for the Java
compiler javac and the Java interpreter (JVM
driver) java, etc. - If you are familiar with UNIX environments, but
want to use these commands from Windows, consider
installing Cygwin www.cygwin.com.
17Java Tools
18Books (From Bilmer)
- Complete Java 2 certification study guide
- Java how to program
- Ivor Horton's beginning Java 2, JDK
- Introduction to Java programming comprehensive
version - Murach's beginning Java 2, JDK 5 Java
developer's guide
19Development Studios
- Eclipse http//www.eclipse.org/
- Netbeans http//www.netbeans.org/
- Jbuilder http//www.borland.com/de/products/jbuild
er/ - Notepad http//www.microsoft.com
20Java Language Basics
21Obvious similarities to C, C
- Java syntax has many similarities to C, C.
- All variables must be declared
- Syntax of expressions and control structures
almost identical to C, C - C or C style comments allowed.
22Obvious differences from C, C
- No low-level pointers or pointer arithmetic.
- Instead have variables and expressions of
reference type. - No malloc() or free() to allocate more memory for
dynamically created data structures instead have
a new operator for creating objects, plus
automatic garbage collection. - Can declare variables almost anywhere (like C).
- No struct, union, enum, typedefclasses and
objects are used uniformly instead.
23Documentation Comments
- Used by documentation-generating tools like
javadoc to produce documentation, typically in
HTML form. - Optionally include formatting tags like _at_param,
which flags a description of a method parameter - / This method does what it feels like.
- _at_param bar This is a pointless
argument. / - void foo (int bar) . . .
- Other formatting tags include _at_returns which
flags a description of a method result value, or
_at_see name, which creates a hypertext link to name.
24Crash Course
- Now it is time to gain some sound knowledge ...
25The Java Object Model Classes, Instances and
Methods
26You should know ?
- This is you expected to know from previous
classes ?
27The Java Object Model Inheritance and the Class
Hierarchy
28Some Dependencies between Classes
- Use
- A uses B the most informal and general
relation. A might, for example, call a method
from class B, or have a method with argument type
B or return type B. - Containment
- A has a B an important special case of
useclass A has a field of type B. - Inheritance
- B is an A class B has all the properties of
class A. The compiler treats B as a special case
of A, and allows an instance of B to be used in
any place where an instance of A could appear.
In general the class B will extend A with some
extra properties of its own.
29Inheritance
- The inheritance relation is (unexpectedly?)
powerful it is built into all fully
object-oriented languages. - In Java, if some class A has been defined, we can
subsequently declare a new class, B, and specify
that it extends A. - Class A is called the superclass of B. Class B
is a subclass of A. - The class B is automatically given (inherits) all
the fields and method definitions of A. Further
fields and methods can be added that are specific
to B. - In particular, for every method signature in
class A, class B will have a method with
identical signature. - Crucially, though, the class B may define a
different implementation for some of those
methods.
30Trivial use of Inheritance
- class Shape
- void setColor(Color color)
this.color color - Color color
- int x, y //
position of center, say -
- class Circle extends Shape
- void drawCircle() . . .
- double radius
-
- class Rectangle extends Shape
- void drawRectangle() . .
. - double height, width
-
- Subclasses automatically inherit color, x, y
fields of Shape, and setColor() method.
31Polymorphism?
32Exceptions
33Exceptions are Pervasive
- Java has a concept of exceptions similar to C.
- Unlike C, Java exceptions are strictly checked.
- Most classes in the standard Java library throw
some exceptions. We will see, these must be
caught or thrown. - This means that it is almost impossible to write
useful Java code without some knowledge of the
exception mechanism!
34Exception Objects, and throw
- Any kind of exception that can be thrown by Java
code is described by an exception object. Its
class must be a subclass of Throwable. - If e is a Throwable object, the statement
- throw e
- behaves something like a break statement it
causes the enclosing block of code to end
abruptly. - If the throw statement appears inside a try
statement whos catch clause matches the class of
e, control is passed to the catch clause. - Otherwise the whole method (or constructor) ends
abruptly. The exception e is thrown again at the
point of invocation (in the calling code).
35throw compared with break
-
- myBlock
- . . .
- break myBlock
- . . .
-
- . . .
- Control jumps to end of matching block
- try
- . . .
- throw new MyException()
- . . .
- catch (MyException e)
- . . .
-
- . . .
- Control jumps to start of matching catch clause
36Interfaces
37Abstract Classes Revisited
- Recall an abstract class is a class that contains
some abstract method declarations, with no
implementation. - An abstract class can only be instantiated
indirectly, as a superclass of a class that
overrides all the abstract methods, and gives
them an implementation. You cannot directly
create an instance of an abstract class. - Constructors, static methods, private methods
cannot be abstract. - A subclass that does not override all abstract
methods is still abstract. - A method that overrides a superclass method
cannot be abstract - But an abstract class will generally also contain
non-abstract membersmethod implementations,
instance variables, etcand constructors.
38Interfaces
- An interface is something like an abstract class
where every method is required to be abstract. - An interface specifies a collection of instance
methods (behaviors) without giving the
implementation of their bodies akin to giving an
API - public interface Storable
- public abstract void
store(Stream s) - public abstract void
retrieve(Stream s) -
- Interfaces cannot include instance variables,
constructors, or static methods. - They can include class variables, but only if
they are declared finalessentially constant
definitions.
39Implementing an interface
- As for an abstract class, one cannot directly
create an instance of an interface. - Unlike an abstract class, one cannot even extend
an interface to create a class. An interface is
not a class, and it cannot have subclasses. - Instead, a class must implement an interface
- public class Picture implements Storable
- public void store(Stream s)
- // JPEG compress image
before storing - . . .
-
- public void retrieve(Stream s)
- // JPEG decompress image
after retrieving - . . .
-
-
40Packages
41Packages
- One file can contain several related classes, but
only one of them can be public. If the public
class is called Wheat, then the file must be
called Wheat.java. - A set of classes in different files can be
grouped together in a package. Each file must
start with a package declaration, eg - package mill
42Import statements
- The import declaration allows you to avoid giving
fully qualified names, eg - import java.util.Vector //
import declaration - public class VectorTest
- public static void main (String
args) - Vector bag new Vector()
- bag.addElement(new
String(item)) -
- Can also import all classes in, eg, java.util by
- import java.util.
- (but note wildcard can only appear in last
position). - Note classes (like String) in java.lang are
automatically imported.
43Some data Types?
- HashMap ?
- ListMap ?
- Array List ?
44Questions?
- Questions?
- Now it is time to open Eclipse, and do some
programming