Javacollection
Javacollection
constant value. It is used with a variable. It is used to restrict the user from
updating the value of the variable.
Sub Class/Child Class: Subclass is a class which inherits the other class. It is
also called a derived class, extended class, or child class.
Super Class/Parent Class: Superclass is the class from where a subclass inherits
the features. It is also called a base class or a parent class.
Interface in Java:
- The interface in Java is a mechanism to achieve abstraction. There can be only
abstract methods in the Java interface, not method body.
ava Interface also represents the IS-A relationship.
It is used to achieve abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling.
Polymorphism :
in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means
many and "morphs" means forms. So polymorphism means many forms.
here are two types of polymorphism in Java: compile-time polymorphism and runtime
polymorphism. We can perform polymorphism in java by method overloading and method
overriding.
Runtime polymorphism :
or Dynamic Method Dispatch is a process in which a call to an overridden method is
resolved at runtime rather than compile-time.
Super Keyword in Java :
The super keyword in Java is a reference variable which is used to refer immediate
parent class object.
Usage of Java super Keyword
super can be used to refer immediate parent class instance variable.
super can be used to invoke immediate parent class method.
super() can be used to invoke immediate parent class constructor.
variable
method
class
Encapsulation :
in Java is a process of wrapping code and data together into a single unit, for
example, a capsule which is mixed of several medicines.
encapsulation in java
We can create a fully encapsulated class in Java by making all the data members of
the class private. Now we can use setter and getter methods to set and get the data
in it.
Advantage of Encapsulation in Java :
By providing only a setter or getter method, you can make the class read-only or
write-only. In other words, you can skip the getter or setter methods.
package :
A java package is a group of similar types of classes, interfaces and sub-packages.
Method Overloading:
If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.
If we have to perform only one operation, having same name of the methods increases
the readability of the program.
Advantage of method overloading
Method overloading increases the readability of the program.
method overriding:
If subclass (child class) has the same method as declared in the parent class, it
is known as method overriding in Java
Method overriding is used to provide the specific implementation of a method which
is already provided by its superclass.
Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
The method must have the same name as in the parent class
The method must have the same parameter as in the parent class.
There must be an IS-A relationship (inheritance).
Since J2SE 5.0, autoboxing and unboxing feature convert primitives into objects and
objects into primitives automatically. The automatic conversion of primitive into
an object is known as autoboxing and vice-versa unboxing.
Use of Wrapper classes in Java
Java is an object-oriented programming language, so we need to deal with objects
many times like in Collections, Serialization, Synchronization, etc. Let us see the
different scenarios, where we need to use the wrapper classes.
recursion:
in java is a process in which a method calls itself continuously. A method in java
that calls itself is called recursive method.
It makes the code compact but complex to understand.
Method
a method is a way to perform some task. Similarly, the method in Java is a
collection of instructions that performs a specific task. It provides the
reusability of code. We can also easily modify code using methods. In this section,
we will learn what is a method in Java, types of methods, method declaration, and
how to call a method in Java.
Every time an object is created using the new() keyword, at least one constructor
is called.
There are two types of constructors in Java: no-arg constructor, and parameterized
constructor
Default constructor (no-arg constructor)
Parameterized constructor
Default Constructor :
A constructor is called "Default Constructor" when it doesn't have any parameter.
Parameterized Constructor :
A constructor which has a specific number of parameters is called a parameterized
constructor
There is only call by value in java, not call by reference. If we call a method
passing a value, it is known as call by value. The changes being done in the called
method, is not affected in the calling method.
String :
is a sequence of characters. But in Java, string is an object that represents a
sequence of characters. The java.lang.String class is used to create a string
object.
By string literal
By new keyword
1) String Literal
Java String literal is created by using double quotes. For Example:
String s="welcome";
By new keyword
String s=new String("Welcome");//creates two objects and one reference variable
immutable string :
A String is an unavoidable type of variable while writing any application program.
String references are used to store various attributes like username, password,
etc. In Java, String objects are immutable. Immutable simply means unmodifiable or
unchangeable.
Once String object is created its data or state can't be changed but a new String
object is created.
substring :
A part of String is called substring. In other words, substring is a subset of
another String. Java String class provides the built-in substring() method that
extract a substring from the given string by using the index values passed as an
argument. In case of substring() method startIndex is inclusive and endIndex is
exclusive.
StringBuffer:
StringBuffer class is used to create mutable (modifiable) String objects. The
StringBuffer class in Java is the same as String class except it is mutable i.e. it
can be changed.
StringBuilder:
class is used to create mutable (modifiable) String. The Java StringBuilder class
is same as StringBuffer class except that it is non-synchronized. It is available
since JDK 1.5.
StringBuffer
StringBuilder
1) StringBuffer is synchronized i.e. thread safe. It means two threads can't
call the methods of StringBuffer simultaneously. StringBuilder is non-
synchronized i.e. not thread
safe. It means two threads can call the methods of StringBuilder simultaneously.
2) StringBuffer is less efficient than StringBuilder.
StringBuilder is more efficient than StringBuffer.
3) StringBuffer was introduced in Java 1.0
StringBuilder was introduced in Java 1.5
Multithreading:
in Java is a process of executing multiple threads simultaneously.
Multitasking:
Exception Handling:
Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException, RemoteException, etc.
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. However, the good approach is to
declare the generated type of exception.
The catch block must be used after the try block only. You can use multiple catch
block with a single try block.
Java Arrays
Normally, an array is a collection of similar type of elements which has contiguous
memory location.
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.