HTML

Best Book to Learn Java Programming for Beginners?

There is no doubt that the best book to learn Java for beginners is indeed Head First Java, 2nd Edition. It's interesting, informative and yet easy to read, which is what a beginner wants. The only drawback of this book is that there is no 3rd Edition available. Java has moved a long way since 2nd edition of this book was released. Yes, the core of the Java programming language is not changed much and information given in this book is still relevant and sufficient for anyone who wants to learn Java programming, but an up to date book comprising changes introduced in Java 7 and Java 8 would have been much appreciated. I was hoping for Head First Java 3rd Edition when Java 8 was launched last year, but no update yet. The changes introduced in Java 8 does demand a new edition of the book, but that is for advanced level.

How to Convert Byte array to String in Java with Example

There are multiple ways to convert a byte array to String in Java but the most straightforward way is to use the String constructor which accepts a byte array i.e. new String(byte []) , but the key thing to remember is character encoding. Since bytes are binary data but String is character data, it's very important to know the original character encoding of the text from which byte array has created. If you use a different character encoding, you will not get the original String back. For example, if you have read that byte array from a file which was encoded in "ISO-8859-1" and you have not provided any character encoding while converting byte array to String using new String() constructor then it's not guaranteed that you will get the same text back? Why? because new String() by default uses platform's default encoding (e.g. Linux machine where your JVM is running), which could be different than "ISO-8859-1".

4 ways to concatenate Strings in Java - Best Performance

When we think about String Concatenation in Java, what comes to our mind is the + operator, one of the easiest way to join two String, or a String and a numeric in Java. Since Java doesn't support operator overloading, it's pretty special for String to have behavior. But in truth, it is the worst way of concatenating String in Java. When you concatenate two String using + operator e.g. "" + 101, one of the popular ways to convert int to String, compiler internally translates that to StringBuilder append call, which results in the allocation of temporary objects. You can see the real difference in performance of our example program, in which we have concatenated 100,000 String using + operator. Anyway, this article is not just about + operator but also about other ways to concatenate multiple Strings. There are four ways to do this, apart from the + operator, we can use StringBuffer, StringBuilder, and concat() method from java.lang.String class for the same purpose.

Difference between Abstraction and Polymorphism in Java OOPS

Abstraction and Polymorphism are very closely related and understanding  the difference between them is not as easy as it looks. Their operating model is also very similar and based upon the relationship of parent and child classes. In fact, Polymorphism needs the great support of Abstraction to power itself, without Abstraction you cannot leverage the power of Polymorphism. Let's understand this by what Abstraction and Polymorphism provide to an object oriented program. Abstraction is a concept to simplify the structure of your code. Abstraction allows you to view things in more general terms rather than looking them as they are at the moment, which gives your code flexibility to deal with the changes coming in future.

How to convert String to Float in Java and vice-versa - Tutorial

There are three ways to convert a String to float primitive in Java parseFloat(), valueOf() method of Float class and new Float() constructor. Suppose you have String which represents a floating point number e.g. "3.14" which is the value of PIE, you can convert it to float by using any of those three methods. Since String is one of the most prominent data types in Java, you will often find yourself converting String to Int, Double, and other data types and vice-versa. Java designer knows about that and they have made arrangement to carry out this basic task in a predictable and consistent manner. Once you know the trick to convert String to float, you should be able to convert String to Integer, Double, and Short. BTW, converting String to a byte array is a little bit tricky because String is text data and bytes are binary, so character encoding comes into the picture. If you want to learn more about how to do conversion between String and byte array, see this step by step tutorial for String to a byte array.