Java introduction
Java Language History
• 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 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 look at Java 1.8.
Flavours Of Java
• JSE
o Java Standard Edition, formerly known as J2SE
o Is a part of Java language
• JEE
o Java Enterprise Edition, formerly known as J2EE
o Is a set of packages used to develop distributed
enterprise-scale applications
o The applications are deployed on JEE application
servers
• JME
o Java Micro Edition was formerly known as JME.
o These are the set of packages for application
development for mobile devices and embedded
systems
Sample Java Program
Hello.java
public class Hello{
public static void main(String args[]) { Main () is a
System.out.println (“Hello World!"): method from
}} where program
execution starts in
java
Statement used to display values on the console ‘println’ causes
the output on new Iine.
Java Development Environment for Compilation & Execution
. Compile java programs
• From command prompt
➢ Through an IDE (Integrated development
environment)
• Eclipse ->Apache
➢ NetBeans >Oracle SON
➢ JBuilder -> Borland
➢ Integrated Development Environment -> IDE
Introducing Eclipse – IDE
• IDE stands for Integrated Development Environment It is
where the’ Java programs are written, compiled and
executed
• Eclipse is an open-source project
o Launched in November 2001
o Designed to help developers with specific
development tasks
• GUI and non-GUI application development support
• Easy integration of tools
• Supported by multiple OS like Windows, Mac, Linux,
Soars, IBM
• AIX and HP-UX.
• Java Development Tooling (JD) is used for Java code build.
• Provides a set of workbench plug-ins for manipulating Java
code
o Java projects, packages, classes, methods, …
• Java compiler is built-in
o Used for compiling Java code
o Creates errors (special markers of code) if
compilation fails
• Numerous content types support
o Java, HTML, C, XML, …
Working with Different Workspaces
• It is possible to run different workspaces using command
prompt
o Data argument must be used with eclipse.exe
• Workspace location must be specified
• Useful for grouping project specific data
• Multiple workspaces can run at the same time
Features of Java Language
• Simple
• Object oriented language:
• Portable and platform independent
• Robust
• Multithreaded
• Dynamic Linking
• Secure
• Performance
Portable & Platform Neutral
• Java Code can be compiled anywhere
• Bytecode can be executed anywhere
Portable
• When java code executes, is behaviors exactly same in any
aware system
• There are no platform-specific code in java programs that
causes compilation problems in any other OS.
• This makes Java program portable
Platform Independent
• A Java program requires a JVM (part of the JRE) to execute
Java code, When Java application starts, 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 Bytecodes can be copied onto any machine that has
JVM and executed. This is what makes Java Platform
Independent.
• “Write Once, Run anywhere’
• Is JVM platform independent? NO IT’S DEPENDENT
JVM: Perspectives
• JVM can be looked as
o A runtime instance: JUM Ife cycle begins when
applications. Stars to run (that is, when main method
is called) and ends when the application ends.
o The abstract speciation: Specification that Java team
at Sun (Oracle), provides which tell JVM makers now
they must design JVM for their OS.
o A concrete implementation: JVM that is built
specifically targeted for n OF based on abstract
specification
Interpreter vs. JIT
• Java Bytecodes were originally designed to be interpreted
by the JVM meaning bytecodes are translated to machine
code without it being stored anywhere.
• Since bytecodes verifier (which is part of the JVM)
performs runtime checks, line by
line execution was important.
• Since speed became an issue, Justin-Time Compilation (JIT)
came into being. JIT converts chunks of code, stores
temporarily in memory and then executes the converted
code.
• JIT compilers are typically bundled with or are a part of
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 campers.
Handling Errors in Java
• Compilation error: generated by the compiler. Examples of
situations where it arises:
o Incorrect syntax, racket mismatch, if keywords are
used as variables
o Using uninitialized variables
o Unreachable code: while (false) {..}
o Strong type checking
• Run-time error: generated by the JVM.
• Examples of situations when it arises:
o Attempt to divide an integral value by 0 or access
array index beyond the defined range.
o Trying to access a class that does not exist at runtime.
(What happens if you delete Hello class and then run
Hello.
• Java has a strong exception handling mechanism that allows
Programmers to handle such errors and come out of the
situation Graceful.
Automatic Garbage Collection
• The garbage collector is a tool that frees up any
unreferenced memory (memory occupied by objects that
are no longer in use by the program) in programs.
• Automatic garbage collection is an integral part of Java and
its run-time system,
• Java technology has no pointers. Hence, no question of
allocating and freeing memory by programmers.
• Does that mean Java does not support dynamic memory
allocation?
➢ No. It means that Java takes care of memory and
relieves the. Programmers from memory-related
hassles.
• java—verbose:gc can be used to get more information about
garbage collection process.
Classpath
• Classpath is an environment variable hat a Java compiler
and JVM (system class loader) use 10 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 dirt;dir2 Someclass.java
Example: javac-classpath C:/MyJava
• Command to specify un-complied source file to be compiled
and included in the classpath
Javac — sourcepath dir1;dir2 Someclass
Example: javac -sourcepath C:/MyJava
• Providing calsspath while executing
Java classpath directory 1;directory2
Commenting Source Code
• Question:
o Why should you comment your code? The essential
purpose is to enhance the readability and
interpretability of programs
o How much should you comment? As you want
• 3 types of comment
➢ Single line comment: //
// This is a single line comment
➢ Multi-line comment: / **/
/* this is multi
Line comment */
➢ Documentation Comment (Doc comment): /** */
/**This class is used to represent a stack.
* @author Murali
*@version 1.0, 08/1612010
*/
How to nest the comments
Valid Nesting
/*//*/ /**//*/
Invalid Nesting
/**/ and /** */ nested with it and with each other gives error
/* /**/*/
/** /* */*/
Guidelines for Doc Comments
• Who owns and edits the Doc Comments: usually the
programmer?
• Doc comments of classes, methods, fields:
• Brief description of what does. In as longer description
required, ink to an external document (word, pdf) can be
included.
• For methods, whether they are thread-safe and what runtime
exception they throw must be specified.
• Proper indentation of documentation for better readability.
For More Information, refer
https://download.oracle.com/avase/1.4.2/docs/tooldocs/windows
/
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 of source file
Javadoc sourcefilename. Java
Javadoc College.java
Annotation
• Annotations are extra text starting with @ symbol that is
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 fools 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.
Annotation uses
• Annotations for the compiler
• Example:
o @SuppressWarnings: can be used by the compiler
to detect errors or suppress warnings.
• Annotations for the application servers and tools
o Application server tools can process annotation
information generate code etc. For services it
provides like security etc.
• javadoc tool uses annotations like
o @author, @version, @param, @return
• Runtime processing — Some annotations are available to
be examined at runtime