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.
Tag Archives: String
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:
- Using the + operator
- Using a StringBuilder
- Using a StringBuffer
- Using String.concat()
- Using String.join (new in Java8)
- 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.
- 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:
- 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?
- 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()?
- 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?
- String.concat() ought to work well for 2 strings but does it work well in a loop?
- String.join() has more functionality that StringBuilder, how does it affect performance if we instruct it to join Strings using an empty delimiter?
Table of contents
- Humanize String
- Dehumanize String
- Transform String
- Truncate String
- Format String
- Humanize Enums
- Dehumanize Enums
- Humanize DateTime
- Humanize TimeSpan
- Humanize Collections
- Inflector methods
- Fluent date
- Number to words
- Number to ordinal words
- Roman numerals
- Mix this into your framework to simplify your life
- How to contribute?
- Continuous Integration from TeamCity
- Main contributors
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 (
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>>
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
String is basically a wrapper around
char 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
I will show you few examples, together with
hashCode() of each
hashCode() 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.
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.