Java introduction.docx
Java introduction.docx
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.
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
*/
Invalid Nesting
/**/ and /** */ nested with it and with each other gives error
/* /**/*/
/** /* */*/
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