Introducing Java
Introducing Java
Objectives
Appreciate Java as a language,
Write, compile and execute simple java program in command
prompt and eclipse
Table of Content
Java Language
JVM: perspectives
Classpath
Interpreter vs JIT
Error handling in java
Introducing Eclipse Helios 3.6 IDE Some Guidelines for Doc comments
Running Different Workspace
javadoc
UI Overview
Nesting comments
Annotations
3
Java Language
Java was created by a team of members called "Green" led by James
Arthur Gosling.
When Java was created in 1991, it was originally called Oak.
It is a free and open source software (FOSS) under GNU General
Public License(GPL)
First version of Java was released in 1995.
Java is an Object oriented language, simple, portable, platform
independent, robust and secure.
We will be focusing on Java 1.6.
Flavors Of Java
JSE
Java Standard Edition formerly known as J2SE.
This forms the core part of Java language.
JEE
Java Enterprise Edition formerly known as J2EE.
These are the set of packages that are used to develop distributed
enterprise-scale applications.
These applications are deployed on JEE application servers.
JME
Java Micro Edition formerly known as J2ME.
These are the set of packages used to develop application for mobile
devices and embedded systems.
Our focus
What is an IDE?
Setup JDK
http://java.com/en/download/index.jsp or find appropriate link in
http://www.oracle.com/technetwork/indexes/downloads
Download Java based on the type of OS
Windows
Linux
Mac OS
Solaris
Install JDK
Java Executables
Java Predefined Classes like
String, Date, Math etc.
Hello.java
Hello.class
compilation
Source code
execution
Execute
C:\MyJava>java Hello
10
java
11
12
Eclipse as an IDE
Java Development Tooling (JDT) is used for building Java code
Provides set of workbench plug-ins for manipulating Java code
Java projects, packages, classes, methods, ....
Java compiler is built in
Used for compiling Java code
Creates errors (special markers of code) if compilation fails
Numerous content types support
Java, HTML, C, XML, ...
13
14
15
16
17
UI Overview
Closing the Welcome page gets you to the Eclipse workbench user interface
18
19
20
21
22
Exercise
Write a java program to display the following
*
*
*
*
*
*
a) Use print and println statements.
b) The class file of this program should be automatically placed inside
Design folder while compiling.
c) Display the version used for compiling.
( 15 mins)
23
Simple
Object oriented language
Portable and platform independent
Robust
Multithreaded
Dynamic Linking
Secure
Performance
24
25
Portable
When java code executes, its behavior is exactly same in any javaaware system.
There are no platform-specific code in java programs that causes
compilation problems in any other OS.
This makes Java programs are portable.
26
Platform Independent
A Java program requires JVM (part of JRE) to execute Java code.
When java application starts to executes, Java Virtual Machine also
starts.
Bytecode has instructions that Java Virtual Machine can understand
and execute.
JVM converts the Bytecode to machine specific code.
Java Bytecode can be copied on to any machine that has JVM and
executed. This is what makes Java Platform Independent.
Write Once, Run any where
27
JVM: perspectives
JVM can be looked as
a runtime instance: JVM life cycle begins when applications
starts to run ( that is when main method is called) and ends
when the application ends.
the abstract specification: Specification that Java team at Sun
(Oracle) provides which tells JVM makers how they must design
JVM for their OS.
a concrete implementation: JVM that is built specifically targeted
for an OS based on abstract specification .
28
Interpreter vs JIT
Java Bytecodes were originally designed to be interpreted by JVM
meaning bytecode are translated to machine code without it being
stored anywhere.
Since bytecode verifier (which is part of JVM) performs runtime
checks, line by line execution was important.
Since speed became an issue, Just-in-Time Compilation (JIT) came
into being. JIT converts chunks of code, stores it temporarily in
memory and then executes the converted code.
JIT compilers are typically bundled with or are a part of a virtual
machine and do the conversion to native code at runtime, on
demand.
The compiler also does automatic register allocation and some
optimization when it produces the bytecodes.
Therefore, JIT is hybrid compiler.
29
Classpath
Classpath is an environment variable that java compiler and JVM
(system class loader) use to locate the classes in the file system.
To set the classpath temporarily in command line
SET CLASSPATH=directory;%CLASSPATH%;.
Command to set classpath while compiling
javac classpath dir1;dir2 Someclass.java
Example: javac classpath C:/MyJava
Command to specify un-compiled source file to be compiled and
included in the classpath
javac sourcepath dir1;dir2 Someclass
Example: javac sourcepath C:/MyJava
Providing classpath while executing
java classpath directory1;directory2
32
Multi-line comment: /* */
/* this is multi
line comment */
34
javadoc
Tool that is used to produce HTML pages by parsing through the
documentation comment in java source code.
Produces documentation for public and protected classes/members.
Works on entire package or a single source file
Usage on source file
javadoc sourcefilename.java
javadoc College.java
36
Nesting comments
Valid Nesting
/* // */
/** // */
///* */
// /** */
Invalid Nesting
/* */ and /** */ nested with itself and nested with each
other gives error.
/* /* */ */
/** /* */ */
37
Annotations
Annotations are extra text starting with @ symbol that are added in
the Java program to provide information about a program.
Annotations do not directly affect program semantics.
They provide information about the program to tools and libraries,
which can in turn affect the semantics of the running program.
Annotations can be read from source files, class files, or reflectively
at run time.
38
Uses
Annotations for the compiler
Example:
@SuppressWarnings : can be used by the compiler to
detect errors or suppress warnings
Annotations for the application servers and tools
Application server tools can process annotation information to
generate code etc for services it provides like security etc.
javadoc tool uses annotations like
@author, @version, @param, @return
Runtime processing Some annotations are available to be
examined at runtime.
39
Exercise
Provide java doc comments for the class created in the previous
exercise and generate HTML document files. Use documentation
annotation.
(10 mins)
40
Summary
Java was created by a team of members called "Green" led by James
Arthur Gosling in 1991 and it was originally called as Oak.
IDE is where the Java programs are written, compiled and executed.
Simple, Object oriented language, Portable and platform independent,
Robust, Multithreaded, Dynamic Linking, Secure and Performance are
some of the features of Java Language
Automatic garbage collection is an integral part of Java.
Classpath environment variable is used to locate classes in the file system.
Javadoc is a tool that is used to produce HTML pages by parsing through
the documentation comment in java source code.
Annotations are extra text starting with @ symbol that is added in the Java
program to provide information about a program.
41