Search

Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Thursday, May 28, 2015

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 most straight forward way is to use the String constructor which accepts a byte array i.e. new String(byte []) , but key thing to remember is character encoding. Since bytes are binary data but String is character data, its 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 its 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". If its different you may see some garbage characters or even different characters changing the meaning of text completely and I am not saying this by reading few books, but I have faced this issue in one of my project where we are reading data from database which contains some french characters. In the absent of any specified coding, our platform was defaulted on something which is not able to convert all those special character properly, I don't remember exact encoding. That issue was solved by providing "UTF-8" as character encoding while converting byte array to String. Yes, there is another overloaded constructor in String class which accepts character encoding i.e. new String(byte[], "character encoding").
Read more »

Tuesday, May 26, 2015

4 ways to concatenate Strings in Java - Best Performance

When we think about String Concatenation in Java, what comes in our mind is + 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 way to convert int to String, compiler internally translate that to StringBuilder append call, which result in 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 + operator, we can use StringBuffer, StringBuilder and concat() method from java.lang.String class for same purpose. StringBuilder and StringBuffer classes are there for just this reason, and you can see that in our performance comparison. StringBuilder is winner and fastest ways to concatenate Strings. StringBuffer is close second, because of synchronized method and rest of them are just 1000 times slower than them. Here we will see example of all four ways of concatenating Strings in Java.
Read more »

Thursday, December 4, 2014

Right way to Compare String in Java

String is a special class in Java, so is String comparison. When I say comparing String variables, it can be either to compare two String object to check if they are same, i.e. contains same characters or compare them alphabetically to check which comes first or second. In this article, we are going to talk about the right way of comparing String variables, but what is the wrong way? The wrong way is to compare String using == operator. It is one area in which almost every Java programmer  has made mistakes sometime by comparing two String variable using == operator. Many Java developers are exposed to string comparison very early in their Java journey,  It's often required in their first few programming assignments e.g. write a program to print hello if user enters "John".  When you first start with String in Java, you create object using String literal syntax e.g. name = "John" and then compare using == operator, you will get right answer, but if you take same String as user input, you will not get correct answer.  Why? because equality operator compare references i.e. if two reference variable points to same object in heap then it returns true, otherwise it returns false.
Read more »