Basics Flashcards
Tell me about JDK, JRE and JVM
JDK - Java development tookit
- contains JRE + development tools(compiler, debugger)
- Platform specific
- provide all tools/executables/binaries used to compile/debug/execute a java program
JRE - Java Runtime environment
- JVM + binaries and other classes to execute java program
- no development tools:java compiler/debugger
JVM - Java virtual Machine
Different Blocks execution order in Java class initialization
- Ananymous block
{
Logger.info(“this is ananymous block”);
}
2. Static block static { Logger.info("this is static block"); } 3. constructors
Inner class
- Nested(private/member) inner class
- can access all members in outer class
- cannot have static method( is implicitly associated with an object of its outer class ) - Method local inner class(not anonymous)
- inner class inside a method
- can only access local variables in outer method which are declared as final - static inner class
- treated as a static member of outer class - Anonymous inner class(lamda)
String Builder and String Buffer
- String is unmodifiable, using concat is low performance also create a lot garbage
- String builder is fast , not thread safe
- String buffer is slow, thread safe(methods are synchronized)
String Pool contains what?
Where is it located ?
- Contains String literals and interned Strings
- Moved from PermGen to heap since Java 7
(The biggest issue of having String pool in PermGen is the small and fixed size of PermGen space. In some JVM it ranges from 32M to 96M, which is quite small for a large program.)
Read more: https://javarevisited.blogspot.com/2016/07/difference-in-string-pool-between-java6-java7.html#ixzz6AuJZzDwW
Why String is immutable?
- String are stored in Pool, 2 reference will point to same string
- HashMapkey - Always used as hashmap Key, also String cache its hashcode, so we do not need to compute every time, fast retrieve
- Security : used as passing host name, port, db connections
- Classloading mechanisim - it is used by the class loading mechanism, and thus have profound and fundamental security aspects. Had String been mutable, a request to load “java.io.Writer” could have been changed to load “mil.vogoon.DiskErasingWriter”
Read more: https://javarevisited.blogspot.com/2010/10/why-string-is-immutable-or-final-in-java.html#ixzz6AuLBIk3p
How to compare String
0) Should not use “==” to compare string
1) equals method
2) equalsIgnoreCase method
lexicographically Ordered:
2) compareTo method
4) compareToIgnoreCase method
Why character array is better than String for storing password in Java
Security risk - String is immutable and cached in string pool, so will be kept in memory for longer time
How does substring method work in Java?
- Substring shares same character array as original String
- Could load to memory leak is original string is no longer used and sub string is very small - origin string will be retained by substring from GC.
String Pool
- In Java heap
- new string literal will be in pool
- new String obj will not in pool unless you call intern()
Exceptions - What is Java Exceptions
Throwable(Class):
1) exceptions - more deal programming mistakes, non availability of requested resource
2) errors (stackoverflowError, outofmemoryError) - more deal witn system errors
What is difference between Checked and Unchecked Exception in Java ?
1) Different in how we handle them
2) checked needs to handle at compile time: try/catch/finally 'throws' IOException all drived from Exception class
3) unchecked
no need to handle at compiletime
all drived from RuntimeException: indexoutofbount/classcast
Best practices to avoid exception
- return a default/zero value instread of null
- do not leave catch block empty
- handle checked exception at proper layer rather than just escalate(i.e. SQL exception)
- Have your own customized exception defined
What is difference between throw and throws keyword in Java?
1) throws keyword is used in method signature
2) throw keyword is actually used to throw any Exception
i. e: throw new UnsupportedOperationException(“Not yet implemented”);
New features for Exception handling
1. multi catch block try { if (number.length() > 5) { throw new IllegalArgumentException(); } Integer.parseInt(number);
} catch (NumberFormatException | IllegalArgumentException e) { e.printStackTrace(); }
2. Automic resource management try ( FileInputStream stockQuoteReader = new FileInputStream("StockQuotes.txt"); FileOutputStream stockQuoteWriter = new FileOutputStream("StockQuotes.txt") ) { int var; while((var= stockQuoteReader.read())