1 - Week Java Notes 20CS43P
1 - Week Java Notes 20CS43P
Since Java SE 8 release, the Oracle corporation follows a pattern in which every even version
is release in March month and an odd version released in September month.
• No pointers but real arrays. Memory corruptions or unauthorized memory accesses are
impossible
• Automatic garbage collection tracks objects usage over time
Java is Secure
• Usage in networked environments requires more security
• Memory allocation model is a major defence.
• Access restrictions are forced (private, public)
Java is Multithreaded
• Multiple concurrent threads of executions can run simultaneously
• Utilizes a sophisticated set of synchronization primitives (based on monitors and
condition variables paradigm) to achieve this
Java is Dynamic
• Java is designed to adapt to evolving environment
• Libraries can freely add new methods and instance variables without any effect on
their clients
• Interfaces promote flexibility and reusability in code by specifying a set of methods an
object can perform, but leaves open how these methods should be implemented
• Can check the class type in runtime
JAVA ARCHITECTURE
Java Architecture in simple steps.
• In Java, there is a process of compilation and interpretation.
• The code written in Java, is converted into byte codes which is done by the Java Compiler.
• The byte codes, then are converted into machine code by the JVM.
• The Machine code is executed directly by the machine.
This diagram illustrates the internal working of a Java code, or precisely, Java Architecture!
COMPONENTS OF JAVA
There are three main components of Java language:
1. JVM (Java Virtual Machine),
2. JRE (Java Runtime Environment), and
3. JDK (Java Development Kit)
Java Virtual Machine:
Ever heard about WORA? (Write once Run Anywhere). Well, Java applications are called WORA
because of their ability to run a code on any platform. This is done only because of JVM. The JVM
is a Java platform component that provides an environment for executing Java programs. JVM
interprets the bytecode into machine code which is executed in the machine in which the Java
program runs.
Class Loader: Class loader is a subsystem of JVM. It is used to load class files. Whenever we run
the java program, class loader loads it first.
Class method area: It is one of the Data Area in JVM, in which Class data will be stored. Static
Variables, Static Blocks, Static Methods, Instance Methods are stored in this area.
Heap: A heap is created when the JVM starts up. It may increase or decrease in size while the
application runs.
Stack: JVM stack is known as a thread stack. It is a data area in the JVM memory which is created
for a single execution thread. The JVM stack of a thread is used by the thread to store various
elements i.e.; local variables, partial results, and data for calling method and returns.
Native stack: It subsumes all the native methods used in your application.
Execution Engine:
• JIT compiler
• Garbage collector
JIT compiler: The Just-In-Time (JIT) compiler is a part of the runtime environment. It helps in
improving the performance of Java applications by compiling bytecodes to machine code at run
time. The JIT compiler is enabled by default. When a method is compiled, the JVM calls the
compiled code of that method directly. The JIT compiler compiles the bytecode of that method into
machine code, compiling it “just in time” to run.
Garbage collector: As the name explains that Garbage Collector means to collect the unused
material. Well, in JVM this work is done by Garbage collection. It tracks each and every object
available in the JVM heap space and removes unwanted ones.
Garbage collector works in two simple steps known as Mark and Sweep:
• Mark – it is where the garbage collector identifies which piece of memory is in use and
which are not
• Sweep – it removes objects identified during the “mark” phase.
Tools Description
java Java interpreter, which runs applets and
applications by reading and interpreting
bytecode files
javac The java compiler, which translates Java
source code to bytecode files that the
interpreter can understand
javadoc Creates HTML-format documentation from
java source code files
JAVA APPLICATIONS
Some top applications of Java are:
• Mobile Applications
• Desktop GUI Applications
• Web-based Applications
• Enterprise Applications
• Scientific Applications
• Gaming Applications
• Cloud-based Applications
You can download the JDK 8 from this link click (CTRL + CLICK) here → jdk software
or
You can download from https://www.oracle.com/java/technologies/downloads/
For this you have to create an account on Oracle
Step 1: click on the jdk-8u271-windows-x64 exe file and click on Next button
Step 2: if you want to change the path change it or else let it be default depends on your
convenience. And click on to Next
Step 3: This gives the window for installing wait till it goes to another window.
Step 4: if you want to change the JRE path you can change it or let it be default depends on your
convenience and click on to Next
Step 5: wait until it completes
Step 6: Now you have successfully installed JDK software only when you click Finish/close button.
After installing your JDK your work is not done Still lot more things to do……..
Setting up your path and classpath for making your programs run anywhere in the drive
Open the command prompt and give javac command you get the above error as javac is not
recognized as an internal or external command. So you need to set the path.
Here how it goes…..
In this section you have 2 parts one is user variables and system variables
Because the jdk is installed in this path and you should take the full path till bin because bin
contains javac tool
Finally click on to Ok
Now open a Fresh command prompt and give javac –version and java -version
Now you can see the version displayed now you path and class path is set successfully and you
can run anywhere in your hard disk drives (c, d, e etc…)
• This Test.java is in the D: drive since you have already set the path and class path. Any
where you can compile and run..
• If you use any of the IDE’s like eclipse IDE, Netbeans IDE you no need to worry of the
path settings. This IDE’s will take care..
• But this path setting should be known by every one because if you sit working in any of
the system in any part of the world unfortunately you might not get this IDE software at
that moment. only option is to use notepad and set the path.
Documentation Section
It includes the comments that improve the readability of the program. A comment is a non-
executable statement that helps to read and understand a program especially when your programs
get more complex.
This section is optional and comments may appear anywhere in the program.
Java programming language supports three types of comments.
Single line (or end-of line) comment:
It starts with a double slash symbol (//) and terminates at the end of the current line. The compiler
ignores everything from // to the end of the line. For example:
// Calculate sum of two numbers
Multiline Comment:
Java programmer can use C/C++ comment style that begins with delimiter /* and ends with */. All
the text written between the delimiter is ignored by the compiler.
/*calculate sum of two numbers
and it is a multiline comment */
Documentation comments:
his comment style is new in Java. Such comments begin with delimiter /** and end with */.
/** The text enclosed here will be part of program documentation */
Package Statement
A package is a collection of classes, interfaces and sub-packages. A sub package contains collection
of classes, interfaces and sub-sub packages etc.
package institute;
Import statements
Java contains many predefined classes that are stored into packages. In order to refer these standard
predefined classes in your program, you need to use fully qualified name (i.e.
Packagename.className).
Example If you want to import Date class of java.util package using import statement then write.
import java.util.Date;
Interface Section
It is an optional section and is used when we wish to implement multiple inheritance feature in the
program.
Class Definition:
Java program may contain multiple class definition. Classes are primary feature of Java program.
Main Method Class Section:
The Class section describes the information about user-defined classes present in the program. A
class is a collection of fields (data variables) and methods that operate on the fields. Every program
in Java consists of at least one class, the one that contains the main method. The main () method
which is from where the execution of program actually starts and follow the statements in the order
specified.
• You can download the Eclipse IDE from the link (Ctrl+Click) click→ Eclipse Software
or
• Directly you can download from the website https://www.eclipse.org/downloads/
• Click on eclipse-inst-jre-win64 exe file to install the software
• Choose the installation folder, here I have taken D:\eclipse (created eclipse folder in d:
drive)
• Click on install and in next window Accept now
• Wait until installation
• After installation finished click on Launch.
• Select the workspace folder you can choose your own path, here I have chosen
D:\eclipse\workspace (created workspace folder inside D:\eclipse).
• Click on launch. Eclipse will starts loading…….
• After it loads click on Create a New Java Web Project
• Give the project name and click finish.
• After entering the project name, right click on project name here in this example it is java
practice select new → class → give class name → finish.
• Here class name is Hello, when you click on finish it will generete as Hello.java
• Type the program and click on Run as Application you will get the output as below.