Java JavaTM
TM : A Quick Look
TM : A Quick Look Java TM : A Quick Look Java somchaip@chula.ac.th - - PowerPoint PPT Presentation
TM : A Quick Look Java TM : A Quick Look Java somchaip@chula.ac.th somchaip@chula.ac.th Programming Languages Programming Languages 1957 1957 : : Fortran Fortran 1959 1959 : : Cobol, Lisp Cobol, Lisp 1964 1964 :
TM : A Quick Look
1957 : : Fortran Fortran
1959 : : Cobol, Lisp Cobol, Lisp
1964 : : Basic Basic
1971 : : Pascal Pascal
1972 : : C, Prolog C, Prolog
1983 : : C C+ + + +
1995 : : Java Java
2001 : : C# , J# C# , J#
– – “ “The convergence of digitally controlled The convergence of digitally controlled consumer devices and computers consumer devices and computers. .” ”
– – * 7 went ch11 * 7 went ch11
– – Java Virtual Machine Java Virtual Machine – – Java API Java API
Solaris
Linux
Windows
Mac OS X
HP-
UX
IBM-
AIX, OS/390
PDA
Java-
enabled cell phones phones
GameBoy Advance Advance
...
Java 2
– – client client-
side general-
purpose applications
– – multi multi-
tier server-
centric applications
– – consumer and embedded devices consumer and embedded devices
– – “… “… software VLSI, end software VLSI, end-
to-
end, side-
to-
side, homogenous view of heterogeneity reality ... reality ...” ”
2003 1995
500 million desktop PCs run Java applets
3 million+ + professional Java developers professional Java developers
Of all Java developers ( (Evans Data, 2002 Evans Data, 2002): ):
– – 58% develop desktop applications 58% develop desktop applications – – 50% develop intranet applets 50% develop intranet applets – – 38% develop internet applets 38% develop internet applets
> 36 million Java runtime env
. downloads
JRE downloads surging, now about 3 million per month per month
3.7
JavaScript
10 3.8
Delphi/Pascal/Kylix
9 4.1
C#
8 6.1
SQL
7 9.5
PHP
6 15.2 (Visual) Basic 5 18.3
Perl
4 33.8
C+ +
3 36.7
C
2 47.4
Java
1
Ratings Program m ing Language Position
– – Write programs correctly Write programs correctly – – Write programs quickly Write programs quickly – – Write programs easily Write programs easily
http://research.microsoft.com/~toddpro/papers/disruptive.ppt
– – Life is too short, Java lets him do more Life is too short, Java lets him do more coding, less debugging coding, less debugging – – Java beats C and C+ + by a factor of 2 in Java beats C and C+ + by a factor of 2 in developer productivity. developer productivity.
Platform independence is the main reason for using Java (ComputerWorld’s survey)
– – strict compile time checking strict compile time checking – – strict run strict run-
time checking – – automatic garbage collection automatic garbage collection – – support exception handling & assertion support exception handling & assertion
– – no pointer no pointer – – verify verify bytecode bytecode before class loading before class loading – – sandbox sandbox
– – compiled to Java compiled to Java bytecode bytecode – – run on Java VM run on Java VM – – fixed sizes, formats, and behaviors of all fixed sizes, formats, and behaviors of all primitive data types primitive data types – – behave (almost) the same on multi behave (almost) the same on multi-
platforms
Write today, use next year.
Easy to maintain at a fixed level of functionality
– – bad code dies bad code dies – – good code lives on (modified, evolves, good code lives on (modified, evolves, becomes better) becomes better)
http://www.wgrosso.com/Articles/Presentations/PresentationonJavaasaProg.html
– – Very little need to worry about platform Very little need to worry about platform dependencies dependencies – – Widespread adoption of coding conventions Widespread adoption of coding conventions – – And large And large-
scale libraries
– – Write Half, Steal the Rest Write Half, Steal the Rest
http://www.wgrosso.com/Articles/Presentations/PresentationonJavaasaProg.html
– – generics, generics, enum enum, enhanced for, , enhanced for, autoboxing/unboxing autoboxing/unboxing, , varargs varargs, static , static import, metadata. import, metadata. increase expressiveness, increase safety
– – @author @author @deprecated @deprecated – – @ @param param @throws @throws – – @return @return @since @since – – @see @see ... ...
/* * * Returns the trigonometric cosine of an angle. Special cases: * < ul> < li> If the argument is NaN or an infinity, then the * result is NaN.< /ul> * < p> A result must be within 1 ulp of the correctly rounded * result. Results must be semi-monotonic. * * @param a an angle, in radians. * @return the cosine of the argument. * / public static double cos(double a) { return StrictMath.cos(a); } /* * * Returns the trigonometric cosine of an angle. Special cases: * < ul> < li> If the argument is NaN or an infinity, then the * result is NaN.< /ul> * < p> A result must be within 1 ulp of the correctly rounded * result. Results must be semi-monotonic. * * @param a an angle, in radians. * @return the cosine of the argument. * / public static double cos(double a) { return StrictMath.cos(a); }
rd party
Swing
– – Lightweight Lightweight – – Complex Complex – – Pluggable L&F Pluggable L&F – – Pure Java Pure Java – – Java 1.2, ... Java 1.2, ...
AWT
– – Heavyweight Heavyweight – – Primitive Primitive – – Single L&F Single L&F – – Not Pure Java Not Pure Java – – Java 1.0, ... Java 1.0, ...
JApplet
JFrame
JButton, , JCheckbox JCheckbox, , JRadioButton JRadioButton, , JComboBox JComboBox, , JDialog JDialog, , JLabel JLabel, , JList JList, , JSlider JSlider, , JTextComponent JTextComponent, , ... ...
Applet
Frame
Button, Checkbox, Choice, Dialog, Choice, Dialog, Label, List, Label, List, Scrollbar, Scrollbar, TextComponent TextComponent, ... , ...
– – create some remote objects create some remote objects – – make references to the objects accessible make references to the objects accessible – – wait for client to invoke methods on wait for client to invoke methods on these objects these objects
– – get remote references get remote references – – invoke methods on these references invoke methods on these references
Cross-
language, cross-
vendor interoperability is achieved via the IIOP (a transport protocol is achieved via the IIOP (a transport protocol for distributed app. written in either IDL or for distributed app. written in either IDL or Java RMI) Java RMI)
Java 1.1 : RMI
– – a natural outgrowth of RPC a natural outgrowth of RPC
Java 1.2 : Java IDL
– – API for interoperability and integration with CORBA API for interoperability and integration with CORBA
Java 1.3 : RMI over IIOP
– – enabled enabled remote remote objects
written in in Java Java to to be be accessible accessible from from any any language language via via IIOP IIOP
lang.
util.
– – logging logging – – perference perference – – collection collection – – regexp regexp – – jar, zip jar, zip
I/O & new I/O
networking
security
locale
JavaBean
XML
native interface
< < Collection> > < < Set> > < < List> > < < SortedSet> > < < Map> > < < SortedMap> >
General-
purpose
– – ArrayList ArrayList, , LinkedList LinkedList – – TreeSet TreeSet, , HashSet HashSet, , LinkedHashSet LinkedHashSet, , – – TreeMap TreeMap, , HashMap HashMap, , LinkedHashMap LinkedHashMap
Wrapper
– – synchronized synchronized – – unmodifiable unmodifiable
Convenience
– – Array.asList Array.asList – – ... ...
Legacy
– – Vector, Vector, Hashtable Hashtable
Special purpose
– – WeakHashMap WeakHashMap, , IdentityHashMap IdentityHashMap
Algorithms
– – sort, search, fill, ... sort, search, fill, ...
native method can
– – create, access, and update its own objects create, access, and update its own objects including objects passed to it including objects passed to it – – call Java methods call Java methods – – load and get information about Java classes load and get information about Java classes
class HelloWorld { public native void displayHelloWorld(); static { System.loadLibrary("hello"); } public static void main(String[] args) { new HelloWorld().displayHelloWorld(); } }
javac HelloWorld.java javah –jni HelloWorld HelloWorld.java
#include <jni.h> #include "HelloWorld.h" #include <stdio.h> JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) { printf("Hello world!\n"); return; }
cl -Ic:\java\include -Ic:\java\include\win32
HelloWorld.c
public void init() { Button btOK = new Button("OK"); btOK.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(“I’m OK”); } } ); add(btOK); Button btCancel = new Button("CANCEL"); btCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(“I was cancelled”); } } ); add(btCancel); }
Private Sub OK_Click() Debug.print "I'm OK" End Sub Private Sub CANCEL_Click() Debug.print "I was cancelled" End Sub public void onButtonPushed_OK() { System.out.println("I'm OK"); } public void onButtonPushed_CANCEL() { System.out.println("I'm Cancelled"); }
public void init() { ActionListener btListener = new ActionListener() { public void actionPerformed(ActionEvent e) { methodInvoke("onButtonPushed_" + e.getActionCommand()); } }; Button btOK = new Button("OK"); btOK.addActionListener(btListener); add(btOK); Button btCancel = new Button("CANCEL"); btCancel.addActionListener(btListener); add(btCancel); } public void methodInvoke(String metName) { try { Class c = this.getClass(); Method met = c.getMethod(metName, new Class[0]); met.invoke(this, new Object[0]); } catch (Exception ex) { ex.printStackTrace(); } }