Unit-01 - Basics of Java - With - Notes
Unit-01 - Basics of Java - With - Notes
2
3
Hello World Example:
Let's create the hello java program and save this file as Simple.java
To compile: javac Simple.java
To execute: java Simple
Parameters used in First Java Program:
Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().
• class keyword is used to declare a class in java.
• public keyword is an access modifier which represents visibility. It means it is visible to
all.
• static is a keyword. If we declare any method as static, it is known as the static method.
The core advantage of the static method is that there is no need to create an object to
invoke the static method. The main method is executed by the JVM, so it doesn't require
to create an object to invoke the main method. So it saves memory.
• void is the return type of the method. It means it doesn't return any value.
• main represents the starting point of the program.
• String[] args is used for command line argument.
• System.out.println() is used to print statement. Here, System is a class, out is the object
of PrintStream class, println() is the method of PrintStream class.
4
Features of Java:
The primary objective of Java programming language creation was to make it
portable, simple and secure programming language. Apart from this, there are also some
excellent features which play an important role in the popularity of this language. The
features of Java are also known as java buzzwords.
5
Simple
Java is very easy to learn, and its syntax is simple, clean and easy to
understand. According to Sun, Java language is a simple programming language because:
• Java syntax is based on C++ (so easier for programmers to learn it after
C++).
• Java has removed many complicated and rarely-used features, for
example, explicit pointers, operator overloading, etc.
• There is no need to remove unreferenced objects because there is an
Automatic Garbage Collection in Java.
6
Secured:
Java is best known for its security. With Java, we can develop virus-free
systems. Java is secured because:
• No explicit pointer
• Java Programs run inside a virtual machine sandbox
• Class Loader: Class Loader in Java is a part of the Java Runtime
Environment(JRE) which is used to load Java classes into the Java Virtual
Machine dynamically. It adds security by separating the package for the
classes of the local file system from those that are imported from network
sources.
• Bytecode Verifier: It checks the code fragments for illegal code that can
violate access right to objects.
• Security Manager: It determines what resources a class can access such
as reading and writing to the local disk.
Java language provides these securities by default. Some security can also be
provided by an application developer explicitly through SSL, JAAS,
Cryptography, etc.
7
Portable - Platform Independent:
Java is platform independent because it is different from other languages like
C, C++, etc. which are compiled into platform specific machines while Java is a write once,
run anywhere language. A platform is the hardware or software environment in which a
program runs.
The Java platform differs from most other platforms in the sense that it is a
software-based platform that runs on the top of other hardware-based platforms. It has
two components:
1. Runtime Environment
2. API(Application Programming Interface)
Java code can be run on multiple platforms, for example, Windows, Linux,
Sun Solaris, Mac/OS, etc. Java code is compiled by the compiler and converted into
bytecode. This bytecode is a platform-independent code because it can be run on multiple
platforms, i.e., Write Once and Run Anywhere(WORA).
8
High-performance:
Java is faster than other traditional interpreted programming languages
because Java bytecode is "close" to native code. It is still a little bit slower than a compiled
language (e.g., C++). Java is an interpreted language that is why it is slower than compiled
languages, e.g., C, C++, etc.
9
Object-oriented:
Java is an object-oriented programming language. Everything in Java is an
object. Object-oriented means we organize our software as a combination of different
types of objects that incorporates both data and behavior.
10
Robust:
Robust simply means strong. Java is robust because:
• It uses strong memory management.
• There is a lack of pointers that avoids security problems.
• There is automatic garbage collection in java which runs on the Java
Virtual Machine to get rid of objects which are not being used by a Java
application anymore.
• There are exception handling and the type checking mechanism in Java.
All these points make Java robust.
11
Multi-threaded:
A thread is like a separate program, executing concurrently. We can write
Java programs that deal with many tasks at once by defining multiple threads. The main
advantage of multi-threading is that it doesn't occupy memory for each thread. It shares a
common memory area. Threads are important for multi-media, Web applications, etc.
12
Dynamic:
Java is a dynamic language. It supports dynamic loading of classes. It means
classes are loaded on demand. It also supports functions from its native languages, i.e., C
and C++.
Java supports dynamic compilation and automatic memory management
(garbage collection).
13
Java Bytecode:
Java bytecode is the instruction set for the Java Virtual Machine. It acts
similar to an assembler which is an alias representation of a C++ code. As soon as a java
program is compiled, java bytecode is generated. In more appropriate terms, java bytecode
is the machine code in the form of a .class file. With the help of java bytecode we achieve
platform independence in java.
14
Advantage of Java Bytecode:
Platform independence is one of the soul reasons for which James Gosling
started the formation of java and it is this implementation of bytecode which helps us to
achieve this. Hence bytecode is a very important component of any java program. The set of
instructions for the JVM may differ from system to system but all can interpret the bytecode.
A point to keep in mind is that bytecode are non-runnable codes and rely on the availability
of an interpreter to execute and thus the JVM comes into play.
Bytecode is essentially the machine level language which runs on the Java
Virtual Machine. Whenever a class is loaded, it gets a stream of bytecode per method of the
class. Whenever that method is called during the execution of a program, the bytecode for
that method gets invoked. Javac not only compiles the program but also generates the
bytecode for the program. Thus, we have realized that the bytecode implementation makes
Java a platform-independent language. This helps to add portability to Java which is lacking
in languages like C or C++. Portability ensures that Java can be implemented on a wide array
of platforms like desktops, mobile devices, severs and many more. Supporting this, Sun
Microsystems captioned JAVA as "write once, read anywhere" or "WORA" in resonance to the
bytecode interpretation.
14
Java Virtual Machine(JVM):
A Java Virtual Machine(JVM) is an abstract computer which interpret the
java class file(bytecode), convert it into native machine code(JIT compilation) and finally
execute the java program.
A runtime instance of the Java Virtual Machine(JVM) has a clear mission in
life : to run one Java application.
When a Java application starts, a runtime instance is born. When the
application completes, the instance dies. If you start three Java applications at the same
time, on the same computer, using the same concrete implementation, you will get three
Java Virtual Machine instances. Each Java application runs inside its own Java Virtual
Machine(name space).
15
Class Loader :
A class loader has following responsibilities –
• Loads all classes necessary for the execution of a program
• Maintains classes of the local file system in separate "namespaces“
• Protect java classes from spoofing attack.
• Guard system packages from bogus classes.
• Resolve symbolic reference from one class to another.
Bytecode Verifier :
It ensures that –
• The code adheres to the JVM specification
• It enforces that Java bytecode is type safe
• class file format is OK,
• The code does not violate system integrity
• The code causes no operand stack overflows or underflows
• The object fields accesses (public/private/protected) are legal.
• The parameter types for all operational code are correct
• No illegal data conversions (the conversion of integers to pointers) have
occurred
Security Manager :
• It guards security policies for Java applications
• It is always consulted before any potentially dangerous operation is
requested by Java application
• It implements appropriate check methods that implement a given security
policy
Garbage Collector :
• It determine the objects which are no longer referenced by the program
and make available the space occupied by such unreferenced objects to the
subsequent new objects.
• Helps in heap fragmentation.
• It relieves you from the burden of freeing allocated memory thus make you
more productive
• it helps to ensure program integrity
Execution Engine :
• Convert the bytecode into native machine code(JIT Compilation)
• Responsible for executing the instructions contained in the methods of
loaded classes.
15
Difference between JDK, JRE, and JVM:
JVM:
JVM (Java Virtual Machine) is an abstract machine. It is called a virtual
machine because it doesn't physically exist. It is a specification that provides a runtime
environment in which Java bytecode can be executed. It can also run those programs which
are written in other languages and compiled to Java bytecode.
JVMs are available for many hardware and software platforms. JVM, JRE, and
JDK are platform dependent because the configuration of each OS is different from each
other. However, Java is platform independent. There are three notions of the JVM:
specification, implementation, and instance.
16
JRE:
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE.
The Java Runtime Environment is a set of software tools which are used for developing Java
applications. It is used to provide the runtime environment. It is the implementation of JVM.
It physically exists. It contains a set of libraries + other files that JVM uses at runtime.
JDK:
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is
a software development environment which is used to develop Java applications and applets.
It physically exists. It contains JRE + development tools.
The JDK contains a private Java Virtual Machine (JVM) and a few other
resources such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a
documentation generator (Javadoc), etc. to complete the development of a Java Application.
16
Data Types in Java:
Data types specify the different sizes and values that can be stored in the
variable. There are two types of data types in Java:
1. Primitive data types: The primitive data types include boolean, char,
byte, short, int, long, float and double.
2. Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays.
17
Boolean Data Type:
The Boolean data type is used to store only two possible values: true and
false. This data type is used for simple flags that track true/false conditions.
The Boolean data type specifies one bit of information, but its "size" can't be
defined precisely.
Example: Boolean one = false
17
Float Data Type:
The float data type is a single-precision 32-bit IEEE 754 floating point. Its value
range is unlimited. It is recommended to use a float (instead of double) if you need to save
memory in large arrays of floating point numbers. The float data type should never be used
for precise values, such as currency. Its default value is 0.0F.
Example: float f1 = 234.5f
17
Operators in Java:
Operator in Java is a symbol which is used to perform operations. For
example: +, -, *, / etc.
There are many types of operators in Java which are given below:
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.
18
Control Statements
19
• Loops in Java:
In programming languages, loops are used to execute a set of
instructions/functions repeatedly when some conditions become true. There are three types
of loops in Java.
1. for loop
2. while loop
3. do-while loop
19
20
21
22