Saturday, July 11, 2015

Interview - Java Core Miscs



10 points about Static in Java
http://java67.blogspot.com/2012/11/what-is-static-class-variable-method.html
1) You can not access non static member inside static context e.g. static method or static block.
2) Unlike local variables,  Static variables and methods are not thread-safe in Java
4) Another important point about static method is that, you can not override static method in Java. If you declare same method in sub class i.e. static method with same name and method signature
it will only hide super class method, instead of overriding it. This is also known as method hiding in Java. What this means is, if you call a static method, which is declared in both super class and sub class, method is always resolved at compile time by using Type of reference variable. Unlike case of method overriding they will not resolved during runtime.

5) You can also make a class static in Java, except top level classes.
6) static keyword can also be used to declare static block which is executed during class loading. 
This is known as static initializer block in Java. If you don't declare a static initializer block by yourself then Java combines all static fields into one block and execute them during class loading. Though static block can not throw checked exception, they can still throw unchecked exception, which if occurred may result in ExceptionInitializerError.  Actually any runtime exception thrown during instantiation and initialization of static fields, will be wrapped by Java runtime into this error. This is also one of the most common reason of  NoClassDefFoundError in Java, because the class in question was not present in memory when its client needed them.

7) One more thing to know about static methods is that they are bonded during compile time using static binding.

8) One of the important property of static field is initialization. Static fields or variables are initialized when class is loaded into memory. They are initialized from top to bottom in the order they are declared in Java source file.

9) During Serialization, like transient variables, static variables are also not serialized. It means, if you store any data in static filed then after de-serialization, new object will contain its default value e.g.
10) Another feature related to static keyword is called static import.

Why String Class is made Immutable or Final
http://java67.blogspot.com/2014/01/why-string-class-has-made-immutable-or-final-java.html
1) String Pool
store String literals in String pool. Goal was to reduce temporary String object by sharing them and in order to share, they must have to be from Immutable class.
4) Multithreading Benefits

5) Optimization and Performance
String cache its hashcode. It even calculate hashcode lazily and once created, just cache it. In simple world, when you first call hashCode() method of any String object, it calculate hash code and all subsequent call to hashCode() returns already calculated, cached value.

2) Security
If String was not immutable, a user might have granted to access a particular file in system, but after authentication he can change the PATH to something else, this could cause serious security issues. Similarly, while connecting to database or any other machine in network, mutating String value can pose security threats. Mutable strings could also cause security problem in Reflection as well, as the parameters are strings.

3) Use of String in Class Loading Mechanism
As String been not Immutable, an attacker can take advantage of this fact and a request to load standard Java classes e.g. java.io.Reader can be changed to malicious class.

5) Hashmap keys
It's one of the most popular object to be used as key in hash based collections e.g. HashMap and Hashtable. Though immutability is not an absolute requirement for HashMap keys, its much more safe to use Immutable object as key than mutable ones, because if state of mutable object is changed during its stay inside HashMap, it would be impossible to retrieve it back, given it's equals() and hashCode() method depends upon the changed attribute.

Disadvantages
Since String is immutable, it generates lots of temporary use and throw object, which creates pressure for Garbage collector.
new String() will not pick object from String pool.

String pool is located in PermGen Space of Java Heap, which is very limited as compared to Java Heap. Having too many String literals will quickly fill this space, resulting in java.lang.OutOfMemoryError: PermGen Space.
from Java 7 onwards, they have moved String pool to normal heap space, which is much much larger than PermGen space.

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts