Introduction to Java

advertisement
Introduction to Java
Presented by Daniel Rosenthal
Friday, November 16th, 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.
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.
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.
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.
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.
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
Java Terminology
Java 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.
More Java Terminology
Java 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.
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
Java and Blu-ray
Blu-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.
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.
Java Version History
Java 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
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)
Writing Programs in Java
Tools 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.
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
The Class Path
The 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.
Setting the Class Path
The 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.
About Class Paths
The 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.
More About Class Paths
The 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
Example class path
Here are some example class paths:
Windows:
CLASSPATH=.;C:\Program Files\Java\jre1.6.0_01\lib\rt.jar;C:\Tomcat6.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
Testing the Installation
Once 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
JDK Important Locations
• jdk/bin – contains command line tools
such as javac, java, javadoc, and jar
• jdk/jre – the location of the bundled
JRE
Java Language Basics
The simplest Java program:
Program.java:
Java Language Basics
The simplest Java program:
This program will do nothing.
Program.java:
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”.
Example Compilation on Windows
Running from the Command Line
Once 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.class
Exception in thread "main" java.lang.NoClassDefFoundError: Program/class
> java Program
//execution of Program
Example Run in Windows
Java Language Basics
A slightly more useful example:
This program will print “My program is running!” to the screen.
Program.java:
Running from the Command Line
Same steps as before:
> javac Program.java
> java Program
Example Run in Windows
Elements of a Java Program
Everything must be declared inside a class.
Classes in Java are analogous to classes in C++.
Program.java:
class Program
Elements of a Java Program
Classes can contain fields and methods.
Methods in Java are the equivalent of member functions in C++.
Fields in Java are the equivalent of data members in C++.
Program.java:
main() method
Elements of a Java Program
Members are either class or instance.
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.
Program.java:
main() (class method –
declared static)
Elements of a Java Program
Field example.
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.
Program.java:
number field
number is an instance
field because it is not
declared static
Elements of a Java Program
Every class and member must have a visibility.
A visibility indicates what places in a program the class, field, or method is
accessible from. Visibility keywords: public, private, and protected.
Program.java:
visibility specifiers
Java Tools – Javadocs
Javadocs 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/
Java SE API Documentation
Introduction to NetBeans IDE
Introduction to NetBeans IDE
Introduction to NetBeans IDE
The 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++.
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).
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
Web Programming (1 of 2)
Java Applets –
Run inside of web browser
Screenshots:
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:
Download