Found 9150 Articles for Object Oriented Programming

How to extract an HTML tag from a String using regex in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:36:05

3K+ Views

In this article, we will see how to extract an HTML tag from a string using regex in Java. We can achieve this in multiple ways, but if we use regex, it will be better than others and also give us fast performance. What is Regex? Regex is a sequence of characters that describes a search pattern that is used to find a particular pattern in a String. It is also known as a regular expression or regexp. We use regex for pattern matching or searching or replacing a String. We will use the java.util.regex package of Java that provides ... Read More

What is ARM in Java?

Maruthi Krishna
Updated on 01-Sep-2025 14:03:26

1K+ Views

Automatic resource management(ARM) or try-with-resources is a new exception handling mechanism that we are going to discuss in this article. What is a resource? A resource is an object which implements AutoClosable interface. Whenever you use a resource in your program, it is recommended to close it after use. For example, if you open a file using FileInputStream, it is recommended to close the stream after usage. This is done to free up the system resources. In Java, we can use the close() method to close the resources. But if you forget to close the resource, it will lead ... Read More

What is the role of the String intern() method in java?

Maruthi Krishna
Updated on 10-Oct-2019 08:55:14

132 Views

A String is a class in Java which stores a sequence of characters, it belongs to the java.lang package. Once you create a String object you cannot modify them (immutable).StorageAll String objects are stored in a separate memory location in the heap area known as, String Constant pool.Whenever you define a String value JVM creates a String object with the given value in the String constant pool. Therefore, if you run the above program two String values are created in the String constant pool.The intern() methodThis method returns value of the current String from the pool of unique String values. ... Read More

What are the rules of exception handling with respect to method overriding in java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:24:13

732 Views

While a superclass method throws an exception when overriding it, you need to follow certain rules. We will be discussing these rules in this chapter. What is Exception Handling in Java? Exception handling is a mechanism to handle errors in the program. For example, if a program tries to divide a number by zero, it will throw an ArithmeticException. In such cases, the program will terminate abnormally. To avoid this, we can use exception handling. Now, let's discuss the rules of exception handling with respect to method overriding. Those are: Should throw the same exception or a subtype ... Read More

How do we split a string with any whitespace chars as delimiters using java?

Maruthi Krishna
Updated on 01-Sep-2025 12:30:57

845 Views

What is split() method in Java? The split() method of the String class accepts a delimiter (in the form of a string), divides the current String into smaller strings based on the delimiter, and returns the resulting strings as an array. If the String does not contain the specified delimiter, this method returns an array that contains only the current string. If the String does not contain the specified delimiter, this method returns an array containing the whole string as an element. Splitting the string with white space as delimiter Following are steps to split a String into an array ... Read More

How to convert Java object to JSON using Jackson library?

Maruthi Krishna
Updated on 06-Sep-2023 11:49:04

51K+ Views

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc.There are several Java libraries available to handle JSON objects. Jackson is a simple java based library to serialize java objects to JSON and vice versa.Converting Java object to JSONThe ObjectMapper class of the Jackson API in Java provides methods to convert a Java object to JSON object and vice versa.The writeValueAsString() method of this class accepts a JSON object as a parameter and returns its respective JSON StringTherefore, ... Read More

How to convert Java object to JSON using GSON library?

Maruthi Krishna
Updated on 10-Oct-2019 06:35:44

2K+ Views

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc.There are several Java libraries available to handle JSON objects. Google Gson is a simple Java-based library to serialize Java objects to JSON and vice versa. It is an open-source library developed by Google.Converting Java object to JSONThe Google's Gson library provides a class with the same name (Gson) which is the main class of the library.This class provides a method named toJson() there are several variants of this ... Read More

In how many ways we can convert a String to a character array using Java?

Maruthi Krishna
Updated on 10-Oct-2019 06:32:04

330 Views

You can convert a String to a character array either by copying each element of the String to an array or, using the toCharArray() method.Copying each elementGet the String to be converted.Create an empty character array with the length of the String.The charAt() method of the String class returns the character at a particular position. Using this method copy each character of the String to the array.Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class StringToCharArray {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a String value: ");     ... Read More

Convert CSV to JSON using the Jackson library in Java?

raja
Updated on 06-Jul-2020 11:44:03

8K+ Views

A Jackson is a Java JSON API that provides several different ways to work with JSON. We can convert CSV data to JSON data using the CsvMapper class, it is specialized ObjectMapper, with extended functionality to produce CsvSchema instances out of POJOs. We can use the reader() method for constructing ObjectReader with default settings. In order to convert this, we need to import the com.fasterxml.jackson.dataformat.csv package.In the below example, convert a CSV to JSON.Exampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.dataformat.csv.*; public class CsvToJsonTest {    public static void main(String args[]) throws Exception {       File input = new File("input.csv");       try { ... Read More

How to deserialize a JSON array to list generic type in Java?

raja
Updated on 06-Jul-2020 11:38:01

2K+ Views

The Gson library provides a class called com.google.gson.reflect.TypeToken to store generic types by creating a Gson TypeToken class and pass the class type. Using this type, Gson can able to know the class passed in the generic class.Syntaxpublic class TypeToken extends ObjectWe can deserialize a JSON array to a generic type of list in the below exampleExampleimport java.lang.reflect.Type; import java.util.*; import com.google.gson.*; import com.google.gson.reflect.*; public class JSONArrayToListTest {    public static void main(String args[]) throws Exception {       String jsonStr = "[{\"name\":\"Adithya\", \"course\":\"Java\"}, " + "{\"name\":\"Ravi\", \"course\":\"Python\"}]";       Type listType = new TypeToken() {}.getType();       List students = ... Read More

Advertisements