Prepared and Submitted by
Animesh Mani
Computer Science and Engineering, 3rd Year
Roll no:- 10600122065
Registration Number:- 221060120108
Index
• Instruction of Java.
• Features of Java
• Difference of java from other programming language.
• OOPs and its features
• Data types in Java
• Arrays in Java
• Taking input from user
• Class and Object in Java
• Constructor in Java
Instruction of Java.
• Java is a popular programming language, created in 1995. It is
owned by Oracle, and more than 3 billion devices run Java.
It is used for:-
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
• And much, much more!
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.
Difference of java from other
programming language
• The main difference between Java and any other
programming language is the unique method by which
Java code is executed. Unlike compiled languages such as
C++, Java is compiled into bytecode which can run on any
device with the Java Virtual Machine (JVM). C++,
• C is very much like C++(which was used to
derive Java). In fact, C++ is an updated form of C.
• C is a structure or procedure-oriented language.
• Execution time for programs written in C is very
less when compared to Java.
• C cannot handle exceptions in its program
whereas Java is very good at handling
exceptions.
• C++ is both procedural and object-oriented programming
language whereas Java is a pure object-oriented language.
• C++ libraries are simple and also they are robust. It also
provides container and associative arrays. But Java contains a
powerful cross-platform library.
• In Java, there is an automatic garbage collection whereas this
is not the case in C++. In C++ all objects are destroyed
manually with the help of the code.
• C++ supports pointers which are variables which store
addresses of other variables. But Java does not have any kind
of variable which stores addresses of other variables.
• C++ executes its programs very fast compared to Java.
• Python programs are much shorter than JAVA programs.
• Python is widely used in companies for building projects
as its programs are shorter whereas JAVA is rarely used
in companies for projects because it is difficult to use.
• Python supports dynamic typing which is very useful for
the programmers because they need to write less code
because of which their time is saved and which is user-
friendly as well as programmer-friendly. But in the case
of JAVA, developers are required to define the type of
each variable before using it which consumes the
programmer’s lots of time.
• Python is very much slower than Java.
OOPs and its features
Data abstraction
Data abstraction is one of the most essential and
important features of object-oriented programming.
Data abstraction refers to providing only essential
information about the data to the outside world, hiding
the background details or implementation.
Encapsulation
• Encapsulation is defined as the wrapping up of data under a
single unit. It is the mechanism that binds together code and
the data it manipulates. In Encapsulation, the variables or
data of a class are hidden from any other class and can be
accessed only through any member function of their class in
which they are declared. As in encapsulation, the data in a
class is hidden from other classes, so it is also known as data-
hiding.
Inheritance
• Inheritance is an important pillar of OOP(Object-Oriented
Programming). The capability of a class to derive properties
and characteristics from another class is called Inheritance.
When we write a class, we inherit properties from other
classes. So when we create a class, we do not need to write all
the properties and functions again and again, as these can be
inherited from another class that possesses it. Inheritance
allows the user to reuse the code whenever possible and
reduce its redundancy.
Polymorphism
• The word polymorphism means having many forms.
In simple words, we can define polymorphism as the
ability of a message to be displayed in more than one
form. For example, A person at the same time can
have different characteristics. Like a man at the same
time is a father, a husband, an employee. So the same
person posses different behavior in different
situations. This is called polymorphism.
Data types in Java
Primitive Data Types
• A primitive data type specifies the size and type of
variable values, and it has no additional methods.
There are eight primitive data types in Java:
Non-Primitive Data Types
• There are five types of non-primitive data types in Java,
including the built-in String and Array,
custom Classes and Interfaces, as well as the
special Object class, which is the superclass of all classes in
Java. Hence, every class in Java is a non-primitive data type
and Object is a class, which means that it, too, qualifies as a
data type.
Arrays in Java
• Java array is an object which contains elements of a similar data
type. Additionally, The elements of an array are stored in a
contiguous memory location. It is a data structure where we store
similar elements. We can store only a fixed set of elements in a Java
array.
• Advantages
Code Optimization: It makes the code optimized, we can retrieve or
sort the data efficiently.
Random access: We can get any data located at an index position.
• Disadvantages
Size Limit: We can store only the fixed size of elements in the array. It
doesn't grow its size at runtime. To solve this problem, collection
framework is used in Java which grows automatically.
Taking input from user
import java.util.*;
class UserInputDemo
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Enter first number- ");
int a= sc.nextInt();
System.out.print("Enter second number- ");
int b= sc.nextInt();
System.out.print("Enter third number- ");
int c= sc.nextInt();
int d=a+b+c;
System.out.println("Total= " +d);
}
}
Class in Java
• A class is a group of objects which have common properties. It is a
template or blueprint from which objects are created. It is a logical entity.
It can't be physical.
• A class in Java can contain:
• Fields
• Methods
• Constructors
• Blocks
• Nested class and interface
Syntax to declare a class:
class <class_name>
{
field;
method;
}
Object in Java
An entity that has state and behavior is known as an object e.g.,
chair, bike, marker, pen, table, car, etc. It can be physical or
logical (tangible and intangible).
An object is an instance of a class. A class is a template or
blueprint from which objects are created. So, an object is the
instance(result) of a class.
Object Definitions:
• An object is a real-world entity.
• An object is a runtime entity.
• The object is an entity which has state and
behavior.
• The object is an instance of a class.
Simple code for class and object
//Java Program to illustrate how to define a class and fields
//Defining a Student class.
class Student{
//defining fields
int id;//field or data member or instance variable
String name;
//creating main method inside the Student class
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference
variable
System.out.println(s1.name);
}
}
Constructor in Java
// Java Program to demonstrate
// Constructor
import java.io.*;
class Geeks {
// Constructor
Geeks()
{
super();
System.out.println("Constructor Called");
}
// main function
public static void main(String[] args)
{
Geeks geek = new Geeks();
}
}
OUTPUT
Constructor Called
•
Default Constructor in Java
A constructor that has no parameters is known as default the constructor. A default
constructor is invisible. And if we write a constructor with no arguments, the compiler does
not create a default constructor. It is taken out. It is being overloaded and called a
parameterized constructor. The default constructor changed into the parameterized
constructor. But Parameterized constructor can’t change the default constructor.
Example:
// Java Program to demonstrate
// Default Constructor
import java.io.*;
// Driver class
class GFG {
// Default Constructor
GFG() {
System.out.println("Default constructor"); }
// Driver function
public static void main(String[] args)
{
GFG hello = new GFG();
}
}
Parameterized Constructor in Java
// Java Program for Parameterized Constructor
import java.io.*;
class Geek {
// data members of the class.
String name;
int id;
Geek(String name, int id)
{
this.name = name;
this.id = id;
}
}
class GFG {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
Geek geek1 = new Geek("avinash", 68);
System.out.println("GeekName :" + geek1.name+ " and GeekId :" +
geek1.id);
}
}