Q) What is Java?
Java is a programming language and a platform. Java is a high level, robust, object-
oriented and secure programming language.
Q) History of Java ?
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle)
in the year 1995. James Gosling is known as the father of Java. Before Java, its
name was Oak. Since Oak was already a registered company, so James Gosling
and his team changed the name from Oak to Java.
Q) What is Platform ?
Any hardware or software environment in which a program runs, is known as a
platform. Since Java has a runtime environment (JRE) and API, it is called a
platform.
Q) Applications of Java ?
Desktop Applications.
Web Applications.
Banking applications.
Mobile Applications.
Q) Features of Java ?
1. Simple - Java is very easy to learn, and its syntax is simple, clean and easy to
understand.
2. Object-Oriented - Java is an object-oriented programming language. Everything
in Java is an object.
3. Portable - Java is portable because it facilitates you to carry the Java bytecode to
any platform. It doesn't require any implementation.
4. 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.
5. Secured - Java is best known for its security. With Java, we can develop virus-free
systems.
6. Robust - Java is robust because:
o It uses strong memory management.
o There is a lack of pointers that avoids security problems.
7. Architecture neutral - Java is architecture neutral because there are no
implementation dependent features,for example, the size of primitive types is
fixed.
8. Interpreted
9. High Performance - Java is faster than other traditional interpreted programming
languages because Java bytecode is "close" to native code.
10. Multithreaded - 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.
11. Distributed - Java is distributed because it facilitates users to create distributed
applications in Java.
12. Dynamic - Java is a dynamic language. It supports the dynamic loading of classes.
It means classes are loaded on demand
Q) Basic concepts of OOPS ?
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
JVM,JRE & JDK :
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.
JRE : 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 : The Java Development Kit (JDK) is a software development environment
which is used to develop Java applications and applets
Compilation Process :
Data Types :
Data Type Default Value Default size
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
Operators in Java
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Types of Variables
There are three types of variables in Java:
local variable - A variable declared inside the body of the method is called local
variable.
instance variable - A variable declared inside the class but outside the body of the
method, is called an instance variable. It is not declared as static.
static variable - A variable that is declared as static is called a static variable. It cannot
be local.
Q) What is an Array?
Array is a collection of similar data type in contiguous manner.
1) Single dimensional array
2) Multi-dimensional array
Method : A method is a block of code which only runs when it is called. Methods are
used to perform certain actions, and they are also known as functions.
Method Overriding in Java
If child class has the same method as declared in the parent class, it is known
as method overriding in Java. Method overriding is used for runtime polymorphism.
A. The method must have the same name as in the parent class.
B. The method must have the same parameter as in the parent class.
class Vehicle
{
void run()
{
System.out.println("Vehicle is running");
}
}
//Creating a child class
class Bike extends Vehicle
{
public static void main(String args[])
{
//creating an instance of child class
Bike obj = new Bike();
//calling the method with child class instance
obj.run();
}
}
Output:
Vehicle is running
Method Overloading
If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.
Method overloading increases the readability of the program.
Method overloading is the example of compile time polymorphism.
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Java Package
A java package is a group of similar types of classes, interfaces and sub-packages.
Inheritance
Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object.
o For Method Overriding (so runtime polymorphism can be achieved).
o For Code Reusability.
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Exception Handling in Java
The Exception Handling in Java is one of the powerful mechanism to handle the
runtime errors so that the normal flow of the application can be maintained.
Dictionary Meaning: Exception is an abnormal condition.
In Java, an exception is an event that disrupts the normal flow of the program. It is an
object which is thrown at runtime.
Java try block
Java try block is used to enclose the code that might throw an exception. It must be
used within the method.
Java catch block
Java catch block is used to handle the Exception by declaring the type of exception
within the parameter. The declared exception must be the parent class exception ( i.e.,
Exception) or the generated exception type.
public class TryCatchExample2 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
}
//handling the exception
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}
}
Java throw keyword
The Java throw keyword is used to throw an exception explicitly.
Java throws keyword
The Java throws keyword is used to declare an exception. It gives an information to
the programmer that there may occur an exception.
Java finally block
Java finally block is a block used to execute important code such as closing the
connection, etc.
Wrapper classes in Java
The wrapper class in Java provides the mechanism to convert primitive data types into
object and object into primitive data types. Java Wrapper classes wrap the primitive
data types, that is why it is known as wrapper classes.
Boxing:- Converting primitive datatype to object is called boxing.
UnBoxing:- Whereas, converting an object into corresponding primitive datatype is
known as unboxing.
The automatic conversion of primitive data type into its corresponding wrapper class is
known as autoboxing
Input in Java :
Java Scanner class allows the user to take input from the console. It belongs
to java.util package.
Scanner sc=new Scanner(System.in);
Interface in Java
An interface in Java is a blueprint of a class. It has static constants and abstract
methods.
The interface in Java is a mechanism to achieve abstraction.
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
How to achieve complete abstraction in java ?
In java, abstraction is achieved by interfaces and abstract classes. We can achieve
100% abstraction using interfaces. Abstract classes and Abstract methods : An
abstract class is a class that is declared with an abstract keyword.
How to create objects in two ways ?
Using new keyword
Using new instance
Using clone() method
Using deserialization
Using newInstance() method of Constructor class
Acess Specifiers in java ?
Public
Private
Protected
Default
Abstract class in Java
A class which is declared with the abstract keyword is known as an abstract class
in Java. It can have abstract and non-abstract methods (method with the body).
Abstract Method in Java
A method which is declared as abstract and does not have implementation is known as
an abstract method.
Key words in java :
implments: Java implements keyword is used to implement an interface.
import: Java import keyword makes classes and interfaces available and accessible to
the current source code.
interface: Java interface keyword is used to declare an interface. It can have only
abstract methods.
new: Java new keyword is used to create new objects.
private: Java private keyword is an access modifier. It is used to indicate that a
method or variable may be accessed only in the class in which it is declared.
protected: Java protected keyword is an access modifier. It can be accessible within
the package and outside the package but through inheritance only. It can't be applied
with the class.
public: Java public keyword is an access modifier. It is used to indicate that an item is
accessible anywhere. It has the widest scope among all other modifiers.
static: Java static keyword is used to indicate that a variable or method is a class
method. The static keyword in Java is mainly used for memory management.
This keyword : This can be used to refer current class instance variable. This can be
used to invoke current class method (implicitly). This() can be used to invoke current
class constructor.
Runtime Polymorphism in Java
Runtime polymorphism is a process in which a call to an overridden method is
resolved at runtime rather than compile-time.
Compile-time Polymorphism in Java
Compile-time polymorphism is a polymorphism that is resolved during the
compilation process.
OOPS IN JAVA :
What is class and object ?
Class is a collection of objects of similar type.Once a class has been defined, we can
create any number of objects belonging to that class.
Object :- Object is a instance of class.
What is inheritance?
Inheritance is the process of acquiring the properties of the exiting class into the new
class. The existing class is called as base/parent class and the inherited class is called
as derived/child class
What is polymorphism ?
Polymorphism is one of the OOPs feature that allows us to perform a single action in
different ways.
What do you mean by abstraction ?
Abstraction means displaying only essential information and hiding the details. (Or)
Abstraction refers to hiding the internal implementation and exhibiting only the
necessary details.
Define Encapsulation ?
Encapsulation is one of the key features of object-oriented programming. It involves
the bundling of data members and functions inside a single class.
Encapsulation :- wrapping up of data &function into a single unit .
Data abstraction :- displaying the only essential information and hiding the details .
Inheritance :- process by which objects of one class acquire the properties of object of
another class.
Polymorphism : an operation that may exhibit different behaviour in different
instances