Monday, 1 May 2017

What is Polymorphism in Java? Overriding or Overloading

What is Polymorphism in Java? Overriding or Overloading


Polymorphism vs Overloading vs Overriding
Someone asked me What are the difference between Polymorphism and Overriding in Java and the similar difference between Polymorphism and Overloading. Well, they are not two different things, Polymorphism is an object oriented or OOPS concept like Abstraction, Encapsulation or Inheritance which facilitate the use of the interface and allows Java program to take advantage of dynamic binding in Java. Polymorphism is also a way through which a Type can behave differently than expected based upon which kind of Object it is pointing. Overloading and overriding are two forms of Polymorphism available in Java.

Sunday, 30 April 2017

Difference between NoClassDefFoundError vs ClassNotFoundExcepiton in Java


Difference between NoClassDefFoundError vs ClassNotFoundExcepiton in Java


Both NoClassDefFoundError and ClassNotFoundException are dangerous errors which come when JVM or ClassLoader not able to locate class during the class loading process. Since different ClassLoader loads classes from a different location, sometimes this issue may be caused because of incorrect CLASSPATH as well i.e. some JAR files from lib are missing or from the old version. Though look quite similar there is a subtle difference between NoClassDefFoundError and ClassNotFoundException, NoClassDefFoundError indicates that class was present during the time of compilation but not available when you run Java program, sometimes error on static initializer block can also result in NoClassDefFoundError.

Wednesday, 26 April 2017

Examples of substring() in Java


Examples of substring() in Java

SubString in Java is a useful method from java.lang.String class, which is used to create smaller String from bigger ones. The way Substring works prior to Java 1.7 can create a subtle memory leak because both String and their substring shares same character array. Which means, if you have a big String of 200MB and created a substring of 2MB from that, that could prevent 200MB String from being garbage collected. I agree this doesn't look normal and indeed was a bug, but it was like that till Java 1.6 and it's various update. One reason, which I could think, why Java designer initially thought like that, maybe to save memory by sharing char array and to make, creating substring faster by just copying pointers, instead of data. Nevertheless, this was reported as bug and Oracle have fixed it, so no more substring memory leak issue in Java 7.

Sunday, 23 April 2017

15 Technical Core Java Interview Questions Answers for Experienced


15 Technical Core Java Interview Questions Answers for Experienced Developers


When the experience of a Java Programmer grows in the years e.g. when it goes from beginner years ( 2 to 4) to more experience or sort of senior level ( 5 to 7 years), Core Java Interview Questions also changes a bit. Of course, basics like data structurealgorithms, and object-oriented programming remains same, but types of questions will become more advanced and their answers will definitely need to be more detailed and accurate. I often receive queries about core Java questions asked to a senior developer of 5 to 6-year experience, or, sometimes, I am going for an interview of senior Java developer, what kind of questions I should expect. This sometimes puzzles me, that once you become senior, you automatically starts taking part in the interview, and you should have an idea of what to expect on Interviews, but at the same time, I can understand that having an idea of questions before going on Interview, helps preparation. Of course, you are not going to get a question like the one you have faced on 2 to 3 years level Java Interviews, but It also depends on different rounds of Interviews.

Saturday, 22 April 2017

Difference between FileReader vs FileInputStream in Java


Difference between FileReader vs FileInputStream in Java


Even though both FileReader and FileInputStream are used to read data from a file in Java, they are quite a different. The main difference between the FileReader and FileInputStream is that one read data from character stream while other read data from a byte stream. The FileReader automatically converts the raw bytes into character by using platform's default character encoding. This means you should use this class if you are reading from a text file which has same character encoding as the default one. If you happen to read a text file encoded in different character encoding then you should use InputStreamReader with specified character encoding. An InputStreamReader is a bridge between byte stream and character stream and can take a FileInputStream as a source. Though, it's worth remembering that it caches the character encoding which means you cannot change the encoding scheme programmatically.