SlideShare a Scribd company logo
Java Overview
http://www-sldnt.slac.stanford.edu/tony/talk/java-98/

Tony Johnson
SLD
9th January 1998
What will this talk cover?
Will discuss
– History of Language
– What is Java
• Language
• Libraries

– What is it good for
– Why you should (perhaps) use it
– How does it compare with other
languages, particularly C, C++
– Future of Language
• Lawsuits etc.

– Conclusion

Not a tutorial on how to program in
Java
– There are a number of example
programs, but intent is to give a feel
for the language rather than go into
too many details.
– If there is sufficient interest it may
be possible to arrange tutorial-like
session(s) at a future date.

Appendix (left over slides)
– Gives a brief introduction to
language syntax and features.
What is Java?
History
– Originally designed (1991) as a small
language for consumer electronics
(cable boxes, toasters etc.)
– Eventually someone wrote HotJava
Browser, which could run Java
Applets
– At height of internet craze, adopted by
Netscape, Microsoft, Sun etc. as Web
Programming Language.

More than just a Web Tool
– Java is a fully functional, platform
independent, programming language
– Powerful set of machine independent
libraries, including windowing (GUI)
libraries.

Past Year
– Sun has released several new versions
of language + libraries
– >1,000,000 copies of Java
Development kit downloaded
– Java available for MAC, Win 95/NT,
HP, SUN, SGI, Digital Unix, Linux,
Amiga, OS/2, BEOS, VMS, and even
AIX
– Microsoft <---Sues---> Sun

Next Year
– Should see many real Java
applications emerge
How does Java work?
Java source code is compiled into
machine independent “bytecodes”
The bytecodes can be run directly by
an interpreter
The bytecodes can be converted to
machine code and executed (“Just in
Time (JIT)” compilation).
An optimizing interpreter can
dynamically identify program
“hotspots” and create code optimized
for the specific machine/environment.
– Optimizing interpreter coming soon
from SUN.

Java Source code

Compiler
Java “Bytecodes”

Mac
Bytecode

Unix

PC
JIT
Compiler

Interpreter
Machine Code
Java Buzzwords
Simple
– well, simpler than C++

Object Orientated
– more so than C++

Distributed
– Built in support for Internet
protocols, URL’s, HTTP etc.
– Support for distributed objects, RMI,
CORBA etc.

Robust
– Difficult to create memory leaks, go
beyond the end of an array, corrupt
stack or code

Secure
– Bytecode “verifier”, padded cell

Architecture Neural, Portable
– Runs on PC, MAC, Unix, VMS

Interpreted
– “Compiler” converts code into
machine independent “bytecodes”

High Performance
– With JIT compiler still several times
slower than native C++
– Coming soon! Optimizing interpreter
from Sun, faster (?!?) than C++

Multithreaded
– Language has direct support for
multithreading (c.f. subprocesses)

Dynamic
– Libraries can change without
recompiling programs that use them
Java Language Features
Pure Object Oriented Language
– Can only write classes

Syntax very similar to C/C++,
but without many of the rarely
used, overly complex features
of C++
–
–
–
–

No history
No operator overloading
No multiple inheritance
No Pointers

Supports
–
–
–
–

Inheritance
Interfaces
Nested Classes
Automatic Garbage Collection

Built in support for
–
–
–
–
–

Threads
Exception handling
Object serialization
Networking
Reflection
Java Libraries (API’s)
The usefulness of Java is greatly enhanced by the large range of
platform independent libraries available:
–
–
–
–
–
–

AWT - Basic GUI toolkit - delegates to OS specific “Peers”
SWING (a.k.a. JFC) Enhanced “Peerless” GUI toolkit (Beta)
2D - Transformations, stroked fonts, pattern fill, textures etc. (Beta)
JDBC - Standard Interface between Java and SQL databases
ODMG binding - Standard Interface to Object databases
RMI + CORBA - Remote procedure calls between distributed Java
applications, and between Java and other languages
– BEANS - Java based components, plug commercial components into your
applications.
– Media (audio, video etc.), Share (e.g. whiteboards), Animation, Telephony,
3D (developed by SUN and SGI), cryptography, server. (Beta)
What is Java Good For?
Web Applets
Database programming
– SQL databases using JDBC
– Object databases using ODMG binding

Java Servlets
Stand Alone Applications
– GUI based applications

Distributed client-server applications
– Using TCPIP + RMI + CORBA

Graphics applications
– 2D and 3D

Physics Analysis
Hello World in Java
Java Applets
Java Applets are programs designed
to run inside a web browser.
Instead of having a “main” method
they have a class derived from
“Applet” whose “init” method is
called when the applet is loaded.
The applet class provides
capabilities for loading images,
sounds and animations from the
internet.
Applets loaded from the internet are
run inside a “sandbox” which
restricts what they can do:
– No Access to local filesystem
– Can only open network connections
with the site they came from

Problems
– Lack of network/filesystem access
can be restrictive
– Complex programs can be large and
hence slow to download
– Sun has been introducing new
releases far to fast for browser
manufacturers to keep up.
– Each browser has subtle differences.

Solutions?
– Java activator from SUN
• Plug in for netscape/IE to ensure
latest SUN features always available.

– Signed applets can be granted more
privileges.
• Can download local copies of large
programs
Java Applet Example
Java SQL Database Access
Java provides standard interface to SQL
databases, JDBC
– Most database vendors now have implementations
of JDBC for use with their database
– Oracle has pure Java JDBC driver (no need to
install SQLNet, SQLPlus etc. to be able to use it).

Makes it possible to write database programs
which are independent of the underlying
database.
Automatic translation of database types to native
Java types:
– e.g. Date/time in database is extracted as Java Date
object, manipulation of dates straightforward.
Java SQL Database Access
Java Object Database Access
Using JDBC to connect to databases is fine for accessing
legacy data in SQL databases.
– Using SQL and pulling columns out of tables is a little
inconvenient.
– Would be better to create a network of related objects in
Java and
• “stick them all in a database”
• “retrieve all or part of the network later”

– This is what object databases are for
• ODMG (Object Database Management Group) has defined a
standard binding between Java and OO databases.
• Several OO databases new support the ODMG Java binding
Java Object Database Access
Java Servlets
A Java Servlet runs on a web server instead of a web browser
– Performs similar function to CGI scripts under Unix
– Works with many web servers
• Netscape, Microsoft IIS, Apache, Java Web Server, many more...

– Machine independent
– Much faster than CGI (no process forking)
– Gives full power of Java for database access, data manipulation etc.
Real Applications in Java?
So far all examples have been fairly trivial.
Is it possible to write real applications in Java? Yes! (at least I hope so)
Java for Physics Analysis?
Java has good mix of power, simplicity, math, speed for physics analysis
– SLD has developed a set of hep.analysis classes for Histograming
– Networking support and mobility of code excellent for client server applications.
• Analysis routine goes to data instead of data coming to analysis routine.
How to get started with Java
Download free Java Developer Kit (JDK) from SUN (www.javasoft.com)
–
–
–
–

Available for Windows, Solaris.
Contains line mode compiler+debugger+misc. tools
Similar kits available for many other platforms (from vendors)
Solaris/AIX JDK installed for Unix at SLAC in /afs/slac/packages/java

For more serious work get hold of a Java Integrated Development Environment
–
–
–
–

Symantec Café (the oldest and most popular IDE at present)
Visual J++ from Microsoft (beware of assimilation)
Visual Age for Java from IBM
Java Workshop from SUN
• All contain integrated editor/debugger/source control
• None have really good WYSIWYG layout tools (yet).
More Information
Books
– Keep away from books whose
focus is too web related
– O’Reilly
– Java in a Nutshell
– Exploring Java
– Core Java (SunSoft)
• A little two thick for comfort, but
comprehensive and down-to-earth

– Learn Java Now (Microsoft)
• Comes free with Visual J++

Web Sites
–

http://www.javasoft.com

• Sun’s home page for Java
–

http://www.javasoft.com/books/Series/Tutorial/index.html

• Complete JAVA tutorial, in case you are
too poor to buy the book
–

http://www.gamelan.com/

• Huge library of Java applets and
applications
–

http://www.javaworld.com

–

http://www.javareport.com

• JavaWorld/Java Report (magazines)
–

http://café.symantec.com/

• Café Web Site
–

http://www.microsoft.com/visualj

• Visual J++
–

http://www-sldnt.slac.stanford.edu/jas

• Java Analysis Studio
Conclusion
If you are still using:
– Fortran, C, C++, Perl, CGI, SQLPlus, Rexx, IDA etc.

you could save yourself a lot of time and effort by switching to:
Appendix

Java Language Elements
Topics to be Covered
Java Language Features
–
–
–
–
–
–
–
–
–

Variables, Operators, Expressions
Statements
Classes + Objects
Inheritance
Packages
Exceptions
Multithreading
Data Structures
Standard Libraries

Plenty of example programs
– Tutorial rather than real-world programs
Types, Variables, Operators, Expressions
Primitive Types
int, short, long, byte
• all signed
• size is machine independent
float, double
char
• Stores single UNICODE character
boolean

Variables and Expressions
int a, b;
double salary = 100000;
float slacRaise = 1.00002;
salary = salary * slacRaise;
salary *= slacRaise;
salary -= parkingFine;

Operators
+ - * / % ^ ++ -! ~ & | && || ? :
<< >> >>>
< <= > >= == !=

Strings
– Java has nice UNICODE string type
that supports concatenation, substrings,
comparison etc.
String s = “Your raise is ” +
slacRaise;

Notes:
– if (a = 3) { } is illegal!

– No operator overloading
– No pointers! (But surprisingly you
won’t miss them)
Statements
Again, very C like
if (condition1) {block1}
else if (condition2) {block2}
else {block3}
for (int i=0 ; i<10 ; i++)
{block}
while (condition) {block}
do {block} while (condition);

Unfortunately even the switch
statement is just like C
switch (choice)
{
case 1:
case 2:
…
break;
case 3:
…
break;
default:
…
break;
}
Introduction to OO Programming
Java is an object oriented
programming language, so some
rudimentary knowledge of OO
terminology is required
– Classes (e.g. “Employee”, “Track”)
• Objects
– specific instances of a class (e.g.
“Tony Johnson”, “Track 5”)
• In Java all functions are contained
within classes
• For largely historical reasons
functions are often called methods or
member functions

– Inheritance (aka subclassing)
• e.g. “Associate Director”, “Drift
Chamber Track”

– Encapsulation
• Variables/Functions can be declared:
– private
• only functions within same class
or superclass can access them (c.f.
C++ protected)
– public
• any function can access them
• Java also implements
– static
• Applies to class not object
– final
• Cannot be overridden by
subclass
Classes
class Employee
Class Declaration
{
public Employee (String n, double s)
Constructor
{
name = n;
salary = s;
}
public Employee (String n)
Overloaded Constructor
{
name = n;
salary = 0;
}
public void raiseSalary(double byPercent)
{
salary *= 1 + byPercent / 100;
}
public double getSalary()
Access Method
{
return salary;
}
private String name;
Private Member Variables
private double salary;
}
Objects
Objects represent instances of classes
Employee javaExpert = new Employee(“Joe”,100000);
javaExpert.RaiseSalary(10); // Raise salary by 10%
system.out.println(“Salary is” + javaExpert.GetSalary());

Note that Java uses the keyword new for creation of new objects.
Unlike C++ there is no delete keyword for the deletion of objects.
– Java handles deletion of objects (and recovery of the memory they occupy)
automatically as soon as there are no longer any references to them.
– This makes writing code much easier, it is “impossible” to create a memory
leak in Java..
– The process of keeping track of when objects are unreferenced and deleting
them is called “garbage collection”. It does impose some processing overhead
which slows down Java programs compared to equivalent programs in C or C+
+.

Appl_1 Example + EmployeeTest Example
Inheritance
class Manager Extends Employee
Class Declaration
{
public Manager (String n, double s, Employee e)
Constructor
{
name = n;
salary = s;
secretary = e;
}
public String GetSecretary()
New Access Method
{
return secretary;
}
public void RaiseSalary(double byPercent)
Overridden Method
{
double bonus = 10;
super.RaiseSalary(byPercent + bonus);
}
private Employee secretary;
New member variables
}

ManagerTest Example
Interfaces
Interfaces in Java are a replacement
for the concept of “multiple
inheritance” in C++.
Suppose we want to sort managers by
the number of employees they have.

Employee
Manager
Sortable
In C++ both Employee and Sortable
can be classes
In Java one (probably sortable) must
be an “interface”
Employee Sort Example

Interfaces
– Objects can implement any number of
interfaces
– Can have hierarchies of interfaces
– Interfaces cannot have methods
– Cleaner but not quite as powerful as
multiple inheritance
– Only way to implement “callbacks” in
Java

Enumeration Interface
– Can loop over all elements of any class
that implements the enumeration
interface
Enumeration e = thing.elements();
while (e.hasMoreElements())
{
Thing t = e.getNextElement();
}
Arrays, Collections etc.
C++

Built in objects continued…

– “If you build it they will come”
– Standard Template Library

Java has powerful built in objects
– String
– Arrays
• Arbitrary size but cannot be resized

– Vectors
• Full dynamic sizing

– Property Sets
• Useful for storing program
parameters

–
–
–
–
–

– Bit Sets
• Arbitrary size array of bits

– HashTables + HashSets
• Quick way to look up object in large
collection

Linked Lists
Queues
Stacks
Multi-Dimensional Arrays
Full featured “Collections” coming
in JDK 1.2

Sieve Example (Benchmark)
–
–
–
–

C++ 1.53 seconds
Java 59.93 seconds
Visual Basic 65.69 seconds
Microsoft JIT compiler ?3 seconds
Threads
Threads are similar to “fork” or
“spawn” but
– Threads run in same address space,
not separate process
• Very efficient
• Very dangerous

– All modern operating systems have
support for threads

Java has built-in support for threads
– Just derive a class from thread
– Issue the start() method on the
object

Bouncing Ball Example

Java Threads support
–
–
–
–

Priorities
Interrupts
Thread groups
Synchronization
• synchronized keyword can be
applied to classes or methods

– Timers
Java AWT Package
AWT allows development of
platform independent GUI programs
– Java takes a “Lowest Common
Denominator” approach
• Supports only one mouse button
• Doesn’t “look as nice” as native
windows/MAC applications
• Available graphical layout tools are
quite limited.

– Moderate set of graphic objects
• Buttons, Labels, Text Input, Menus,
• Scroll Bars, Dialogs, Radio Buttons

– Mapped onto native GUI objects on
each platform.

“Layouts” are used to compensate for
the fact that GUI objects may have
different sizes on different platforms
– Limited set of layouts are provided
•
•
•
•

Flow Layout
Border Layout
Card Layout
Grid (Bag) Layout

– User defined layouts needed for most
real programs

HelloWorld Example
Java for Physics?
Java
Compiler

Experiment
Extensions
(Event Display)

import physics.hep.*;

GUI
Network
Data
Analysis
Engine
Users
Java
Code

Experiment
Interface

class MyAnalysis extends Analysis
{
public void init()
{
h = new Hist(“Energy”);
}
public void Evanal(Event e)
{
Enumeration x = e.getTracks();
while (x.hasMoreElements())
{
Track t = x.getNextElement();
h.Add(t.getEnergy());
}
}
private Histogram h;
}

More Related Content

What's hot

Dd13.2013.milano.open ntf
Dd13.2013.milano.open ntfDd13.2013.milano.open ntf
Dd13.2013.milano.open ntfUlrich Krause
 
OSGi Community Event 2010 - OSGi and Android
OSGi Community Event 2010 - OSGi and AndroidOSGi Community Event 2010 - OSGi and Android
OSGi Community Event 2010 - OSGi and Android
mfrancis
 
NetBeans IDE & Platform
NetBeans IDE & PlatformNetBeans IDE & Platform
NetBeans IDE & Platform
Aatul Palandurkar
 
Introduction to Java
Introduction to Java Introduction to Java
Introduction to Java
Hitesh-Java
 
Netbeans
NetbeansNetbeans
Netbeansacosdt
 
Lessons learned from building Eclipse-based add-ons for commercial modeling t...
Lessons learned from building Eclipse-based add-ons for commercial modeling t...Lessons learned from building Eclipse-based add-ons for commercial modeling t...
Lessons learned from building Eclipse-based add-ons for commercial modeling t...
IncQuery Labs
 
UKLUG 2012 - XPages, Beyond the basics
UKLUG 2012 - XPages, Beyond the basicsUKLUG 2012 - XPages, Beyond the basics
UKLUG 2012 - XPages, Beyond the basicsUlrich Krause
 
Desktop Applications with NetBeans
Desktop Applications with NetBeansDesktop Applications with NetBeans
Desktop Applications with NetBeans
Huu Bang Le Phan
 
An XPager's Guide to Process Server-Side Jobs on Domino
An XPager's Guide to Process Server-Side Jobs on DominoAn XPager's Guide to Process Server-Side Jobs on Domino
An XPager's Guide to Process Server-Side Jobs on Domino
Frank van der Linden
 
From Java to Ruby...and Back
From Java to Ruby...and BackFrom Java to Ruby...and Back
From Java to Ruby...and Back
Anil Hemrajani
 
What's New in NetBeans IDE 7.x
What's New in NetBeans IDE 7.xWhat's New in NetBeans IDE 7.x
What's New in NetBeans IDE 7.x
Geertjan Wielenga
 
IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...
IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...
IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...
Paul Withers
 
Dc roundtablesmall webservices_2002
Dc roundtablesmall webservices_2002Dc roundtablesmall webservices_2002
Dc roundtablesmall webservices_2002eaiti
 
BP207 - Meet the Java Application Server You Already Own – IBM Domino
BP207 - Meet the Java Application Server You Already Own – IBM DominoBP207 - Meet the Java Application Server You Already Own – IBM Domino
BP207 - Meet the Java Application Server You Already Own – IBM Domino
Serdar Basegmez
 
Introducing CrossWorlds for IBM Domino
Introducing CrossWorlds for IBM DominoIntroducing CrossWorlds for IBM Domino
Introducing CrossWorlds for IBM Domino
Daniele Vistalli
 
Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)
Martijn Verburg
 
Java: Finding Bugs, Fixing Bugs in IBM Domino Designer and XPages
Java: Finding Bugs, Fixing Bugs in IBM Domino Designer and XPagesJava: Finding Bugs, Fixing Bugs in IBM Domino Designer and XPages
Java: Finding Bugs, Fixing Bugs in IBM Domino Designer and XPages
panagenda
 
Silver Light1.0
Silver Light1.0Silver Light1.0
Silver Light1.0
coolajju143
 
GateIn - The Solution for Managing and Building Enterprise Web Apps
GateIn - The Solution for Managing and Building Enterprise Web AppsGateIn - The Solution for Managing and Building Enterprise Web Apps
GateIn - The Solution for Managing and Building Enterprise Web Apps
Wesley Hales
 
GateIn - Presented at Atlanta JUG on 1/19/2010
GateIn - Presented at Atlanta JUG on 1/19/2010GateIn - Presented at Atlanta JUG on 1/19/2010
GateIn - Presented at Atlanta JUG on 1/19/2010
Wesley Hales
 

What's hot (20)

Dd13.2013.milano.open ntf
Dd13.2013.milano.open ntfDd13.2013.milano.open ntf
Dd13.2013.milano.open ntf
 
OSGi Community Event 2010 - OSGi and Android
OSGi Community Event 2010 - OSGi and AndroidOSGi Community Event 2010 - OSGi and Android
OSGi Community Event 2010 - OSGi and Android
 
NetBeans IDE & Platform
NetBeans IDE & PlatformNetBeans IDE & Platform
NetBeans IDE & Platform
 
Introduction to Java
Introduction to Java Introduction to Java
Introduction to Java
 
Netbeans
NetbeansNetbeans
Netbeans
 
Lessons learned from building Eclipse-based add-ons for commercial modeling t...
Lessons learned from building Eclipse-based add-ons for commercial modeling t...Lessons learned from building Eclipse-based add-ons for commercial modeling t...
Lessons learned from building Eclipse-based add-ons for commercial modeling t...
 
UKLUG 2012 - XPages, Beyond the basics
UKLUG 2012 - XPages, Beyond the basicsUKLUG 2012 - XPages, Beyond the basics
UKLUG 2012 - XPages, Beyond the basics
 
Desktop Applications with NetBeans
Desktop Applications with NetBeansDesktop Applications with NetBeans
Desktop Applications with NetBeans
 
An XPager's Guide to Process Server-Side Jobs on Domino
An XPager's Guide to Process Server-Side Jobs on DominoAn XPager's Guide to Process Server-Side Jobs on Domino
An XPager's Guide to Process Server-Side Jobs on Domino
 
From Java to Ruby...and Back
From Java to Ruby...and BackFrom Java to Ruby...and Back
From Java to Ruby...and Back
 
What's New in NetBeans IDE 7.x
What's New in NetBeans IDE 7.xWhat's New in NetBeans IDE 7.x
What's New in NetBeans IDE 7.x
 
IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...
IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...
IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...
 
Dc roundtablesmall webservices_2002
Dc roundtablesmall webservices_2002Dc roundtablesmall webservices_2002
Dc roundtablesmall webservices_2002
 
BP207 - Meet the Java Application Server You Already Own – IBM Domino
BP207 - Meet the Java Application Server You Already Own – IBM DominoBP207 - Meet the Java Application Server You Already Own – IBM Domino
BP207 - Meet the Java Application Server You Already Own – IBM Domino
 
Introducing CrossWorlds for IBM Domino
Introducing CrossWorlds for IBM DominoIntroducing CrossWorlds for IBM Domino
Introducing CrossWorlds for IBM Domino
 
Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)
 
Java: Finding Bugs, Fixing Bugs in IBM Domino Designer and XPages
Java: Finding Bugs, Fixing Bugs in IBM Domino Designer and XPagesJava: Finding Bugs, Fixing Bugs in IBM Domino Designer and XPages
Java: Finding Bugs, Fixing Bugs in IBM Domino Designer and XPages
 
Silver Light1.0
Silver Light1.0Silver Light1.0
Silver Light1.0
 
GateIn - The Solution for Managing and Building Enterprise Web Apps
GateIn - The Solution for Managing and Building Enterprise Web AppsGateIn - The Solution for Managing and Building Enterprise Web Apps
GateIn - The Solution for Managing and Building Enterprise Web Apps
 
GateIn - Presented at Atlanta JUG on 1/19/2010
GateIn - Presented at Atlanta JUG on 1/19/2010GateIn - Presented at Atlanta JUG on 1/19/2010
GateIn - Presented at Atlanta JUG on 1/19/2010
 

Viewers also liked (6)

Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Java
JavaJava
Java
 
Java
JavaJava
Java
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Uml for Java Programmers
Uml for Java ProgrammersUml for Java Programmers
Uml for Java Programmers
 
Introduction of c# day1
Introduction of c# day1Introduction of c# day1
Introduction of c# day1
 

Similar to Java

MWLUG - Universal Java
MWLUG  -  Universal JavaMWLUG  -  Universal Java
MWLUG - Universal Java
Philippe Riand
 
01-Introduction.ppt
01-Introduction.ppt01-Introduction.ppt
01-Introduction.ppt
EmanAsem4
 
Java Introduction
Java IntroductionJava Introduction
Java Introduction
sunmitraeducation
 
Lec 1-of-oop2
Lec 1-of-oop2Lec 1-of-oop2
Lec 1-of-oop2
SM Rasel
 
Java1
Java1Java1
Java1
Java1Java1
Lecture-01 _Java Introduction CS 441 Fast
Lecture-01 _Java Introduction CS 441 FastLecture-01 _Java Introduction CS 441 Fast
Lecture-01 _Java Introduction CS 441 Fast
UzairSaeed18
 
Introduction to Java Part-2
Introduction to Java Part-2Introduction to Java Part-2
Introduction to Java Part-2
RatnaJava
 
Migrating to Java 11
Migrating to Java 11Migrating to Java 11
Migrating to Java 11
Arto Santala
 
The Evolution of Java
The Evolution of JavaThe Evolution of Java
The Evolution of Java
Fu Cheng
 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programming
Elizabeth Thomas
 
Java1 in mumbai
Java1 in mumbaiJava1 in mumbai
Java1 in mumbai
vibrantuser
 
JAVA INTRODUCTION - 1
JAVA INTRODUCTION - 1JAVA INTRODUCTION - 1
JAVA INTRODUCTION - 1
Infoviaan Technologies
 
The Java Story
The Java StoryThe Java Story
The Java Story
David Parsons
 
Java
JavaJava
Advance java prasentation
Advance java prasentationAdvance java prasentation
Advance java prasentation
dhananajay95
 
Industrial training
Industrial trainingIndustrial training
Industrial training
SOUVIK BANERJEE
 
Java withrealworldtechnology
Java withrealworldtechnologyJava withrealworldtechnology
Java withrealworldtechnology
Anju Kanjirathingal
 

Similar to Java (20)

MWLUG - Universal Java
MWLUG  -  Universal JavaMWLUG  -  Universal Java
MWLUG - Universal Java
 
01-Introduction.ppt
01-Introduction.ppt01-Introduction.ppt
01-Introduction.ppt
 
Java Introduction
Java IntroductionJava Introduction
Java Introduction
 
Lec 1-of-oop2
Lec 1-of-oop2Lec 1-of-oop2
Lec 1-of-oop2
 
Java1
Java1Java1
Java1
 
Java1
Java1Java1
Java1
 
Java (1)
Java (1)Java (1)
Java (1)
 
Lecture-01 _Java Introduction CS 441 Fast
Lecture-01 _Java Introduction CS 441 FastLecture-01 _Java Introduction CS 441 Fast
Lecture-01 _Java Introduction CS 441 Fast
 
Introduction to Java Part-2
Introduction to Java Part-2Introduction to Java Part-2
Introduction to Java Part-2
 
Migrating to Java 11
Migrating to Java 11Migrating to Java 11
Migrating to Java 11
 
The Evolution of Java
The Evolution of JavaThe Evolution of Java
The Evolution of Java
 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programming
 
Java1 in mumbai
Java1 in mumbaiJava1 in mumbai
Java1 in mumbai
 
JAVA INTRODUCTION - 1
JAVA INTRODUCTION - 1JAVA INTRODUCTION - 1
JAVA INTRODUCTION - 1
 
The Java Story
The Java StoryThe Java Story
The Java Story
 
Java
JavaJava
Java
 
Advance java prasentation
Advance java prasentationAdvance java prasentation
Advance java prasentation
 
Industrial training
Industrial trainingIndustrial training
Industrial training
 
Java withrealworldtechnology
Java withrealworldtechnologyJava withrealworldtechnology
Java withrealworldtechnology
 
java full 1.docx
java full 1.docxjava full 1.docx
java full 1.docx
 

More from QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH, SINDH, PAKISTAN (6)

Java multi thread programming on cmp system
Java multi thread programming on cmp systemJava multi thread programming on cmp system
Java multi thread programming on cmp system
 
Java applets
Java appletsJava applets
Java applets
 
Introduction to-java
Introduction to-javaIntroduction to-java
Introduction to-java
 
Introduction+to+java+2
Introduction+to+java+2Introduction+to+java+2
Introduction+to+java+2
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Threads
ThreadsThreads
Threads
 

Recently uploaded

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 

Recently uploaded (20)

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 

Java

  • 2. What will this talk cover? Will discuss – History of Language – What is Java • Language • Libraries – What is it good for – Why you should (perhaps) use it – How does it compare with other languages, particularly C, C++ – Future of Language • Lawsuits etc. – Conclusion Not a tutorial on how to program in Java – There are a number of example programs, but intent is to give a feel for the language rather than go into too many details. – If there is sufficient interest it may be possible to arrange tutorial-like session(s) at a future date. Appendix (left over slides) – Gives a brief introduction to language syntax and features.
  • 3. What is Java? History – Originally designed (1991) as a small language for consumer electronics (cable boxes, toasters etc.) – Eventually someone wrote HotJava Browser, which could run Java Applets – At height of internet craze, adopted by Netscape, Microsoft, Sun etc. as Web Programming Language. More than just a Web Tool – Java is a fully functional, platform independent, programming language – Powerful set of machine independent libraries, including windowing (GUI) libraries. Past Year – Sun has released several new versions of language + libraries – >1,000,000 copies of Java Development kit downloaded – Java available for MAC, Win 95/NT, HP, SUN, SGI, Digital Unix, Linux, Amiga, OS/2, BEOS, VMS, and even AIX – Microsoft <---Sues---> Sun Next Year – Should see many real Java applications emerge
  • 4. How does Java work? Java source code is compiled into machine independent “bytecodes” The bytecodes can be run directly by an interpreter The bytecodes can be converted to machine code and executed (“Just in Time (JIT)” compilation). An optimizing interpreter can dynamically identify program “hotspots” and create code optimized for the specific machine/environment. – Optimizing interpreter coming soon from SUN. Java Source code Compiler Java “Bytecodes” Mac Bytecode Unix PC JIT Compiler Interpreter Machine Code
  • 5. Java Buzzwords Simple – well, simpler than C++ Object Orientated – more so than C++ Distributed – Built in support for Internet protocols, URL’s, HTTP etc. – Support for distributed objects, RMI, CORBA etc. Robust – Difficult to create memory leaks, go beyond the end of an array, corrupt stack or code Secure – Bytecode “verifier”, padded cell Architecture Neural, Portable – Runs on PC, MAC, Unix, VMS Interpreted – “Compiler” converts code into machine independent “bytecodes” High Performance – With JIT compiler still several times slower than native C++ – Coming soon! Optimizing interpreter from Sun, faster (?!?) than C++ Multithreaded – Language has direct support for multithreading (c.f. subprocesses) Dynamic – Libraries can change without recompiling programs that use them
  • 6. Java Language Features Pure Object Oriented Language – Can only write classes Syntax very similar to C/C++, but without many of the rarely used, overly complex features of C++ – – – – No history No operator overloading No multiple inheritance No Pointers Supports – – – – Inheritance Interfaces Nested Classes Automatic Garbage Collection Built in support for – – – – – Threads Exception handling Object serialization Networking Reflection
  • 7. Java Libraries (API’s) The usefulness of Java is greatly enhanced by the large range of platform independent libraries available: – – – – – – AWT - Basic GUI toolkit - delegates to OS specific “Peers” SWING (a.k.a. JFC) Enhanced “Peerless” GUI toolkit (Beta) 2D - Transformations, stroked fonts, pattern fill, textures etc. (Beta) JDBC - Standard Interface between Java and SQL databases ODMG binding - Standard Interface to Object databases RMI + CORBA - Remote procedure calls between distributed Java applications, and between Java and other languages – BEANS - Java based components, plug commercial components into your applications. – Media (audio, video etc.), Share (e.g. whiteboards), Animation, Telephony, 3D (developed by SUN and SGI), cryptography, server. (Beta)
  • 8. What is Java Good For? Web Applets Database programming – SQL databases using JDBC – Object databases using ODMG binding Java Servlets Stand Alone Applications – GUI based applications Distributed client-server applications – Using TCPIP + RMI + CORBA Graphics applications – 2D and 3D Physics Analysis
  • 10. Java Applets Java Applets are programs designed to run inside a web browser. Instead of having a “main” method they have a class derived from “Applet” whose “init” method is called when the applet is loaded. The applet class provides capabilities for loading images, sounds and animations from the internet. Applets loaded from the internet are run inside a “sandbox” which restricts what they can do: – No Access to local filesystem – Can only open network connections with the site they came from Problems – Lack of network/filesystem access can be restrictive – Complex programs can be large and hence slow to download – Sun has been introducing new releases far to fast for browser manufacturers to keep up. – Each browser has subtle differences. Solutions? – Java activator from SUN • Plug in for netscape/IE to ensure latest SUN features always available. – Signed applets can be granted more privileges. • Can download local copies of large programs
  • 12. Java SQL Database Access Java provides standard interface to SQL databases, JDBC – Most database vendors now have implementations of JDBC for use with their database – Oracle has pure Java JDBC driver (no need to install SQLNet, SQLPlus etc. to be able to use it). Makes it possible to write database programs which are independent of the underlying database. Automatic translation of database types to native Java types: – e.g. Date/time in database is extracted as Java Date object, manipulation of dates straightforward.
  • 14. Java Object Database Access Using JDBC to connect to databases is fine for accessing legacy data in SQL databases. – Using SQL and pulling columns out of tables is a little inconvenient. – Would be better to create a network of related objects in Java and • “stick them all in a database” • “retrieve all or part of the network later” – This is what object databases are for • ODMG (Object Database Management Group) has defined a standard binding between Java and OO databases. • Several OO databases new support the ODMG Java binding
  • 16. Java Servlets A Java Servlet runs on a web server instead of a web browser – Performs similar function to CGI scripts under Unix – Works with many web servers • Netscape, Microsoft IIS, Apache, Java Web Server, many more... – Machine independent – Much faster than CGI (no process forking) – Gives full power of Java for database access, data manipulation etc.
  • 17. Real Applications in Java? So far all examples have been fairly trivial. Is it possible to write real applications in Java? Yes! (at least I hope so)
  • 18. Java for Physics Analysis? Java has good mix of power, simplicity, math, speed for physics analysis – SLD has developed a set of hep.analysis classes for Histograming – Networking support and mobility of code excellent for client server applications. • Analysis routine goes to data instead of data coming to analysis routine.
  • 19. How to get started with Java Download free Java Developer Kit (JDK) from SUN (www.javasoft.com) – – – – Available for Windows, Solaris. Contains line mode compiler+debugger+misc. tools Similar kits available for many other platforms (from vendors) Solaris/AIX JDK installed for Unix at SLAC in /afs/slac/packages/java For more serious work get hold of a Java Integrated Development Environment – – – – Symantec Café (the oldest and most popular IDE at present) Visual J++ from Microsoft (beware of assimilation) Visual Age for Java from IBM Java Workshop from SUN • All contain integrated editor/debugger/source control • None have really good WYSIWYG layout tools (yet).
  • 20. More Information Books – Keep away from books whose focus is too web related – O’Reilly – Java in a Nutshell – Exploring Java – Core Java (SunSoft) • A little two thick for comfort, but comprehensive and down-to-earth – Learn Java Now (Microsoft) • Comes free with Visual J++ Web Sites – http://www.javasoft.com • Sun’s home page for Java – http://www.javasoft.com/books/Series/Tutorial/index.html • Complete JAVA tutorial, in case you are too poor to buy the book – http://www.gamelan.com/ • Huge library of Java applets and applications – http://www.javaworld.com – http://www.javareport.com • JavaWorld/Java Report (magazines) – http://café.symantec.com/ • Café Web Site – http://www.microsoft.com/visualj • Visual J++ – http://www-sldnt.slac.stanford.edu/jas • Java Analysis Studio
  • 21. Conclusion If you are still using: – Fortran, C, C++, Perl, CGI, SQLPlus, Rexx, IDA etc. you could save yourself a lot of time and effort by switching to:
  • 23. Topics to be Covered Java Language Features – – – – – – – – – Variables, Operators, Expressions Statements Classes + Objects Inheritance Packages Exceptions Multithreading Data Structures Standard Libraries Plenty of example programs – Tutorial rather than real-world programs
  • 24. Types, Variables, Operators, Expressions Primitive Types int, short, long, byte • all signed • size is machine independent float, double char • Stores single UNICODE character boolean Variables and Expressions int a, b; double salary = 100000; float slacRaise = 1.00002; salary = salary * slacRaise; salary *= slacRaise; salary -= parkingFine; Operators + - * / % ^ ++ -! ~ & | && || ? : << >> >>> < <= > >= == != Strings – Java has nice UNICODE string type that supports concatenation, substrings, comparison etc. String s = “Your raise is ” + slacRaise; Notes: – if (a = 3) { } is illegal! – No operator overloading – No pointers! (But surprisingly you won’t miss them)
  • 25. Statements Again, very C like if (condition1) {block1} else if (condition2) {block2} else {block3} for (int i=0 ; i<10 ; i++) {block} while (condition) {block} do {block} while (condition); Unfortunately even the switch statement is just like C switch (choice) { case 1: case 2: … break; case 3: … break; default: … break; }
  • 26. Introduction to OO Programming Java is an object oriented programming language, so some rudimentary knowledge of OO terminology is required – Classes (e.g. “Employee”, “Track”) • Objects – specific instances of a class (e.g. “Tony Johnson”, “Track 5”) • In Java all functions are contained within classes • For largely historical reasons functions are often called methods or member functions – Inheritance (aka subclassing) • e.g. “Associate Director”, “Drift Chamber Track” – Encapsulation • Variables/Functions can be declared: – private • only functions within same class or superclass can access them (c.f. C++ protected) – public • any function can access them • Java also implements – static • Applies to class not object – final • Cannot be overridden by subclass
  • 27. Classes class Employee Class Declaration { public Employee (String n, double s) Constructor { name = n; salary = s; } public Employee (String n) Overloaded Constructor { name = n; salary = 0; } public void raiseSalary(double byPercent) { salary *= 1 + byPercent / 100; } public double getSalary() Access Method { return salary; } private String name; Private Member Variables private double salary; }
  • 28. Objects Objects represent instances of classes Employee javaExpert = new Employee(“Joe”,100000); javaExpert.RaiseSalary(10); // Raise salary by 10% system.out.println(“Salary is” + javaExpert.GetSalary()); Note that Java uses the keyword new for creation of new objects. Unlike C++ there is no delete keyword for the deletion of objects. – Java handles deletion of objects (and recovery of the memory they occupy) automatically as soon as there are no longer any references to them. – This makes writing code much easier, it is “impossible” to create a memory leak in Java.. – The process of keeping track of when objects are unreferenced and deleting them is called “garbage collection”. It does impose some processing overhead which slows down Java programs compared to equivalent programs in C or C+ +. Appl_1 Example + EmployeeTest Example
  • 29. Inheritance class Manager Extends Employee Class Declaration { public Manager (String n, double s, Employee e) Constructor { name = n; salary = s; secretary = e; } public String GetSecretary() New Access Method { return secretary; } public void RaiseSalary(double byPercent) Overridden Method { double bonus = 10; super.RaiseSalary(byPercent + bonus); } private Employee secretary; New member variables } ManagerTest Example
  • 30. Interfaces Interfaces in Java are a replacement for the concept of “multiple inheritance” in C++. Suppose we want to sort managers by the number of employees they have. Employee Manager Sortable In C++ both Employee and Sortable can be classes In Java one (probably sortable) must be an “interface” Employee Sort Example Interfaces – Objects can implement any number of interfaces – Can have hierarchies of interfaces – Interfaces cannot have methods – Cleaner but not quite as powerful as multiple inheritance – Only way to implement “callbacks” in Java Enumeration Interface – Can loop over all elements of any class that implements the enumeration interface Enumeration e = thing.elements(); while (e.hasMoreElements()) { Thing t = e.getNextElement(); }
  • 31. Arrays, Collections etc. C++ Built in objects continued… – “If you build it they will come” – Standard Template Library Java has powerful built in objects – String – Arrays • Arbitrary size but cannot be resized – Vectors • Full dynamic sizing – Property Sets • Useful for storing program parameters – – – – – – Bit Sets • Arbitrary size array of bits – HashTables + HashSets • Quick way to look up object in large collection Linked Lists Queues Stacks Multi-Dimensional Arrays Full featured “Collections” coming in JDK 1.2 Sieve Example (Benchmark) – – – – C++ 1.53 seconds Java 59.93 seconds Visual Basic 65.69 seconds Microsoft JIT compiler ?3 seconds
  • 32. Threads Threads are similar to “fork” or “spawn” but – Threads run in same address space, not separate process • Very efficient • Very dangerous – All modern operating systems have support for threads Java has built-in support for threads – Just derive a class from thread – Issue the start() method on the object Bouncing Ball Example Java Threads support – – – – Priorities Interrupts Thread groups Synchronization • synchronized keyword can be applied to classes or methods – Timers
  • 33. Java AWT Package AWT allows development of platform independent GUI programs – Java takes a “Lowest Common Denominator” approach • Supports only one mouse button • Doesn’t “look as nice” as native windows/MAC applications • Available graphical layout tools are quite limited. – Moderate set of graphic objects • Buttons, Labels, Text Input, Menus, • Scroll Bars, Dialogs, Radio Buttons – Mapped onto native GUI objects on each platform. “Layouts” are used to compensate for the fact that GUI objects may have different sizes on different platforms – Limited set of layouts are provided • • • • Flow Layout Border Layout Card Layout Grid (Bag) Layout – User defined layouts needed for most real programs HelloWorld Example
  • 34. Java for Physics? Java Compiler Experiment Extensions (Event Display) import physics.hep.*; GUI Network Data Analysis Engine Users Java Code Experiment Interface class MyAnalysis extends Analysis { public void init() { h = new Hist(“Energy”); } public void Evanal(Event e) { Enumeration x = e.getTracks(); while (x.hasMoreElements()) { Track t = x.getNextElement(); h.Add(t.getEnergy()); } } private Histogram h; }