170 likes | 189 Views
Learn about the basics of Java, its object-oriented nature, platform independence, and multi-threading. See a simple Java program and how to download and execute Java programs.
E N D
Introduction to Java Yangjun Chen Dept. Business Computing University of Winnipeg
Outline: Introduction to Java • What is Java? • - Java characters • A simple Java program • Java download and program execution
What is Java? • Java is a high level programming language that is very similar to C and C ++. • - You do not need to know these languages to learn Java, but they • would help. • Java is an object-oriented language. • Java is platform independent. • Java is multi-threaded.
procedure1 procedure2 procedureN Data files Java isObject-Oriented • In traditional computer languages, data are separated from • procedures as in C, Pascal, … (procedural language) • Prolog, and (declarative language) • LISP (functional language) • In Java, data are encapsulated by the procedures defined over them. Traditional languages: Java: data procedures
Java isObject-Oriented • Advantage of object-oriented programming • - Application independence • - Software reuse • - Easy maintenance of programs
Platform Independence • Platform • - A platform is basically some combination of hardware • and software running within a system: • CPU and operating system • - An executable program (object code) may not be executed • on different machines (platforms) due to the fact that each • machine has its own machine instruction set. • Java solves the platform-independence problem by using • bytecodes and the so-called Java Virtual Machine (JVM).
Platform Independence • JVM • - The JVM is some software that implements a virtual or • imaginary machine inside your computer. • - Accordingly, a Java program will be executed in two phases: • compiling and interpreting • The first phase (Java compiler) • - It compiles the program, but instead of generating machine • code specific to hardware, it generates bytecodes. • - Bytecodes are instructions that looks like machine code • but are not specific to any one processor.
Platform Independence • The second phase (Java interpreter) • - The interpreter will read in the bytecode and translate it • into native machine instructions. • - The Java bytecode interpreter is often called the Java • Virtual machine or the Java Runtime. • By the traditional language, a program is compiled into • machine code or processor instructions. • - These instructions are specific to the processor your • computer is running.
Platform Independence Java: Traditional compiler: Java code code Java Compiler compiling SPARC compiler Power compiler Pentium compiler compiling Java Bytecode interpreting SPARC Power PC Pentium SPARC Power PC Pentium
Java Virtual Machine • The JVM is different for each platform because each machine • has a different machine instruction set. • - But all JVMs take Java bytecode as input. • Interpreters are slower than compilers because they translate • as they go. • Using the “Just-In-Time” compiler, Java increases the execution • speed which rivals that of C++.
Java is Multi-Threaded • Multithreading can be loosely defined as the simultaneous • execution of multiple processes within a program. • Java is inherently multi-threaded. • - For example, multiple applets can run on the same web page with • each applet receiving more or less equal CPU time. • Downside to multi-threading • - Errors in using threads can be very difficult to locate.
A simple Java Program • Let’s take a look at a simple program • import java.lang.*; • //A simple Java application • public class HelloWorld { • public static void main(String args[]) { • System.out.println(“HelloWorld!”); • } • }
Java Download and Program Execution • Java Download (JDK1.1.8) • - http://java.sun.com/products/archive/jdk/1.1.8_010/index.html • (you’ll see a table.) • In the first item of the table, click “continue” button. • (you’ll see the license information.) • click “ACCEPT” button • jdk-1_1_8_004-win.exe will be stored in your computer. • - PATH and CLASSPATH setting (Windows 95/98) • Example: (in Autoexec.bat file) • PATH C:\Novell\Client32;%PATH%;C:\jdk1.1.8\bin • set CLASSPATH=.;jdk1.1.8\bin\..\classes;jdk1.1.8\lib\classes.zip
Java Download and Program Execution • Java Download (JDK1.4) • - http://java.sun.com/ • (you’ll see J2SE 1.4.2 SDK • Click on this, you’ll get another page for downloading • Click on “download” • You’ll see a table with title “Dowload J2SE v 1.4.2_03” • Click on “download” under “SDK” • (you’ll see the license information) • click “ACCEPT” button. • In the next page, click on • Windows Offline Installation, Multi-language • (j2sdk-1_4_2_03-windows-i586-p.exe, 48.30 MB)
Change path environment variable: • For windows 2000, • My Computer Control panel System • click on Tab “Advanced” • click on “Environment Variables …” • edit the system variable “Path” by adding • ;C:\J2SDK-1_4_2_03\bin • RealJ Download • Using Google or Yahoo to find ‘RealJ’ home page; • and then download it onto your computer.
Java Download and Program Execution • Program execution • - The code should be written in a plain text editor; not a word processor. • The file name must be of the form: <filename>.java; <filename> must • match the public class name stored in it. • Example: HelloWorld.java • - You would then compile the program using “javac”, the Java compiler. • Example: javac HelloWorld.java • - A class, for example, HelloWorld.class, will be produced. • - The final step is to run the program: • java HelloWorld • This will print “HelloWorld!” onto the screen.
What is the difference between the object-oriented method • and the traditional programming method with files? • What is the platform-independence?