Raja has Published 760 Articles

What are the advantages of private methods in an interface in Java 9?

raja

raja

Updated on 21-Feb-2020 04:59:01

435 Views

In Java 9, an interface can also have private methods. Apart from static and default methods in Java 8, this is another significant change as it allows the re-usability of common code within the interface itself.In an interface, there is a possibility of writing common code on more than one default method that leads to code duplication. ... Read More

What are the new methods added to Process API in Java 9?

raja

raja

Updated on 21-Feb-2020 04:57:55

95 Views

Java 9 improves Process class by adding new methods and also provides a new interface: ProcessHandle and ProcessHandle.Info to get all the details about the process and its information.Below is the list of new methods added to Process in Java 9boolean supportsNormalTermination(): It can return true if the implementation of the destroy() ... Read More

How to add/insert additional property to JSON string using Gson in Java?

raja

raja

Updated on 19-Feb-2020 10:28:54

7K+ Views

The com.google.gson.JSonElement class represents an element of Json. We can use the toJsonTree() method of Gson class to serialize an object's representation as a tree of JsonElements. We can add/ insert an additional property to JSON string by using the getAsJsonObject() method of JSonElement. This method returns to get the element as JsonObject.Syntaxpublic ... Read More

When to use @JsonValue annotation using Jackson in Java?

raja

raja

Updated on 19-Feb-2020 10:02:51

4K+ Views

The @JsonValue annotation is useful at the method level. We can use this annotation to generate a JSON string from java object. If we want to print a serialized object then override the toString() method. But using @JsonValue annotation, we can define a way in which java object is serialized.Syntax@Target(value={ANNOTATION_TYPE, ... Read More

How to convert an array to JSON Array using JSON-lib API in Java?

raja

raja

Updated on 19-Feb-2020 07:46:28

396 Views

A Java array is an object which stores multiple variables of the same type, it can hold primitive types and object references whereas JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values, an internal form is an object having get() and opt() methods for ... Read More

How can we implement a JSON array using Streaming API in Java?

raja

raja

Updated on 18-Feb-2020 10:47:33

815 Views

The JsonGenerator interface can be used to write the JSON data to an output source in a streaming way. We can create or implement a JSON array using the writeStartArray() method of JsonGenerator, this method writes the JSON name/start array character pair within the current object context. The writeStartObject() method writes ... Read More

How can we sort a JSONObject in Java?

raja

raja

Updated on 18-Feb-2020 10:07:15

5K+ Views

A JSONObject is an unordered collection of a key, value pairs, and the values can be any of these types like Boolean, JSONArray, JSONObject, Number and String. The constructor of a JSONObject can be used to convert an external form JSON text into an internal form whose values can be retrieved with ... Read More

How to create a class and object in JShell in Java 9?

raja

raja

Updated on 17-Feb-2020 13:36:48

245 Views

JShell is a new java shell tool released in java 9. It is the first official REPL (Read-Evaluate-Print-Loop) application. This tool helps in executing and evaluating simple java programs and logics such as statements, loops, expressions, and etc. Java REPL provides a simple programming environment in the command prompt. It ... Read More

How to deserialize a JSON string using @JsonCreator annotation in Java?

raja

raja

Updated on 17-Feb-2020 08:18:20

792 Views

The @JsonProperty annotation can be used to indicate the property name in JSON. This annotation can be used for a constructor or factory method. The @JsonCreator annotation is useful in situations where the @JsonSetter annotation cannot be used. For instance, immutable objects do not have any setter methods, so they need their initial ... Read More

How to define alternate names to a field using Jackson in Java?

raja

raja

Updated on 17-Feb-2020 07:20:53

4K+ Views

The @JsonAlias annotation can define one or more alternate names for the attributes accepted during the deserialization, setting the JSON data to a Java object. But when serializing, i.e. getting JSON from a Java object, only the actual logical property name is used instead of the alias.Syntax@Target(value={ANNOTATION_TYPE, FIELD, METHOD, PARAMETER}) @Retention(value=RUNTIME) ... Read More

Advertisements