Assignment No.1 Core Java (1)
Assignment No.1 Core Java (1)
1 Core Java
Aim :- Introduction to Java Enviornment, javac, jdb
and javadoc.
What is the Java Programming Environment?
Java is a recently developed, concurrent, class-based, object-oriented programming
and runtime environment, consisting of:
• A programming language
• An API specification
• A virtual machine specification
• Object oriented - Java provides the basic object technology of C++ with some
enhancements and some deletions.
• Architecture neutral - Java source code is compiled into architecture-
independent object code. The object code is interpreted by a Java Virtual
Machine (JVM) on the target architecture.
• Portable - Java implements additional portability standards. For example, ints
are always 32-bit, 2's-complemented integers. User interfaces are built through
an abstract window system that is readily implemented in Solaris and other
operating environments.
• Distributed - Java contains extensive TCP/IP networking facilities. Library
routines support protocols such as HyperText Transfer Protocol (HTTP) and
file transfer protocol (FTP).
• Robust - Both the Java compiler and the Java interpreter provide extensive
error checking. Java manages all dynamic memory, checks array bounds, and
other exceptions.
• Secure - Features of C and C++ that often result in illegal memory accesses are
not in the Java language. The interpreter also applies several tests to the
compiled code to check for illegal code. After these tests, the compiled code
causes no operand stack over- or underflows, performs no illegal data
conversions, performs only legal object field accesses, and all opcode
parameter types are verified as legal.
• High performance - Compilation of programs to an architecture independent
machine-like language, results in a small efficient interpreter of Java programs.
The Java environment also compiles the Java bytecode into native machine
code at runtime.
• Multithreaded - Multithreading is built into the Java language. It can improve
interactive performance by allowing operations, such as loading an image, to be
performed while continuing to process user actions.
• Dynamic - Java does not link invoked modules until runtime.
• Simple - Java is similar to C++, but with most of the more complex features of
C and C++ removed.
Introducing javac
javac - read Java class and interface definitions and compile them into bytecode and
class files
There are two ways to pass source code file names to javac:
• For a few source files, you can list the file names on the command line.
• For a few source files, you can use the @filename option on the javac command
line to include a file that lists the source file names.
Source code file names must have .java suffixes, class file names must have .class suffixes,
and both source and class files must have root names that identify the class. For
example, a class called MyClass would be written in a source file called MyClass.java:
class MyClass {
public static void main(String[] args) {
System.out.println("this is my class");
}
}
Copy
and compiled into a bytecode class file called MyClass.class:
javac MyClass.java
Copy
MyClass.java
MyClass.class
Copy
Inner class definitions produce additional class files. These class files have names that
combine the inner and outer class names, such as MyClass$MyInnerClass.class.
You should arrange the source files in a directory tree that reflects their package tree.
For example:
• Linux and macOS: If all of your source files are in /workspace, then put the source
code for com.mysoft.mypack.MyClass in /workspace/com/mysoft/mypack/MyClass.java.
• Windows: If all of your source files are in \workspace, then put the source code
for com.mysoft.mypack.MyClass in \workspace\com\mysoft\mypack\MyClass.java.
By default, the compiler puts each class file in the same directory as its source file. You
can specify a separate destination directory with the -d option described in Standard
Options.
What Is jdb ?
You use the jdb command and its options to find and fix bugs in Java platform
programs.
Synopsis
Copy
jdb [options] [classname] [arguments]
options
This represents the jdb command-line options. See Options for the jdb
command.
classname
Description
The Java Debugger (JDB) is a simple command-line debugger for Java classes.
The jdb command and its options call the JDB. The jdb command demonstrates
the Java Platform Debugger Architecture and provides inspection and
debugging of a local or remote JVM.
The MyClass argument isn’t specified in the jdb command line in this case
because the jdb command is connecting to an existing JVM instead of
launching a new JVM.
There are many other ways to connect the debugger to a JVM, and all of them
are supported by the jdb command. The Java Platform Debugger Architecture
has additional documentation on these connection options.
Breakpoints
Breakpoints can be set in the JDB at line numbers or at the first instruction of a
method, for example:
• The command stop at MyClass:22 sets a breakpoint at the first instruction for
line 22 of the source file containing MyClass.
• The command stop in java.lang.String.length sets a breakpoint at the beginning
of the method java.lang.String.length.
• The command stop in MyClass.<clinit> uses <clinit> to identify the static
initialization code for MyClass.
When a method is overloaded, you must also specify its argument types so
that the proper method can be selected for a breakpoint. For
example, MyClass.myMethod(int,java.lang.String) or MyClass.myMethod().
The clear command removes breakpoints using the following syntax: clear
MyClass:45. Using the clear or stop command with no argument displays a list of all
breakpoints currently set. The cont command continues execution.
Stepping
The step command advances execution to the next line whether it’s in the
current stack frame or a called method. The next command advances execution
to the next line in the current stack frame.
Uses the specified path to search for source files in the specified path. If
this option is not specified, then use the default path of dot (.).
-attach address
Connects to the target JVM with the named connector and listed
argument values.
-dbgtrace [flags]
Prints information for debugging the jdb command.
-tclient
-v -verbose[:class|gc|jni]
/*
* Multiple-Line comment
*/
/**
* JavaDoc comment
*/
By writing a number of comments, it does not affect the performance of the
Java program as all the comments are removed at compile time.
JavaDoc Format: –
It has two parts: – a description which is followed by block tags.
Some Integrated Development Environments (IDE) automatically generate
the JavaDoc file like NetBeans, IntelliJ IDEA, Eclipse, etc.
Generation of JavaDoc: –
To create a JavaDoc you do not need to compile the java file. To create the
Java documentation API, you need to write Javadoc followed by file name.
javadoc file_name or javadoc package_name
After successful execution of the above command, a number of HTML files
will be created, open the file named index to see all the information about
classes.
JavaDoc Tags