JAVA
JAVA
Java is known for its simplicity, robustness, and security features, making it a popular
choice for enterprise-level applications.
1. Platform Independent
Compiler converts source code to byte code and then the JVM executes the bytecode
generated by the compiler. This byte code can run on any platform be it Windows, Linux,
or macOS which means if we compile a program on Windows, then we can run it
on Linux and vice versa. Each operating system has a different JVM, but the output
produced by all the OS is the same after the execution of the byte code. That is why we
call java a platform-independent language.
2. Object-Oriented Programming
Abstraction
Data Abstraction may also be defined as the process of identifying only the required
characteristics of an object ignoring the irrelevant details. The properties and behaviours
of an object differentiate it from other objects of similar type and also help in
classifying/grouping the objects.
5. Any class that contains one or more abstract methods must also be declared with
an abstract keyword.
6. There can be no object of an abstract class. That is, an abstract class can not be
directly instantiated with the new operator.
Interface
Interfaces are another method of implementing abstraction in Java. The key difference
is that, by using interfaces, we can achieve 100% abstraction in Java classes. In Java or
any other language, interfaces include both methods and variables but lack a method
body. Apart from abstraction, interfaces can also be used to implement interfaces in
Java.
Advantages of Abstraction
• Abstraction can make it more difficult to understand how the system works.
Encapsulation
• As in encapsulation, the data in a class is hidden from other classes using the
data hiding concept which is achieved by making the members or methods of a
class private, and the class is exposed to the end-user or the world without
providing any details behind implementation using the abstraction concept, so it
is also known as a combination of data-hiding and abstraction.
Advantages of Encapsulation
• Data Hiding: it is a way of restricting the access of our data members by hiding
the implementation details. Encapsulation also provides a way for data hiding.
The user will have no idea about the inner implementation of the class. It will not
be visible to the user how the class is storing values in the variables. The user will
only know that we are passing the values to a setter method and variables are
getting initialized with that value.
• Increased Flexibility: We can make the variables of the class read-only or write-
only depending on our requirements. If we wish to make the variables read-only
then we have to omit the setter methods like setName(), setAge(), etc. from the
above program or if we wish to make the variables write-only then we have to
omit the get methods like getName(), getAge(), etc. from the above program
• Reusability: Encapsulation also improves the re-usability and is easy to change
with new requirements.
• Testing code is easy: Encapsulated code is easy to test for unit testing.
class Name {
// getter
// setter
// Driver Class
class GFG {
// main function
+ n1.getAge());
Inheritance
• Abstraction: The concept of abstract where we do not have to provide all details,
is achieved through inheritance. Abstraction only shows the functionality to the
user.
• Super Class/Parent Class: The class whose features are inherited is known as a
superclass(or a base class or a parent class).
• Sub Class/Child Class: The class that inherits the other class is known as a
subclass(or a derived class, extended class, or child class). The subclass can
add its own fields and methods in addition to the superclass fields and methods.
Polymorphism
• Compile-time Polymorphism
• Runtime Polymorphism
Method Overloading
When there are multiple functions with the same name but different parameters
then these functions are said to be overloaded. Functions can be overloaded by
changes in the number of arguments or/and a change in the type of arguments.
1. Function Overloading
It is a feature in C++ where multiple functions can have the same name but with
different parameter lists. The compiler will decide which function to call based
on the number and types of arguments passed to the function.
2. Operator Overloading
3. Template
i. Virtual functions
Diagram –
3. Simplicity
Java’s syntax is simple and easy to learn, especially for those familiar with C or C++. It
eliminates complex features like pointers and multiple inheritances, making it easier
to write, debug, and maintain code.
4. Robustness
Java language is robust which means reliable. It is developed in such a way that it puts
a lot of effort into checking errors as early as possible, that is why the java compiler is
able to detect even those errors that are not easy to detect by another programming
language. The main features of java that make it robust are garbage collection, exception
handling, and memory allocation.
5. Security
6. Distributed
7. Multithreading
8. Portability
As we know, java code written on one machine can be run on another machine. The
platform-independent feature of java in which its platform-independent bytecode can be
taken to any platform for execution makes java portable. WORA(Write Once Run
Anywhere) makes java application to generates a ‘.class’ file that corresponds to our
applications(program) but contains code in binary format. It provides ease t architecture-
neutral ease as bytecode is not dependent on any machine architecture. It is the primary
reason java is used in the enterprising IT industry globally worldwide.
9. High Performance
Java architecture is defined in such a way that it reduces overhead during the runtime
and at some times java uses Just In Time (JIT) compiler where the compiler compiles
code on-demand basis where it only compiles those methods that are called making
applications to execute faster.
If you want to master Java, check out the Java Programming – Basic to Advanced course
by us. It’s perfect for taking your Java skills from beginner to expert.
The Java compiler (javac) converts the source code into bytecode, which is stored in
a .class file. This bytecode is platform-independent and can be executed on any
machine with a JVM.
The JVM executes the compiled bytecode, translating it into machine code specific to
the operating system and hardware.
Example Program:
Java
System.out.println("Hello, World!");
Before learning Java, one must be familiar with these common terms of Java.
The JVM is an integral part of the Java platform, responsible for executing Java bytecode.
It ensures that the output of Java programs is consistent across different platforms.
• The compilation is done by the JAVAC compiler which is a primary Java compiler
included in the Java development kit (JDK). It takes the Java program as input and
generates bytecode as output.
• In the Running phase of a program, JVM executes the bytecode generated by the
compiler.
Now, we understood that the function of Java Virtual Machine is to execute the bytecode
produced by the compiler. Every Operating System has a different JVM but the output
they produce after the execution of bytecode is the same across all the operating
systems. This is why Java is known as a platform-independent language.
2. Bytecode
While we were using the term JDK when we learn about bytecode and JVM. So, as the
name suggests, it is a complete Java development kit that includes everything
including compiler, Java Runtime Environment (JRE), java debuggers, java docs, etc.
For the program to execute in java, we need to install JDK on our computer in order to
create, compile and run the java program.
JDK includes JRE. JRE installation on our computers allows the java program to run,
however, we cannot compile it. JRE includes a browser, JVM, applet support, and plugins.
For running the java program, a computer needs JRE.
5. Garbage Collector
In Java, programmers can’t delete the objects. To delete or recollect that memory JVM
has a program called Garbage Collector. Garbage Collectors can recollect the objects
that are not referenced. So Java makes the life of a programmer easy by handling
memory management. However, programmers should be careful about their code
whether they are using objects that have been used for a long time. Because Garbage
cannot recover the memory of objects being referenced.
6. ClassPath
The Classpath is the file path where the java runtime and Java compiler look
for .class files to load. By default, JDK provides many libraries. If you want to include
external libraries they should be added to the classpath.
Advantages of Java
• Platform independent: Java code can run on any platform that has a Java Virtual
Machine (JVM) installed, which means that applications can be written once and
run on any device.
• Object-Oriented: Java is an object-oriented programming language, which
means that it follows the principles of encapsulation, inheritance, and
polymorphism.
• Security: Java has built-in security features that make it a secure platform for
developing applications, such as automatic memory management and type
checking.
• Large community: Java has a large and active community of developers, which
means that there is a lot of support available for learning and using the language.
Disadvantages of Java
Arrays are fundamental structures in Java that allow us to store multiple values of
the same type in a single variable. They are useful for managing collections of data
efficiently.
type[] arrayName;
Here, type specifies the type of data being allocated, size determines the number of
elements in the array, and var-name is the name of the array variable that is linked
to the array. To use new to allocate an array, you must specify the type and number
of elements to allocate.
Example:
//declaring array
int intArray[];
// allocating memory to array
intArray = new int[20];
// combining both statements in one
int[] intArray = new int[20];
1. Single-Dimensional Arrays
These are the most common type of arrays, where elements are stored in a linear order.
2. Multi-Dimensional Arrays
Arrays with more than one dimension, such as two-dimensional arrays (matrices).
int[][] multiDimArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}; // A 2D array (matrix)
You can also access java arrays using for each loops.
Arrays of Objects in Java
An array of objects is created like an array of primitive-type data items in the following
way.
// an array of objects
class Student {
this.roll_no = roll_no;
this.name = name;
Student[] arr;
// so on...
+ arr[i].name);
// an array of objects
class Student
this.name = name;
@Override
return name;
for(Student m:myStudents){
System.out.println(m);
arr[0] = 10;
arr[1] = 20;
System.out.println(arr[i]);
Declaration:
import java.io.*;
// Driver class
class GFG {
// Syntax
// Number of Rows
System.out.println("Number of Rows:"+
arr.length);
// Number of Columns
System.out.println("Number of Columns:"+
arr[0].length);
// Driver Class
// main function
int arr[][]
= { { 2, 7, 9 }, { 3, 6, 1 }, { 7, 4, 2 } };
// printing 2D array
System.out.println();
}
}
Output
279
361
742
Like variables, we can also pass arrays to methods. For example, the below program
passes the array to method sum to calculate the sum of the array’s values.
// Driver method
int arr[] = { 3, 1, 2, 5, 4 };
sum(arr);
int sum = 0;
for (int i = 0; i < arr.length; i++)
sum += arr[i];
Output
• Efficient Access: Accessing an element by its index is fast and has constant time
complexity, O(1).
• Fixed Size: Once an array is created, its size cannot be changed, which can lead
to memory waste if the size is overestimated or insufficient storage if
underestimated.
• Type Homogeneity: Arrays can only store elements of the same data type, which
may require additional handling for mixed types of data.
System.out.println(arr[i]);
int[] a = {1,2,3,4,5,6,7,8,9,10};
System.out.println(a[i]);
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.
VMs 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.
o Loads code
o Verifies code
o Executes code
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:
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.