Java Base Flashcards
What is SOLID?
SOLID is development principle in Java S - Single Responsibility O - Open-closed L - Liskov Substitution I - Interface Segregation D - Dependency Inversion
Single Responsibility principle
One class should have only one and only responsibility
It should only have one reason to change.
Single Responsibility principle benefit
Testing – A class with one responsibility will have far fewer test cases
Lower coupling – Less functionality in a single class will have fewer dependencies
Organization – Smaller, well-organized classes are easier to search than monolithic ones
Open-closed principle
Open for Extension, Closed for Modification
Extend classes for modification
Benefit: In doing so, we stop ourselves from modifying existing code and causing potential new bugs
Liskov Substitution
If class A is a subtype of class B, then we should be able to replace B with A without disrupting the behavior of our program.
Interface Segregation
Larger interfaces should be split into smaller ones.
By doing so, we can ensure that implementing classes only need to be concerned about the methods that are of interest to them.
Dependency Inversion
The principle of Dependency Inversion refers to the decoupling of software modules.
This way, instead of high-level modules depending on low-level modules, both will depend on abstractions.
Difference between String str = “String” and String str = new String(“String”);
what is difference between String literal and String object in Java?
Both expression gives you String object, but there is subtle difference between them.
When you create String object using new() operator, it always create a new object in heap memory.
On the other hand, if you create object using String literal syntax e.g. “Java”, it may return an existing object from String pool (a cache of String object in Perm gen space, which is now moved to heap space in recent Java release), if it’s already exists. Otherwise it will create a new string object and put in string pool for future re-use. In rest of this article, why it is one of the most important thing you should remember about String in Java.
String a = “Java”;
String b = “Java”;
System.out.println(a == b);
True
String c = new String("Java"); String d = new String("Java");
System.out.println(c == d);
False
Checked and unchecked exceptions
The main difference between checked and unchecked exception is that the checked exceptions are checked at compile-time while unchecked exceptions are checked at runtime.
What are checked exceptions?
Checked exceptions are checked at compile-time.
It means if a method is throwing a checked exception then it should handle the exception using try-catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error.
What are Unchecked exceptions?
Unchecked exceptions are not checked at compile time.
It means if your program is throwing an unchecked exception and even if you didn’t handle/declare that exception, the program won’t give a compilation error.
Most of the times these exception occurs due to the bad data provided by user during the user-program interaction. It is up to the programmer to judge the conditions in advance, that can cause such exceptions and handle them appropriately. All Unchecked exceptions are direct sub classes of RuntimeException class.
Difference between exception and error.
Error mostly is thrown by JVM
Exception is thrown by Application
Error
An Error “indicates serious problems that a reasonable application should not try to catch.”