Java Core Concepts
Java Core Concepts
What is JVM?
It is virtual machine which provides runtime environment to execute java
Byte code. The JVM doesn’t understand the Java code directly, that’s why
we need to compile the *.java files to obtain *.class files that contain
Bytecodes understandable by JVM.
What is JRE?
JRE is Java Runtime Environment which contains JVM along with Java
libraries and tools to develop the Java programs.
What is JDK?
JDK is Java Development Kit which is Superset of JRE ,which has Java
Virtual machine along with java libraries & tools. Apart from this it
contains java Compilers & debuggers.
JAVA main() Method:
The main() is the starting point for JVM to start execution of a Java program.
Without the main() method, JVM will not execute the program.
public static void main(String [] args);
“Public” access specifier is given to the main(), because JVM starts the execution
from the main() method. So, it is important that it should be visible. If it’s made
private, protected, or default, then it will not be visible to JVM.
“Static” keyword is used to create static methods. These are methods that can
be invoked without creating the objects.
Yes, Java’s main() method can be overloaded but JVM will only call the
orignal main method with str args[] , the JVM have the main() method
signature predefined , it always looks for the main() with string args[]
as the parameter. If doesn’t find it. It throws an error.
No, we cannot override main method of java because a static method cannot be
overridden.The static method in java is associated with class whereas the non-
static method is associated with an object. Static belongs to the class area, static
methods don’t need an object to be called. Static methods can be called directly
by using the classname.
The replaceAll() method can also be used to insert spaces between characters.
1. String regex = ""; // adding a white space before and after every ch
aracter of the input string.
2. str = str.replaceAll(regex, " ");
Even the null regular expression is also not accepted by the replaceAll() method
as the NullPointerException is raised:
1. String regex = null; // regular expression is null
2. str = str.replaceAll(regex, " ");
StringIndexOutOfBoundsException is thrown when any one of the
following conditions is met.
o if the start index is negative value
o end index is lower than starting index.
o Either starting or ending index is greater than the total number of
characters present in the string.
JAVA Collections:
Java collections refer to a collection\group of individual objects that are
represented as a single unit. You can perform all operations such as searching,
sorting, insertion, manipulation, deletion, etc., on Java collections just like you
do it on data.
Iterator interface : Iterator is an interface that iterates the elements. It is used
to traverse the list and modify the elements.
1. public boolean hasNext() – This method returns true if the
iterator has more elements.
2. public object next() – It returns the element and moves the cursor
pointer to the next element.
3. public void remove() – This method removes the last elements
returned by the iterator.
LinkedList: LinkedList uses doubly linked list internally to store the elements.
Manipulation of linkedList is faster in case of Linkedlist because it internally
uses doubly linked lists , so after removal of some element no shifting is
required in the memory.
HashMap:
Java HashMap class implements the Map interface which allows us to store
key and value pair, where keys should be unique. If you try to insert the
duplicate key, it will replace the element of the corresponding key. It is easy to
perform operations using the key index like updation, deletion, etc.
TreeMap:
It stores the elements in key-value pairs in sorted order. It can not contain null
key but can have multiple null values. It doesn’t contain duplicate elements.
Exceptions:
Exception is basically an event which disrupts the normal execution of the
program. So it is very important to handle the exceptions.
The java.lang.Throwable class is the root class of Java Exception hierarchy
inherited by two subclasses: Exception and Error. The hierarchy of Java
Exception classes is given below:
Checked Exception: The exceptions that occurs at the compile time
are checked Exception. ForExample: IOException, SQLException,
ClassNotFoundException etc.
Unchecked Exception: The exceptions that occur at the runtime ,not
checked at the compile time . ForExample: ArithematicException,
NullPointerException, ArrayIndexOutOfBoundsException, etc.