0% found this document useful (0 votes)
5 views25 pages

Java 1st Lesson Notes

The document outlines the key features of Java, highlighting its simplicity, object-oriented nature, platform independence, security, robustness, and dynamic capabilities. It also explains the architecture and functioning of the Java Virtual Machine (JVM), detailing components such as class loaders, runtime data areas, and the execution engine. Additionally, it covers Java operators, classes and objects, method overloading, and constructors, emphasizing their roles in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views25 pages

Java 1st Lesson Notes

The document outlines the key features of Java, highlighting its simplicity, object-oriented nature, platform independence, security, robustness, and dynamic capabilities. It also explains the architecture and functioning of the Java Virtual Machine (JVM), detailing components such as class loaders, runtime data areas, and the execution engine. Additionally, it covers Java operators, classes and objects, method overloading, and constructors, emphasizing their roles in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Q.1. FEATURES OF JAVA.

Features of Java
The primary objective of Java programming language creati
there are also some excellent features which play an impor
buzzwords.
A list of the most important features of the Java language is

1.Simple
2.Object-Oriented
3.Portable
4.Platform independent
5.Secured
6.Robust
7.Architecture neutral
8.Interpreted
9.High Performance
10. Multithreaded
11. Distributed
12. Dynamic

Simple
Java is very easy to learn, and its syntax is simple, clean
programming language because:
o Java syntax is based on C++ (so easier for programmer

o Java has removed many complicated and rarely-used fe

o There is no need to remove unreferenced objects becau

Object-oriented
Java is an object-oriented programming language. Everyt
combination of different types of objects that incorporate bo
Object-oriented programming (OOPs) is a methodology that
Basic concepts of OOPs are:
1.Object
2.Class
3.Inheritance
4.Polymorphism
5.Abstraction
6.Encapsulation

Platform Independent
Java is platform independent because it is different from o
while Java is a write once, run anywhere language. A platfor
There are two types of platforms software-based and hardw
The Java platform differs from most other platforms in the
platforms. It has two components:
1.Runtime Environment
2.API(Application Programming Interface)
Java code can be executed on multiple platforms, for exam
and converted into bytecode. This bytecode is a platform-in
Anywhere (WORA).

Secured
Java is best known for its security. With Java, we can develo
 No explicit pointer

 Java Programs run inside a virtual machine sandb


o Classloader: Classloader in Java is a part of the Java
Machine dynamically. It adds security by separating th
network sources.
o Bytecode Verifier: It checks the code fragments for ille

o Security Manager: It determines what resources a cla

Java language provides these securities by default. Some s


Cryptography, etc.

Robust
The English mining of Robust is strong. Java is robust becau
o It uses strong memory management.

o There is a lack of pointers that avoids security problems

o Java provides automatic garbage collection which runs

application anymore.
o There are exception handling and the type checking me

Architecture-neutral
Java is architecture neutral because there are no implement
In C programming, int data type occupies 2 bytes of mem
occupies 4 bytes of memory for both 32 and 64-bit architect

Portable
Java is portable because it facilitates you to carry the Java b

High-performance
Java is faster than other traditional interpreted programmin
than a compiled language (e.g., C++). Java is an interpreted

Distributed
Java is distributed because it facilitates users to create dist
This feature of Java makes us able to access files by calling

Multi-threaded
A thread is like a separate program, executing concurrentl
threads. The main advantage of multi-threading is that it do
important for multi-media, Web applications, etc.

Dynamic
Java is a dynamic language. It supports the dynamic loading
native languages, i.e., C and C++.
Java supports dynamic compilation and automatic memory m

Q.2. JVM ARCHITECTURE AND ITS WORKING.


What is JVM (Java Virtual Machine)?
The JVM (Java Virtual Machine) is a virtual machine,
an abstract computer that has its own ISA, memory, stack, heap,
etc. It runs on the host OS and places its demands for resources
on it.
The JVM (Java Virtual Machine) is a specification and
can have different implementations, as long as they adhere to the
specs. The specs can be found in the below link
− https://docs.oracle.com
Oracle has its own JVM implementation (called the HotSpot
JVM), the IBM has its own (the J9 JVM, for example).
The operations defined inside the spec are given below (source
− Oracle JVM Specs)

JVM (Java Virtual Machine) Architecture


The architecture of the HotSpot JVM 3 is shown below −
The execution engine comprises the garbage collector and
the JIT compiler. The JVM comes in two flavors
− client and server. Both of these share the same runtime
code but differ in what JIT is used. We shall learn more about
this later. The user can control what flavor to use by specifying
the JVM flags -client or -server. The server JVM has been
designed for long-running Java applications on servers.
The JVM comes in 32b and 64b versions. The user can specify
what version to use by using -d32 or -d64 in the VM arguments.
The 32b version could only address up to 4G of memory. With
critical applications maintaining large datasets in memory, the
64b version meets that need.
Components of JVM (Java Virtual Machine)
Architecture
The following are the main components of JVM (Java Virtual
Machine) architecture:
1. Class Loader
The JVM manages the process of loading, linking and
initializing classes and interfaces in a dynamic manner. During
the loading process, the JVM finds the binary
representation of a class and creates it.
During the linking process, the loaded classes are
combined into the run-time state of the JVM
so that they can be executed during the
initialization phase. The JVM basically uses the symbol
table stored in the run-time constant pool for the linking process.
Initialization consists of actually executing the linked
classes.
The following are the types of class loaders:
 BootStrap class loader: This class loader is on the
top of the class loader hierarchy. It loads the standard JDK
classes in the JRE's lib directory.
 Extension class loader: This class loader is in the

middle of the class loader hierarchy and is the immediate


child of the bootstrap class loader and loads the classes in
the JRE's lib\ext directory.
 Application class loader: This class loader is at

the bottom of the class loader hierarchy and is the


immediate child of the application class loader. It loads the
jars and classes specified by the CLASSPATH
ENV variable.
2. Linking and Initialization
The linking process consists of the following three steps −
 Verification − This is done by the Bytecode verifier to

ensure that the generated .class files (the Bytecode) are


valid. If not, an error is thrown and the linking process
comes to a halt.
 Preparation − Memory is allocated to all static

variables of a class and they are initialized with the default


values.
 Resolution − All symbolic memory references are

replaced with the original references. To accomplish this,


the symbol table in the run-time constant memory of the
method area of the class is used.
Initialization is the final phase of the class-loading process.
Static variables are assigned original values and static blocks are
executed.
3. Runtime Data Areas
The JVM spec defines certain run-time data areas that are
needed during the execution of the program. Some of them are
created while the JVM starts up. Others are local to threads and
are created only when a thread is created (and destroyed when
the thread is destroyed). These are listed below −
PC (Program Counter) Register
It is local to each thread and contains the address of the JVM
instruction that the thread is currently executing.
Stack
It is local to each thread and stores parameters, local variables
and return addresses during method calls. A StackOverflow
error can occur if a thread demands more stack space than is
permitted. If the stack is dynamically expandable, it can still
throw OutOfMemoryError.
Heap
It is shared among all the threads and contains objects, classes'
metadata, arrays, etc., that are created during run-time. It is
created when the JVM starts and is destroyed when the JVM
shuts down. You can control the amount of heap your JVM
demands from the OS using certain flags (more on this later).
Care has to be taken not to demand too less or too much of the
memory, as it has important performance implications. Further,
the GC manages this space and continually removes dead
objects to free up the space.
Method Area
This run-time area is common to all threads and is created when
the JVM starts up. It stores per-class structures such as the
constant pool (more on this later), the code for constructors and
methods, method data, etc. The JLS does not specify if this area
needs to be garbage collected, and hence, implementations of
the JVM may choose to ignore GC. Further, this may or may not
expand as per the application's needs. The JLS does not mandate
anything with regard to this.
Run-Time Constant Pool
The JVM maintains a per-class/per-type data structure that acts
as the symbol table (one of its many roles) while linking the
loaded classes.
Native Method Stacks
When a thread invokes a native method, it enters a new world in
which the structures and security restrictions of the Java virtual
machine no longer hamper its freedom. A native method can
likely access the runtime data areas of the virtual machine (it
depends upon the native method interface), but can also do
anything else it wants.
4. Execution Engine
The execution engine is responsible for executing the bytecode,
it has three different components:
Garbage Collection
The JVM manages the entire lifecycle of objects in Java. Once
an object is created, the developer need not worry about it
anymore. In case the object becomes dead (that is, there is no
reference to it anymore), it is ejected from the heap by the GC
using one of the many algorithms ,cms.g1,etc.

Q.3. OPERATOR IN JAVA


Java Operators
Operators are used to perform operations on
variables and values.
In the example below, we use the + operator to
add together two values:
Example
int x = 100 + 50;
Although the + operator is often used to add
together two values, like in the example above, it
can also be used to add together a variable and a
value, or a variable and another variable:
Example
int sum1 = 100 + 50; // 150 (100 +
50)
int sum2 = sum1 + 250; // 400 (150 +
250)
int sum3 = sum2 + sum2; // 800 (400 +
400)
Java divides the operators into the following
groups:
 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform common
mathematical operations.

Operator Name Description Example

+ Addition Adds together two x+y


values

- Subtraction Subtracts one value from x - y


another

* Multiplication Multiplies two values x*y

/ Division Divides one value by x/y


another

% Modulus Returns the division x%y


remainder

++ Increment Increases the value of a ++x


variable by 1

-- Decrement Decreases the value of a --x


variable by 1

Java Assignment Operators


Assignment operators are used to assign values to
variables.
In the example below, we use
the assignment operator (=) to assign the
value 10 to a variable called x:
Example
int x = 10;
The addition assignment operator (+=) adds a
value to a variable:
Example
int x = 10;
x += 5;
A list of all assignment operators:
Operator Example

= x=5

+= x += 3

-= x -= 3

*= x *= 3

/= x /= 3

%= x %= 3

&= x &= 3

|= x |= 3

^= x ^= 3
>>= x >>= 3

<<= x <<= 3

Java Comparison Operators


Comparison operators are used to compare two
values (or variables). This is important in
programming, because it helps us to find answers
and make decisions.
The return value of a comparison is
either true or false. These values are known
as Boolean values, and you will learn more about
them in the Booleans and If..Else chapter.
In the following example, we use the greater
than operator (>) to find out if 5 is greater than 3:
Example
int x = 5;
int y = 3;
System.out.println(x > y); // returns true,
because 5 is higher than 3
Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or x >= y


equal to

<= Less than or equal x <= y


to

Java Logical Operators


You can also test for true or false values with
logical operators.
Logical operators are used to determine the logic
between variables or values:

Operato Name Description Example Try


r it

&& Logical Returns true if both x < 5 && x


and statements are true < 10

|| Logical Returns true if one of the x < 5 || x < 4


or statements is true

! Logical Reverse the result, !(x < 5 &&


not returns false if the result x < 10)
is true

Q.4.CLASSES AND OBJECTS.

Java Classes/Objects
Java is an object-oriented programming language.
Everything in Java is associated with classes and
objects, along with its attributes and methods. For
example: in real life, a car is an object. The car
has attributes, such as weight and color,
and methods, such as drive and brake.
A Class is like an object constructor, or a
"blueprint" for creating objects.

Create a Class
To create a class, use the keyword class:

Main.javaGet your own Java Server


Create a class named "Main" with a variable x:
public class Main {
int x = 5;
}
Remember from the Java Syntax chapter that a
class should always start with an uppercase first
letter, and that the name of the java file should
match the class name.

Create an Object
In Java, an object is created from a class. We have
already created the class named Main, so now we
can use this to create objects.
To create an object of Main, specify the class
name, followed by the object name, and use the
keyword new:
Example
Create an object called "myObj" and print the value
of x:
public class Main {
int x = 5;

public static void main(String[] args) {


Main myObj = new Main();
System.out.println(myObj.x);
}
}
Q.5. METHOD OVERLOADING IN JAVA
Method Overloading
With method overloading, multiple methods can have the same
name with different parameters:
Example
int myMethod(int x)
float myMethod(float x)
double myMethod(double x, double y)
Consider the following example, which has two methods that
add numbers of different type:
Example
static int plusMethodInt(int x, int y) {
return x + y;
}

static double plusMethodDouble(double x,


double y) {
return x + y;
}
public static void main(String[] args) {
int myNum1 = plusMethodInt(8, 5);
double myNum2 = plusMethodDouble(4.3,
6.26);
System.out.println("int: " + myNum1);
System.out.println("double: " + myNum2);
}
Try it Yourself »

Instead of defining two methods that should do the same thing, it


is better to overload one.
In the example below, we overload the plusMethod method
to work for both int and double:
Example
static int plusMethod(int x, int y) {
return x + y;
}

static double plusMethod(double x, double


y) {
return x + y;
}
public static void main(String[] args) {
int myNum1 = plusMethod(8, 5);
double myNum2 = plusMethod(4.3, 6.26);
System.out.println("int: " + myNum1);
System.out.println("double: " + myNum2);
}

Q.6.CONSTRUCTOR IN JAVA

Java Constructors
Java constructors are special types of methods that are
used to initialize an object when it is created. It has the same
name as its class and is syntactically similar to a method.
However, constructors have no explicit return type.
Typically, you will use a constructor to give initial values to the
instance variables defined by the class or to perform any other
start-up procedures required to create a fully formed object.
All classes have constructors, whether you define one or not
because Java automatically provides a default constructor that
initializes all member variables to zero. However, once you
define your constructor, the default constructor is no longer
used.

Rules for Creating Java Constructors


You must follow the below-given rules while creating Java
constructors:
 The name of the constructors must be the same as the class

name.
 Java constructors do not have a return type. Even do not

use void as a return type.


 There can be multiple constructors in the same class, this

concept is known as constructor overloading.


 The access modifiers can be used with the constructors, use

if you want to change the visibility/accessibility of


constructors.
 Java provides a default constructor that is invoked during

the time of object creation. If you create any type of


constructor, the default constructor (provided by Java) is
not invoked.
Creating a Java Constructor
To create a constructor in Java, simply write the constructor's
name (that is the same as the class name) followed by the
brackets and then write the constructor's body inside the curly
braces ({}).
Syntax
Following is the syntax of a constructor −
class ClassName {
ClassName() {
}
}
Example to create a Java Constructor
The following example creates a simple constructor that will
print "Hello world".
Open Compiler
public class Main {
// Creating a constructor
Main() {
System.out.println("Hello, World!");
}

public static void main(String[] args) {


System.out.println("The main() method.");

// Creating a class's object


// that will invoke the constructor
Main obj_x = new Main();
}
}
This program will print:
The main() method.
Hello, World!
Types of Java Constructors
There are three different types of constructors in Java, we have
listed them as follows:
 Default Constructor

 No-Args Constructor

 Parameterized Constructor

1. Default Constructor
If you do not create any constructor in the class, Java provides a
default constructor that initializes the object.

2. No-Args (No Argument) Constructor


As the name specifies, the No-argument constructor does not
accept any argument. By using the No-Args constructor you can
initialize the class data members and perform various activities
that you want on object creation.

3. Parameterized Constructor
A constructor with one or more arguments is called a
parameterized constructor.
Most often, you will need a constructor that accepts one or more
parameters. Parameters are added to a constructor in the same
way that they are added to a method, just declare them inside the
parentheses after the constructor's name.

You might also like