Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

46
Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th , 2007

Transcript of Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Page 1: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Introduction to Java

Presented by Daniel Rosenthal

Friday, November 16th, 2007

Page 2: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

What is Java?

Java is a cross platform, object-oriented programming language.

It was created by Sun Microsystems and first made publicly available in January 1996.

Page 3: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java History (1 of 2)Java was designed and implemented by James Gosling.

The Java language is specified by the book The Java Language Specification, Third Edition by James Gosling, Bill Joy, Guy Steele, and Gilad Bracha (ISBN: 0321246780).

It started in 1991 as a project, code-named “Green”, to develop a platform for consumer devices, such as cable tv switchboxes. Only later when the world wide web caught on in 1995 was the project refocused towards creating a general purpose programming language.

Page 4: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java History (2 of 2)Java was originally called Oak, because of a tree

outside of the window of James Gosling’s office at Sun, and was later renamed to Java when it was discovered a language named Oak already existed.

The name “Java” was chosen because the creators of the language often discussed ideas for the language at a local coffee shop.

Page 5: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

What makes Java unique?Java is run inside a piece of software known as a

Java Virtual Machine.

In many languages, such as C and C++, the source code (.c and .cpp files) is portable. What makes Java unique is that Java’s executable code is also portable (.class files).

This means that the same application can be run without modification on any system that has a Java Virtual Machine without being recompiled.

Page 6: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

What makes Java unique? (cont.)

Java programs require a Java Virtual Machine to run.

The Java Virtual Machine is written in C and C++, and has been ported to many platforms, including desktop computers running Windows and Mac OS X, to high-end servers running Solaris and Linux, to mobile phones and PDA’s.

Page 7: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Advantages of Java• Available on almost all platforms• Standardized (by Sun)• Well documented• Makes many traditionally difficult tasks very

easy, including networking, multithreading, and cross-platform GUI programming (GUI stands for graphical user interface)

• Very large API• Most skills learned while using Java apply

elsewhere

Page 8: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java TerminologyJava Virtual Machine (JVM) – An abstract machine architecture

specified by the Java Virtual Machine Specification, Second Edition by Tim Lindholm and Frank Yellin (ISBN: 0201432943).

Java Runtime Environment (JRE) – A runtime environment which implements Java Virtual Machine, and provides all class libraries and other facilities (such as JNI) necessary to execute Java programs. This is the software on your computer that actually runs Java programs.

Note:

These two terms (JVM and JRE) are often used interchangeably, but it should be noted that they are not technically the same thing.

Page 9: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

More Java TerminologyJava Development Kit (JDK) – The basic tools necessary

to compile, document, and package Java programs (javac, javadoc, and jar, respectively). The JDK includes a complete JRE.

Page 10: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java Platforms

There are three main platforms for Java:

• Java SE (short for Standard Edition) – runs on desktops and laptops

• Java ME (short for Micro Edition) – runs on mobile devices such as cell phones

• Java EE (short for Enterprise Edition) – runs on servers

Page 11: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java and Blu-rayBlu-ray – Sony’s high definition disc format (built-in support

on PlayStation 3). Holds 25 GB per side.

The Blu-ray standard specifies that a Java Runtime Environment must be present in all Blu-ray Disc players.

The name of the Java platform on Blu-ray Disc players is called BD-J.

Page 12: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java Versions

Sun has a history of choosing poor version numbering schemes, and for renumbering a version after it has been publicly released. The names of the platforms have also been changed after being publicly released.

Page 13: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java Version HistoryJava 1.0 (JDK 1.0) – Released January 1996

Java 1.1 (JDK 1.1) – Released in February 1997

Java 1.2 (JDK 1.2) / J2SE 1.2 (J2SE SDK 1.2) – Released in December 1998• Development Kit initially named JDK 1.2 but renamed three days after release to

J2SE SDK 1.2 (short for Java 2 Standard Edition)• Java 2 Micro Edition (J2ME) and Java 2 Enterprise Edition (J2EE) released

Java 2 Standard Edition 1.3 (J2SE SDK 1.3) – Released in May 2000

Java 2 Standard Edition 1.4 (J2SE SDK 1.4) – Released in February 2002

Java 2 Standard Edition 5.0 (J2SE SDK 5.0) – Released in September 2004• Originally named 1.5 – many official documents still refer to this as version

1.5.0

Java SE 6 (Java SE SDK) – Released in December 2006• J2SE SDK renamed back to Java SE SDK

Page 14: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Current Java Versions

The most current versions of the respective releases of Java are:

Java SE 6

Java EE 5

Java ME 1.1 (depends on configuration)

Page 15: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Writing Programs in JavaTools required to write Java programs:• Java Development Kit 6 (JDK 6). This can be obtained

from java.sun.com. Specifically: http://java.sun.com/javase/downloads/index.jsp

After downloading and installing the most recent JDK, you must set your CLASSPATH and JAVA_HOME environment variables. JAVA_HOME should be the location of your JDK installation.

If you want to use Java from the command line, you must also add the jdk/bin directory to your PATH variable.

Page 16: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java Development Kit (JDK)

Comes with all tools necessary to compile and run Java programs, including a complete Java Runtime Environment (JRE) (located in the jdk/jre directory).

Get the most recent version from: http://java.sun.com/javase/downloads/index.jsp

Page 17: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

The Class PathThe class path is a list of locations (folders) that

are searched for classes when the Java Virtual Machine attempts to locate a referenced class.

The CLASSPATH variable is an environment variable that specifies the class path on a given system. The CLASSPATH must be set and must include the current directory in order to run Java programs from the command line with the java command.

Page 18: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Setting the Class PathThe CLASSPATH variables simply contains a list

of absolute directories, separated by a system dependent separator. On Windows a semicolon “;” is used to separate directories, on Linux, Unix, Mac OS X, and Solaris, a colon “:” is used to separate directories.

The reason for the discrepancy between Windows and Unix flavored operating systems is that absolute file paths in Windows can contain colons “:”, for example “C:\Program Files”, so colons can not be used to identify a boundary between path entries as it can be in Unix. This is the same reason why the PATH separator is different in Windows and Unix.

Page 19: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

About Class PathsThe class path must contain the current directory “.” or the

Java interpreter (the java command) will be unable to find compiled class files and you will get an error similar to:

Exception in thread "main" java.lang.NoClassDefFoundError: SomeClass/java

The Java compiler (the javac command) automatically looks in the current directory when compiling files, but the Java interpreter only looks in the current directory if the class path is undefined. You would then be able to compile classes, but be unable to run them.

Page 20: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

More About Class PathsThe standard Java SE class libraries are located in jre/lib/rt.jar

Any class libraries (.jar files) located in the jre/lib/ext directory are also available to Java programs.

You don’t need to worry about adding jre/lib/rt.jar or jre/lib/ext to your classpath because both of these are searched automatically by both java and javac

Page 21: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Example class path

Here are some example class paths:

Windows:CLASSPATH=.;C:\Program Files\Java\jre1.6.0_01\lib\rt.jar;C:\Tomcat-

6.0.13\lib\servlet-api.jar;C:\Tomcat-6.0.13\lib\jsp-api.jar;C:\Program Files\Java\jre1.6.0_02\lib\ext\QTJava.zip

Linux:CLASSPATH=.:/usr/java/jdk1.5.0_03/jre/lib/rt.jar

Page 22: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Testing the InstallationOnce you have installed the JDK, set the

CLASSPATH and JAVA_HOME environment variables, and updated your PATH variable, type the following on a command line:

> javac –version

You should get the following output:javac 1.6.0

Page 23: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

JDK Important Locations• jdk/bin – contains command line tools

such as javac, java, javadoc, and jar• jdk/jre – the location of the bundled

JRE

Page 24: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java Language Basics

Program.java:

The simplest Java program:

Page 25: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java Language Basics

This program will do nothing.Program.java:

The simplest Java program:

Page 26: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Compiling from the Command Line

To compile, type javac <source file> where “<source file>” is the name of the file you want to compile.

For example:

> javac Program.java

This will output a file named “Program.class”.

Page 27: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Example Compilation on Windows

Page 28: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Running from the Command LineOnce compiled, use the java command to run. The

java command takes a single argument which is the name of the class containing a main() method that you wish to execute. You only need to specify the name of the class, not the full name of the .class file; if you specify the full name of the .class file, you will get an error.

> java Program.classException in thread "main" java.lang.NoClassDefFoundError: Program/class

> java Program//execution of Program

Page 29: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Example Run in Windows

Page 30: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java Language Basics

A slightly more useful example:

Program.java:

This program will print “My program is running!” to the screen.

Page 31: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Running from the Command Line

Same steps as before:

> javac Program.java

> java Program

Page 32: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Example Run in Windows

Page 33: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Elements of a Java Program

Everything must be declared inside a class.

Program.java: class Program

Classes in Java are analogous to classes in C++.

Page 34: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Elements of a Java Program

Classes can contain fields and methods.

Program.java: main() method

Methods in Java are the equivalent of member functions in C++.

Fields in Java are the equivalent of data members in C++.

Page 35: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Elements of a Java Program

Members are either class or instance.

Program.java: main() (class method – declared static)

Instance members exist for every object (instance) of a class (this is the default).

Class members only have one copy per class, and are declared with static.

Page 36: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Elements of a Java Program

Field example.

Program.java:

Instance members exist for every object (instance) of a class (this is the default).

Class members only have one copy per class, and are declared with static.

number field

number is an instance field because it is not declared static

Page 37: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Elements of a Java Program

Every class and member must have a visibility.

Program.java:

A visibility indicates what places in a program the class, field, or method is accessible from. Visibility keywords: public, private, and protected.

visibility specifiers

Page 38: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java Tools – JavadocsJavadocs are html documentation files

generated by the javadoc tool, which generates html documentation from source code (it looks for comments of the form /** … */).

Javadocs for the Java SE API’s are online:http://java.sun.com/javase/6/docs/api/

Page 39: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Java SE API Documentation

Page 40: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Introduction to NetBeans IDE

Page 41: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Introduction to NetBeans IDE

Page 42: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Introduction to NetBeans IDEThe IDE in NetBeans IDE stands for Integrated

Development Environment.

IDE’s are used by professional programmers to accelerate the completion of certain tasks.

NetBeans is Sun’s free, open source IDE for Java (it is also written in Java, which means it is available on any platform that Java is available on). NetBeans supports plugins for working in other languages as well, such as C and C++.

Page 43: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

About NetBeans IDE

Website at http://www.netbeans.org/.

Current version of NetBeans is 5.5 with the release of 6.0 expected by the end of this year or the beginning of next year (version 6.0 is currently a release candidate).

Page 44: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Cool Features of NetBeans• Syntax highlighting• Code completion• Integrated debugger• Automatic project build facilities using

Apache Ant• Code refactoring, regular expression

search and replace• Matisse GUI builder

Page 45: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Web Programming (1 of 2)Java Applets –

Run inside of web browser

Screenshots:

Page 46: Introduction to Java Presented by Daniel Rosenthal Friday, November 16 th, 2007.

Web Programming (2 of 2)Java Web Start –

Launches Java programs outside of browser with fewer security restrictions than applets (but more security restrictions than regular desktop Java programs)

Screenshots: