Java Pros and Cons Roundtable American Nuclear Society
Java Pros and Cons − Roundtable American Nuclear Society Annual Meeting June 16, 2004 Pittsburgh, PA OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 1
Java – an Object-Oriented Programming Language · Based on objects: data and methods together · Four Concepts: - Abstraction – describes physical world Encapsulation – data and methods together Inheritance - just like something else but. . . Polymorphism – method overloading OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 2
Abstraction / Encapsulation public class Particle { Position position; public class Position { Direction direction; double energy; double x, y, z; public Position(double a, double b, double c){ x = a; y = b; z = c; } public void x = x + y = y + z = z + } public Particle(){ position direction = new Position(1, 0, 0); energy = 1. 0; } move(Direction d, double dis){ d. u*dis; d. v*dis; d. w*dis; public void move(double distance){ position. move(direction, distance); } public double how. Far. To(Position p){ double answer = (p. x-x)*(p. x-x) + (p. y-y)*(p. y-y) + (p. z-z)*(p. z-z); answer = Math. sqrt( answer ); return answer; } } = new Position(0, 0, 0); public void scat(double t, double p){. . . } } OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 3
Inheritance (1/2) public class Charged. Particle extends Particle { double rest. Mass; int charge; // Physical Constants static double ELEMCHARGE static double RESTMASSKG static double RESTMASS static double ERADIUS = = 1. 60217733 e-19; 9. 1093897 e-31; 0. 511; 2. 81792 e-15; // // Coulombs kg Me. V meter public Charged. Particle(){ super(); rest. Mass = Charged. Particle. RESTMASS; charge = -1; } public double get. Speed(){ double speed =. . . (energy-rest. Mass). . . return speed; } } OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 4
Inheritance (2/2) public class Photon extends Particle { // Physical Constants static double speed. Of. Light = 299792458. 0; // m/s static double planck. Constant = 6. 6260755 e-34; // Joule seconds public Photon(){ super(); } public Particle[] compton. Scatter(){ Particle[] answer = new Particle[2]; // photoelectron answer[0] = new Charged. Particle(. . . ); // scattered photon answer[1] = new Photon(. . . ); return answer; } } OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 5
Polymorphism public class Particle { Position position; Direction direction; double energy; public Particle(){ position = new Position(0, 0, 0); direction = new Position(1, 0, 0); energy = 1. 0; } public Particle(Position p, Direction d, double e){ position = p direction = d; energy = e; } public Particle(Particle p){ position = p. position; direction = p. direction; energy = p. energy; } } OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 6
Java Pros · · Free, freely distributable Portable – “Write once, run anywhere” Internet savvy Extensive libraries – database, GUI, networking, etc. · Language – strongly typed, array indices are checked, automatic garbage collection, Threads, Exceptions · *. jar files – Java archive, runnable OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 7
Java Cons · Interpreted OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 8
OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 9
OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 10
OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 11
OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 12
OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 13
OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 14
OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 15
OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 16
OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 17
Java 1. 5 “Tiger” · Generics - no casting, errors at compile time, not runtime · Enhanced for Loop · Autoboxing/Unboxing collections of primitive data types · Formatted Output - more like C or FORTRAN · New JVM Profiling API · Improved Diagnostic Ability - get. Stack. Trace, Thread. get. All. Stack. Traces private void print. Collection(Collection c) { Iterator<String> i = c. iterator(); while(i. has. Next()) { String item = i. next(); System. out. println("Item: "+item); } } public void new. For(Collection<String> c) { for(String str : c) { System. out. println(str); } } System. out. printf("%s %3 d", name, age); OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 18
Sun Microsystems, Inc. Binary Code License Agreement for the JAVATM 2 SOFTWARE DEVELOPMENT KIT (J 2 SDK), STANDARD EDITION, VERSION 1. 4. 2_X 3. RESTRICTIONS. . You acknowledge that Licensed Software is not designed or intended for use in the design, construction, operation or maintenance of any nuclear facility. . OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 19
Sun Microsystems, Inc. Binary Code License Agreement for the JAVATM 2 SOFTWARE DEVELOPMENT KIT (J 2 SDK), STANDARD EDITION, VERSION 1. 4. 2_X 3. RESTRICTIONS. . You acknowledge that Licensed Software is not designed or intended for use in the design, construction, operation or maintenance of any nuclear facility. . OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 20
Any Questions? OAK RIDGE NATIONAL LABORATORY U. S. DEPARTMENT OF ENERGY 21
- Slides: 21