350 likes | 604 Views
Java Background v1.2. topics. simplest Java program Java platform overview getting the software you need why did Sun create Java? Files (.java, .class, .jar). 1 the simplest Java program. simplest program in C and Java. public class Hello {
E N D
topics • simplest Java program • Java platform overview • getting the software you need • why did Sun create Java? • Files (.java, .class, .jar) Java_background..ppt
1 the simplest Java program Java_background..ppt
simplest program in C and Java public class Hello { public static void main(String[] args) { System.out.println("Hello in Java"); } } #include <stdio.h> int main() { printf(“Hello in C\n"); return 0; } Like C, Java is case-sensitive, and statements end with ; You need a “console” to see the results. Java file name must be Hello.java. Java_background..ppt
simplest program in Java – using Window from library import java.awt.*; import javax.swing.*; publicclass Hello { publicstaticvoid main(String[] args) { JFrame myWindow = new JFrame(); myWindow.setSize(new Dimension(400,275)); myWindow.setTitle("Hello in Java"); myWindow.setVisible(true); } } Uses the JFrame class in the library to create a Window. Java file name must be Hello.java. Java_background..ppt
simplest program in Java – extending Window from library import java.awt.*; import javax.swing.*; publicclass Hello extends JFrame { publicstaticvoid main(String[] args) { new Hello().run(); } privatevoid run() { this.setSize(new Dimension(400,275)); this.setTitle("Hello in Java"); this.setVisible(true); } } Extends the JFrame class in the library to create a Window. Java_background..ppt
2 Java platform overview Java_background..ppt
Java is a platform • Java programs require a Java Virtual Machine (JVM) to run • the JVM is available for Linux, Windows, Mac and Solaris • commonly also called the Java Runtime Environment (JRE) • free download from Sun • the JRE is pre-installed in Mac OS X • Windows users have to download and install it themselves • NOTE: the JRE does not include the Java compiler • developers need the JDK instead (or in addition to) • the Java compiler is in the Java Development Kit (JDK) • installing the JDK gives you the compiler and the JRE • the JDK does not include an integrated development environment (IDE) • you can one of several free IDE’s for Java • Eclipse (free from IBM) • Netbeans (free from Sun) • BlueJ (free from Monash University) Java_background..ppt
Java’s Just in Time (JIT) compiler bytecode Java source compiler .class file metadata at run time native code JIT Compiler at compile time .java .class JIT compilation occurs the first time a method is called, and only if it’s called (cached in memory) Java_background..ppt
the JDK includes lots of libraries • Java programs can call a huge body of pre-written code • these reusable components are called the Class Libraries • sometimes they are also called “packages” • they are also called “the Java API’s” • the libraries are designed to be used identically regardless of OS Java_background..ppt
who implements JVM’s? Sun Microsystems • Java HotSpot Virtual Machine for Windows, Linux and Solaris Hewlett-Packard • Java runtime for HP-UX, OpenVMS, Tru64, Reliant(Tandem) UNIX) IBM • Java runtime for MVS, AIX, OS/400, z/OS Apple Computer • MacOS Runtime for Java (MRJ) • the full version of J2SE, version 1.4.2, is part of Mac OS X • includes the JDK (compilers, etc.) and the HotSpot Virtual Machine BEA Systems • JRockit (for their web server) Java_background..ppt
3 getting the software you need Java_background..ppt
versions of Java • Java 1 - 1996 • Java 1.0: original, not a very good version • Java 1.1: greatly revised and improved (1997) • Java 2 - 1998 • Java 1.2: includes “Swing” (for graphical user interfaces) • Java 1.3: more new packages, no new syntax (2000) • Java 1.4: introduced the assert statement (2002) • Java 5 - 2004 • Java 1.5: quite a bit of new syntax, including generics • Java 6 – 2006 • Faster runtime and other new features • I am using JDK v6.0 SE on my laptop • Its runtime is faster than the 5.0 one Java_background..ppt
editions • Java SE (Standard Edition) is for desktop applications (a.k.a. J2SE) • Java EE (Enterprise Edition) is for web servers (a.k.a. J2EE) • Java ME (Micro Edition) is for devices such as pagers and mobile phones (a.k.a. J2ME). Java_background..ppt
what languages require a JVM? • Java used to be pretty much the only language • there are now efforts by other languages to use JVM • Jython (formerly known as JPython) • except for some standard modules, Jython programs use Java classes instead of Python modules • Jython was fairly complete • its development stopped in 2005 when Microsoft hired its creator to work on IronPython ( which targets .NET instead) • JRuby • tightly integrates Ruby with Java, with full two-way access between the java and the Ruby code. • this year (Sept.), Sun Microsystems hired JRuby's two creators to work on JRuby full time • Groovy is a new ‘dynamic language’ targeted at the JVM Java_background..ppt
the three programs you need to write Java • The Java Runtime Environment (JRE) • this is the virtual machine that ‘executes’ Java programs • the Java compiler and associated utilites • a way to edit source code • you’ll want an integrated development environment (IDE) with: • an editor for source code • a debugger, to help you find your mistakes • a viewer to see the parts of your program • an easy way to compile and run programs • an easy way to view documentation • some good IDE’s for Java: • BlueJ (free) I used this one first • Eclipse (free from IBM) I use this one now • Netbeans (free from Sun) well regarded; I haven’t used it Java_background..ppt
what I am using • Java Development Kit (JDK) 5.0 of the J2SE • from http://java.sun.com/javase/downloads/ • get the JDK, not the JRE • get the Standard Edition (not the Enterprise Edition) • the JDK gives you both the runtime and the compiler • it’s OK if your machine already has a JRE installed • if your computer can’t run Java 5, get Java version 1.4.2 • Eclipse IDE • from http://eclipse.org/ (current version is 3.2.1) • unzip into a folder • run eclipse.exe • more details at http://harbormist.com/AcmJava NOTE: Java SE (Standard Edition) is for desktop applications (a.k.a. J2SE) Java EE (Enterprise Edition) is for web servers (a.k.a. J2EE) Java ME (Micro Edition) is for devices such as pagers and mobile phones. Java_background..ppt
the Eclipse welcome screen go to the workbench overview tutorials samples what’s new Java_background..ppt
the Java “perspective” Java_background..ppt
projects in the workspace • In Eclipse, classes are organized into projects (complete programs) • each project is kept in a folder • Eclipse projects are kept in a workspace, which is just a folder containing project folders • when you first start Eclipse, you will be asked to choose a workspace (folder) • you can set this as default, so you use the same workspace every time • you can tell Eclipse to ask you for the workspace each time it starts up Java_background..ppt
starting a new project • choose File New Project... Java Project and give your new project a name • this will also be the name of the folder that is created in the workspace • choose File New Class and give your new class a name • you can also check the box that asks Eclipse to generate a main method for you • start editing! Java_background..ppt
compiling and running • you don’t compile your program; Eclipse does that automatically as you type • it also points out errors as you go along • the first time you run your program, choose Run Run As... Java Application • select the default package first, then right-click • after that, you can just click the green circle with a triangle in it • or, you can use the drop-down list to the right of the green circle to choose a particular program to run Java_background..ppt
importing code into Eclipse • if you have a program that is outside Eclipse: • on the file system, create a folder for the project in your Eclipse workspace folder • move all the .java source files to that folder • choose File New Project... Java Project and type in the name of your new folder • Eclipse will recognize it and ask if you want to make the folder into a new project; just click Finish • alternative method: • In Eclipse, create an empty project with the desired name • Choose File Import… and follow instructions to import .java files from the file system into Eclipse Java_background..ppt
4 why did Sun create Java? Java_background..ppt
C and C++ perceived common problems • pointers are dangerous • memory leaks (failing to free memory correctly) • function pointers (jumping to the wrong place) • data pointers (pointing to the wrong place) • manual garbage collection is a lot of work • multiple inheritance (C++) can get very complicated • ambiguities like the “diamond problem” (a.k.a. “diamond of death”) • not easily portable across platforms, even with re-compile and discipline Java_background..ppt
a few ways Java improved on C++ • instead of pointers, Java has references • references are similar to pointers, but with protections (cannot jump into illegal parts of memory)—avoids segmentation fault problems • automatic garbage collection • memory is reclaimed from the heap automatically—avoids memory leaks • single inheritance • ambiguity and complexity of multiple inheritance avoided • encapsulation • all code must be in a class—intended to encourage information hiding • array bounds checking • libraries • many common tasks already coded and available for “reuse” by means of inheritance • many interfaces (behaviors) already coded Java_background..ppt
5 Files (.java, .class, .jar) Java_background..ppt
What is a .class file? code.java compiler code.class • javac takes a text file as input, and creates a binary file as output • .class files contain two main things: • bytecode • instructions for the Java Virtual Machine • at runtime, bytecode gets JIT-compiled into native code on the fly • metadata • a catalog of resources • a description of the class contents that will be available at runtime by means of “reflection” Java_background..ppt
what is a .jar file? • Java Archive files (.jar extension) contain: • one or more .class files • resources • strings • images • Sounds • .jar files may also contain .java source files (optional) • if a JRE is present on a system, .jar files should be registered to run on the JRE • double-clicking the .jar should run the JRE with the .jar file as input • the JVM must be in the search path • the /lib and /bin folders for the JRE must be in the $CLASSPATH • a .jar may reference other .jars if they can be found Java_background..ppt
the single-file application • monolithic application • all source code compiled into one .JAR file APP.jar Java_background..ppt
component-based application • component-based application .JAR file + one or more other .JAR files compute.jar APP.jar data.jar Java_background..ppt
where do Java library .JAR files reside on the file system? • $CLASSPATH environment variable • user-level, not system-level, if on Windows • programmers must tell the compiler which .JAR files a program will need • in Eclipse, you do this by opening the project Properties • add one or more External .JAR files to the Java Build Path (demo) Java_background..ppt
jargon checklist • what is a .jar file? .java? .class? • what is “metadata”? • What is bytecode? • What is the JVM? • what is the JRE? • what is “the runtime”? • what is JDK? • What are J2SE, J2EE and J2ME? • What is an IDE? Java_background..ppt
what we just covered • simplest Java program • Java platform overview • getting the software you need • why did Sun create Java? • Files (.java, .class, .jar) Java_background..ppt
the end of this PowerPoint file Hooray! Java_background..ppt