What is JSON used for? What are the ways to parse JSON String to Object in Java

 JavaScript Object Notation JSON is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications. 

One of the common task in Java web application, particularly the ones which deals with  web services are parsing JSON messages. Many times you need to parse JSON to create a Java object like parsing a JSON message to create a POJO,  for example, an Order or a Book. Representing JSON in Java is easy, it's like a String value but unfortunately, JDK doesn't provide any standard API to parse JSON in Java. . There are many good open-source JSON parsing libraries you can use to parse any kind of JSON in your Java program. 

1. Parsing JSON using Jackson in Java

It's pretty easy to parse JSON messages into a Java object using Jackson. You just need to use the ObjectMapper class, which provides the readValue() method. This method can convert JSON String to Java object, that's why it takes JSON as a parameter and the Class instance in which you want to parse your JSON. 


Here is an example to convert our JSON String to an EBook object in Java:


ObjectMapper mapper = new ObjectMapper();

EBook effectiveJava = mapper.readValue(JSON, EBook.class);


Also, the readValue method throws IOException. I have omitted the try-catch statement for the sake of readability but you need to either throw that exception or catch it using the try-catch-finally block. 


This is probably the single most important JSON library you should know as a Java developer. It's feature-rich and also quite fast. Usually, you don't need any other JSON library if you are using Jackson. 


Btw, to use the Jackson library, you need to use either jackson-core.jar, jackson-databind.jar, or jackson-annotations.jar in your classpath. Alternatively, you can also add the following Maven dependency in your Eclipse project's pom.xml, if you are using Maven in Eclipse:


<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.2.3</version>

</dependency>

ObjectMapper mapper = new ObjectMapper(); 

try 

{

 EBook effectiveJava = mapper.readValue(JSON, EBook.class); 

System.out.println("Java object after parsing JSON using Jackson"); System.out.println(effectiveJava); 

}

 catch (IOException e) 

{

 e.printStackTrace();

 }


What is the difference between 32-bit and 64-bit JVM?

64-bits JVMs can allocate more memory than the 32-bits ones.

 In 32-bit JVM maximum, addressable memory space is only two to the power of thirty two 2^32 (i.e.~4gb). It means the maximum memory size of your Java process can’t be more than 4GB. Due to various additional constraints such as available swap, kernel address space usage, memory fragmentation, and VM overhead, in practice, the limit is much lower. T

Another questions is, does same Java program can run on both 32-bit and 64-bit JVM?  Java is platform independent and same Java program will run absolutely fine in both 32-bit and 64-bit JVM. The real difference comes in addressable memory or heap memory. In 32-bit JVM, the theoretical limit of maximum heap size is around 4G but practically you get way less than that e.g. around 1.5 GB in Windows and close to 3 GB in Solaris

 

What is the difference between the start() and run() method of the Thread class?

 start() method is used to start a newly created thread, while start() internally calls run() method, there is difference calling run() method directly. 


When you invoke run() as a normal method, it's called in the same thread, no new thread is started, which is the case when you call the start() method. 





When to use Runnable vs Thread in Java?

We know we can implement thread either by extending the Thread class or implementing Runnable interface, but which one is better and when to use one? This question will be easy to answer if you know that the Java programming language doesn't support multiple inheritances of class, but it allows you to implement multiple interfaces. 


This means it's better to implement Runnable than extend Thread if you also want to extend another class e.g. Canvas or CommandListener.


What is Thread in Java?

 The thread is an independent path of execution. It's a way to take advantage of multiple CPUs available on a machine. By using multiple threads you can speed up CPU-bound tasks. For example, if one thread takes 100 milliseconds to do a job, you can use 10 threads to reduce that task into 10 milliseconds. Java provides  support for multithreading at the language level

Interview Questions

 How is Java different from C++?

C++ is only a  compiled language, whereas Java is compiled as well as an interpreted language.

Java programs are machine-independent whereas a c++ program can run only in the machine in which it is compiled. 

C++ allows users to use pointers in the program. Whereas java doesn’t allow it. Java internally uses pointers. 

C++ supports the concept of Multiple inheritances whereas Java doesn't support this. And it is due to avoiding the complexity of name ambiguity that causes the diamond problem

Is Java a completely object-oriented programming language?


It is not wrong if we claim that java is the complete object-oriented programming language. Because Everything in Java is under the classes. And we can access that by creating the objects.


But also if we say that java is not a completely object-oriented programming language because it has the support of primitive data types like int, float, char, boolean, double, etc.


Now for the question: Is java a completely object-oriented programming language? We can say that - Java is not a pure object-oriented programming language, because it has direct access to primitive data types. And these primitive data types don't directly belong to the Integer classes.