Tag Archives: String

How to Read JSON String in Java – Json-Simple Example

via How to Read JSON String in Java – Json-Simple Example.

JSON is a text format is a widely used as data-interchange language because its parsing and its generation is easy for programs. It is slowly replacing XML as most powerful data interchange format, as it is lightweight, consumes less bandwidth and also platform independent.  Though Java doesn’t have built in support for parsing JSON files and objects, there are lot of good open source JSON libraries are available which can help you to read and write JSON object to file and URL. Two of the most popular JSON parsing libraries are Jackson and Gson. They are matured, rich and stable. Though there are couple of more libraries there like JSON simple, which we are going to use in this example. Between Jackson and Gson, later does very nice job in mapping JSON object and serialization. JSON is also used in request and response between client server communication. In this tutorial we are going to see how to read and write JSON to file using JSON.Simple library, and you will notice yourself how simple working with JSON is.

The Optimum Method to Concatenate Strings in Java

via Rational Java: The Optimum Method to Concatenate Strings in Java.

This got me thinking about the different ways in Java to concatenate Strings and how they would all perform against each other. These are the methods I’m going to investigate:

  1. Using the + operator
  2. Using a StringBuilder
  3. Using a StringBuffer
  4. Using String.concat()
  5. Using String.join (new in Java8)
I also experimented with String.format() but that is so hideously slow that I will leave it out of this post for now.
Before we go any further we should separate two use cases:
  1. Concatenating two Strings together as a single call, for example in a logging message. Because this is only one call you would have thought that performance is hardly an issue but the results are still interesting and shed light on the subject.
  2. Concatenating two Strings in a loop.  Here performance is much more of an issue especially if your loops are large.

My initial thoughts and questions were as follows:

  1. The + operator is implemented with StringBuilder, so at least in the case of concatenating two Strings it should produce similar results to StringBuilder. What exactly is going on under the covers?
  2. StringBuilder should be the most efficient method, after all the class was designed for the very purpose of concatenating Strings and supersedes StringBuffer. But what is the overhead of creating the StringBuilder when compared with String.concat()?
  3. StringBuffer was the original class for concatenating Strings – unfortunately its methods are synchronized. There really is no need for the synchronization and it was subsequently replaced by StringBuilder which is not synchronized.  The question is, does the JIT optimise away the synchronisation?
  4. String.concat() ought to work well for 2 strings but does it work well in a loop?
  5. String.join() has more functionality that StringBuilder, how does it affect performance if we instruct it to join Strings using an empty delimiter?
The first question I wanted to get out of the way was how the + operator works. I’d always understood that it used a StringBuilder under the covers but to prove this we need to examine the byte code.
The easiest way to look at byte code these days is with JITWatch which is a really excellent tool created to understand how your code is compiled by the JIT.  It has a great view where you can view your source code side by side with byte code (also machine code if you want to go to that level).

Humanizer meets all your .NET needs

Click to Read:

Table of contents


In this example we shall explain how to use java.lang.reflect.Array. Thejava.lang.reflect.Array class provides static methods to dynamically create and access Java arrays. Arrays are objects that contain values of the same type, with immutable length. So, an array’s components of the same type and their number is always fixed. The length of an array must be set when an array is created, whereas the type of the components may be primitive (int, byte, long), an object type (String, Integer,Long), or even another array, but is always single.

Below, we make use of the methods provided in the java.lang.reflect.Array class, in order to create new instances of arrays, set values to their components, get their components values, get the types of the components of every array and finally get each array’s length. Read more>>

String memory internals

String memory internals

This article is based on my answer on StackOverflow. I am trying to explain how Stringclass stores the texts, how interning and constant pool works.
The main point to understand here is the distinction between String Java object and its contents – char[] under private value field. String is basically a wrapper aroundchar[] array, encapsulating it and making it impossible to modify so the String can remain immutable. Also the String class remembers which parts of this array is actually used (see below). This all means that you can have two different Stringobjects (quite lightweight) pointing to the same char[].
I will show you few examples, together with hashCode() of each String andhashCode() of internal char[] value field (I will call it text to distinguish it from string). Finally I’ll show javap -c -verbose output, together with constant pool for my test class. Please do not confuse class constant pool with string literal pool. They are not quite the same. See also Understanding javap’s output for the Constant Pool.

Java StringBuffer

Java StringBuffer

In this example we are going to present the StringBuffer class, which is contained in the java.lang package. We are going to show some of its most important uses and methods, and explain why and when it should be used, as well as the difference between StringBuffer and String.

Palindrome Program in Java

Palindrome Program in Java

In this example we are going to see how you can create a simple Java Application to check weather a String is a palindrome. A String is considered a palindrome if it can be similarly read both from left to right and from right to left. For example"abbcbba", “12321”, “69796” are all palindromes.