0% found this document useful (0 votes)
24 views

Unit-01 - Basics of Java - With - Notes

Uploaded by

shivam84kp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Unit-01 - Basics of Java - With - Notes

Uploaded by

shivam84kp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

1

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.

A list of most important features of Java language is given below.


1. Simple
2. Object-Oriented
3. Portable - Platform independent
4. Secured
5. Robust
6. High Performance
7. Multithreaded
8. Dynamic

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.

There are two types of platforms software-based and hardware-based. Java


provides a software-based platform.

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.

Object-oriented programming (OOPs) is a methodology that simplifies


software development and maintenance by providing some rules.

Basic concepts of OOPs are:


• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation

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.

When we write a program in Java, firstly, the compiler compiles that


program and a bytecode is generated for that piece of code. When we wish to run this
.class file on any other platform, we can do so. After the first compilation, the bytecode
generated is now run by the Java Virtual Machine and not the processor in consideration.
This essentially means that we only need to have basic java installation on any platforms
that we want to run our code on. Resources required to run the bytecode are made
available by the Java Virtual Machine, which calls the processor to allocate the required
resources. JVM's are stack-based so they stack implementation to read the codes.

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).

Components Of Java Virtual Machine(JVM):


A Java Virtual Machine(JVM) consists of the following main components :-
1. Class Loader
2. Bytecode Verifier
3. Security Manager
4. Garbage Collector
5. Execution Engine

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.

The JVM performs the following main tasks:


• Loads code
• Verifies code
• Executes code
• Provides runtime environment

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.

JDK is an implementation of any one of the below given Java Platforms


released by Oracle Corporation:
• Standard Edition Java Platform
• Enterprise Edition Java Platform
• Micro Edition Java Platform

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.

Java Primitive Data Types:


In Java language, primitive data types are the building blocks of data
manipulation. These are the most basic data types available in Java language.
There are 8 types of primitive data types:
• boolean data type
• byte data type
• char data type
• short data type
• int data type
• long data type
• float data type
• double data type

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

Byte Data Type:


The byte data type is an example of primitive data type. It is an 8-bit signed
two's complement integer. Its value-range lies between -128 to 127 (inclusive). Its minimum
value is -128 and maximum value is 127. Its default value is 0.
The byte data type is used to save memory in large arrays where the memory
savings is most required. It saves space because a byte is 4 times smaller than an integer. It
can also be used in place of "int" data type.
Example: byte a = 10, byte b = -20

Short Data Type:


The short data type is a 16-bit signed two's complement integer. Its value-
range lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768 and maximum
value is 32,767. Its default value is 0.
The short data type can also be used to save memory just like byte data type.
A short data type is 2 times smaller than an integer.
Example: short s = 10000, short r = -5000

Int Data Type:


The int data type is a 32-bit signed two's complement integer. Its value-range
lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum
value is - 2,147,483,648and maximum value is 2,147,483,647. Its default value is 0.
The int data type is generally used as a default data type for integral values
unless if there is no problem about memory.
Example: int a = 100000, int b = -200000

Long Data Type:


The long data type is a 64-bit two's complement integer. Its value-range lies
between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -
1)(inclusive). Its minimum value is - 9,223,372,036,854,775,808and maximum value is
9,223,372,036,854,775,807. Its default value is 0. The long data type is used when you need
a range of values more than those provided by int.
Example: long a = 100000L, long b = -200000L

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

Double Data Type:


The double data type is a double-precision 64-bit IEEE 754 floating point. Its
value range is unlimited. The double data type is generally used for decimal values just like
float. The double data type also should never be used for precise values, such as currency. Its
default value is 0.0d.
Example: double d1 = 12.3

Char Data Type:


The char data type is a single 16-bit Unicode character. Its value-range lies
between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive).The char data type is used to store
characters.
Example: char c1 = 'A'

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

• Java If-else Statement:


The Java if statement is used to test the condition. It
checks boolean condition: true or false. There are various types of if statement in Java.
1. if statement
2. if-else statement
3. if-else-if ladder
4. nested if statement

• Java Switch Statement:


The Java switch statement executes one statement from multiple conditions.
It is like if-else-if ladder statement. The switch statement works with byte, short, int, long,
enum types, String and some wrapper types like Byte, Short, Int, and Long. Since Java 7, you
can use strings in the switch statement.

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

• Java While Loop:


The Java while loop is used to iterate a part of the program several times. If
the number of iteration is not fixed, it is recommended to use while loop.

• Java do-while Loop:


The Java do-while loop is used to iterate a part of the program several times. If
the number of iteration is not fixed and you must have to execute the loop at least once, it is
recommended to use do-while loop.
The Java do-while loop is executed at least once because condition is checked
after loop body.

• Java Break Statement:


When a break statement is encountered inside a loop, the loop is immediately
terminated and the program control resumes at the next statement following the loop.
The Java break statement is used to break loop or switch statement. It breaks
the current flow of the program at specified condition. In case of inner loop, it breaks only
inner loop.
We can use Java break statement in all types of loops such as for loop, while
loop and do-while loop.

• Java Continue Statement:


The continue statement is used in loop control structure when you need to
jump to the next iteration of the loop immediately. It can be used with for loop or while loop.
The Java continue statement is used to continue the loop. It continues the
current flow of the program and skips the remaining code at the specified condition. In case
of an inner loop, it continues the inner loop only.
We can use Java continue statement in all types of loops such as for loop,
while loop and do-while loop.

• Java return Keyword:


Java return keyword is used to complete the execution of a method. The
return followed by the appropriate value that is returned to the caller. This value depends on
the method return type like int method always return an integer value.

19
20
21
22

You might also like