Exception handling, Lambda expressions, GC, Selected Classes Flashcards
System.exit(0)
Whenever we are using System.exit(0) then JVM will be terminated.
LocalDate
LocalTime
LocalDateTime
LocalDate,LocalTime and LocalDateTime are immutable ojects. Hence if we are trying to modify the content, then a new object will be created and in the existing object there are no changes.
Predicate
you only need to know how to implement lambda expressions that
use the Predicate interface. Remember the one method in the interface called test()? It
takes any one reference type parameter and returns a boolean.
Lambda syntax
a -> a.canHop()
■ Specify a single parameter with the name a
■ The arrow operator to separate the parameter and body
■ A body that calls a single method and returns the result of that method
(Animal a) -> { return a.canHop(); }
■ Specify a single parameter with the name a and stating the type is Animal
■ The arrow operator to separate the parameter and body
■ A body that has one or more lines of code, including a semicolon and a return statement
If catch block and finally block throws exceptions
If catch block and finally block throws exceptions, then only finally block thrown exception(most recently raised exception) will be displayed to the console.
ArrayList methods
set()
The set() method changes one of the elements of the ArrayList without changing the size. The method signature is as follows:
E set(int index, E newElement)
== Vs. .equals()
Calling == on String objects will check whether they point to the same object in the pool.
Calling == on StringBuilder references will check whether they are pointing to the same StringBuilder object.
Calling equals() on String objects will check whether the sequence of characters is the same.
Calling equals() on StringBuilder objects will check whether they are pointing to the same object rather than looking at the values inside.
String s = new String(“Bahubali”);
StringBuilder sb = new
StringBuilder(“Bahubali”);
System.out.println(s==sb);
To use == operator between object references, compulsory there should be some relation between argument types (either parent to child or child to parent or same type), otherwise we will get compile time error.
In the above example there is no relation between String and StringBuilder. Hence we will get compile time error saying: incomparable types: String and StringBuilder
public class Test
{
public static void m1() throws Exception
{
System.out.print(“A”);
throw new TestException();
}
public static void main(String[] args)
{
try
{
m1();
}
catch (TestException e)
{
System.out.println(“B”);
}
finally
{
System.out.println(“C”);
}
} }
m1() throws Exception and hence compulsory inside main method we should handle exception, but we are handling TestException. Hence we will get compile time error saying: unreported exception Exception; must be caught or declared to be thrown
ArrayList (3 rules)
- ArrayList is mutable object.
- ArrayList not having fixed size.Based on our requirement, we can increase or decrease the size.
- ArrayList can hold both homogeneous and heterogeneous elements.
If try,catch and finally blocks contains separate return statements
then finally block return statement will get more priority.
ArrayList method
remove()
The remove() methods remove the first matching value in the ArrayList or remove the element at a specified index. The method signatures are as follows:
boolean remove(Object object)
E remove(int index)
java.lang.Object
|
java.lang.Throwable
|
—————————————
| |
java.lang.Exception |
| |
| java.lang.Error
|
java.lang.RuntimeException
exception hierarchy