Found 7442 Articles for Java

How can we add a JSONArray to JSONObject in Java?

raja
Updated on 04-Jul-2020 05:36:04

19K+ Views

The JSON is a text-based format for exchanging data. It is a lightweight component and language independent. We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method. Exampleimport org.json.*; import java.util.*; public class AddJSONArrayToJSONObjTest {    public static void main(String args[]) {       List list = new ArrayList();       list.add("Raja");       list.add("Jai");       list.add("Adithya");       JSONArray array = new JSONArray();       for(int i = 0; i < list.size(); ... Read More

How can we read a JSON file in Java?

raja
Updated on 02-Sep-2023 12:55:51

61K+ Views

The JSON is one of the widely used data-interchange formats and is a lightweight and language independent. The json.simple is a lightweight JSON processing library that can be used to read and write JSON files and it can be used to encode or decode JSON text and fully compliant with JSON specification (RFC4627). In order to read a JSON file, we need to download the json-simple.jar file and set the path to execute it.json fileExampleimport java.io.*; import java.util.*; import org.json.simple.*; import org.json.simple.parser.*; public class JSONReadFromTheFileTest {    public static void main(String[] args) {       JSONParser parser = new JSONParser();       try {     ... Read More

Difference between ArrayList and HashSet in Java

Vivek Verma
Updated on 05-May-2025 14:47:43

16K+ Views

In Java, ArrayList and HashSet are the most important classes of the Collection Framework. Both are used to "store collections of elements", but they are used for different purposes and have different characteristics. ArrayList is an "ordered collection" that allows duplicate elements, while HashSet is an "unordered collection" that does not allow duplicates. ArrayList vs HashSet in Java Here are some key differences between ArrayList and HashSet in Java: Key ArrayList HashSet Implementation ArrayList is the implementation of the List interface. HashSet, on the other hand, is the implementation of a set interface. ... Read More

Difference between Applets and Servlets in Java.

Aishwarya Naglot
Updated on 10-Oct-2024 13:40:48

6K+ Views

In Java, both Applets and Servlets are the programs or applications that run in a Java environment. Applets are designed to provide interactive features that can be embedded into web pages and allow users to engage with content directly through their web browsers. On the other hand, Servlets operate on the server side, they handle requests and responses, which is most important for generating dynamic content in web applications. The main difference in both programs is that their processing is done in different environments. Difference between Applets and Servlets Some of the major differences between Applets and Servlets are as ... Read More

Difference between an Iterator and ListIterator in Java

Nitin Sharma
Updated on 16-Sep-2019 11:31:47

1K+ Views

Java provided these two interfaces to traverse the data one by one stored in a collection. The internal implementation of iterator and list iterator makes them differ apart but the main agenda of both the iterators is the same.The following are the important differences between Iterator and ListIterator.Sr. No.KeyIteratorListIterator1ApplicableIterator can be used to traverse any collection irrespective of the type of collection.List iterator can only be used to iterate only List collection implemented classes like arraylist, linkedlist etc.2CallingAs mentioned Iterator must be used to enumerate elements in all Collections implemented interfaces like Set, List, Queue, Deque and also in all ... Read More

Difference between an Integer and int in Java

Nitin Sharma
Updated on 02-Mar-2020 10:04:38

12K+ Views

A Java both int and Integer are used to store integer type data the major difference between both is type of int is primitive while Integer is of class type.This difference become significant when concept of OOPs comes in picture during development as int follows the principle of primitive data type while Integer behave as a wrapper class.Following are the important differences between int and Integer.Sr. No.KeyintInteger1TypeA int is a data type that stores 32 bit signed two's compliment integer.On other hand Integer is a wrapper class which wraps a primitive type int into an object.2Purposeint helps in storing integer ... Read More

Difference between a Static Queue and a Singly Linked List in Java.

Nitin Sharma
Updated on 16-Sep-2019 11:10:29

864 Views

In Java List and Queue both are introduced as an ordered list of objects, where the same object may be added more than once. The difference between both comes in the manner of adding elements. In the queue, all the elements get inserted at the rear and removed from the front while we can add an element anywhere in the list.Sr. No.KeyStatic QueueSingly Linked List1Data initialization.Static Queue works in first out(FIFO) fashion as all the elements get inserted at the REAR and removed from the FRONT of the queue.In the case of Singly Linked List, one can add elements anywhere ... Read More

How can we convert a JSON string to a JSON object in Java?

raja
Updated on 03-Jul-2020 14:06:49

6K+ Views

The JSON stands for JavaScript Object Notation and it can be used to transfer and storage of data.  The JSONObject can parse text from a String to produce a map-like object. The object provides methods for manipulating its contents, and for producing a JSON compliant object serialization. The JSONArray can parse text from a String to produce a vector-like object. The object provides methods for manipulating its contents, and for producing a JSON compliant array serialization.In the below two examples, We can convert a JSON string to a JSON object.Example 1import org.json.JSONObject; import org.json.JSONArray; public class StringToJSONTest {    public static void main(String args[]) {   ... Read More

Can we throw an Unchecked Exception from a static block in java?

Maruthi Krishna
Updated on 12-Sep-2019 08:57:15

2K+ Views

A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.Example Live Demopublic class MyClass {    static{       System.out.println("Hello this is a static block");    }    public static void main(String args[]){       System.out.println("This is main method");    } }OutputHello this is a static block This is main methodExceptions in static blockJust like any other method in Java when an exception occurs in static block you can handle it using try-catch ... Read More

How do you throw an Exception without breaking a for loop in java?

Maruthi Krishna
Updated on 12-Sep-2019 08:54:22

9K+ Views

Whenever an exception occurred in a loop the control gets out of the loop, by handling the exception the statements after the catch block in the method will get executed. But, the loop breaks.Example Live Demopublic class ExceptionInLoop{    public static void sampleMethod(){       String str[] = {"Mango", "Apple", "Banana", "Grapes", "Oranges"};          try {             for(int i=0; i

Advertisements