Found 9150 Articles for Object Oriented Programming

How to create a JSON using Jackson Tree Model in Java?

Aishwarya Naglot
Updated on 13-May-2025 15:39:15

1K+ Views

To create a JSON using the Jackson Tree Model in Java, we can use the ObjectMapper class. The ObjectMapper class is part of the Jackson library and is used to convert Java objects to JSON and vice versa. To use the Jackson library, we need to add it to our project. If you are using Maven, add this to your pom.xml file: com.fasterxml.jackson.core jackson-databind 2.13.0 If you are not using Maven, you can download the jar file from here. Creating a JSON using Jackson Tree Model in ... Read More

How do we make my string comparison case insensitive in java?

Maruthi Krishna
Updated on 11-Oct-2019 08:25:47

827 Views

We can compare Strings in Java in various ways −Using the comapareTo() method − The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.Example Live Demoimport java.util.Scanner; public class StringComparison {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter string1: ");       String str1 = sc.next();       System.out.println("Enter string2: ");       String str2 ... Read More

How to parse for words in a string for a specific word in java?

Maruthi Krishna
Updated on 05-Jun-2025 15:21:38

978 Views

In this article, we will learn how to parse for words in a string for a specific word in Java,  i.e., we need to check if a specific word exists in a string, and if it does, we will parse the string to find that word. There are various methods in Java that you can use to parse a string for a specific word. Here we are going to discuss 3 of them. Using the contains() method Using the indexOf() method Using the StringTokenizer class Using ... Read More

How to create a variable that can be set only once but isn\'t final in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:43:48

2K+ Views

How to create a variable that can be set only once, meaning it can be assigned a value only once but is not final in Java? Solution In Java we can solve the above using the following two methods: Using a custom wrapper class Using an AtomicReference Using a custom wrapper class We can create a custom wrapper class that allows setting a value only once. The class will have a boolean flag to check if the value has already been set. Wrapper classes are used for converting primitive data types into objects. We will use this for setting ... Read More

Can a final variable be initialized when an object is created in Java?

Maruthi Krishna
Updated on 11-Oct-2019 07:38:33

1K+ Views

Once you declare a variable final, after initializing it, you cannot modify its value further. Moreover, like instance variables, final variables will not be initialized with default values.Therefore, it is mandatory to initialize final variables once you declare them. If not a compile time error will be generated.Example Live Demopublic class FinalExample {    final int j;    public static void main(String args[]){       FinalExample obj = new FinalExample();       System.out.println(obj.j);    } }Compile time errorFinalExample.java:5: error: non-static variable j cannot be referenced from a static context System.out.println(j); ^ 1 errorInitializing the final variableYou can initialize a ... Read More

How do we check if a String contains a substring (ignoring case) in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:51:02

5K+ Views

Problem Statement The task is, given a string and a substring, you have to check if a substring is present in a string or not. Do not mind if the case of the string and substring is different, it should not matter. For example, if the string is "Hello World" and the substring is "hello", then the output should be true. Even if the substring is "HELLO" or "HeLLo", the output should be true. Solution To solve this problem, we will first convert the string and the substring to lowercase using the toLowerCase() method. The toLowerCase () method of the ... Read More

How to get first and last elements from ArrayList in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:48:08

16K+ Views

ArrayList is a part of the Java Collections Framework. Which is a dynamic type of array that can grow and shrink as needed. It is a resizable array implementation of the List interface. The ArrayList class is used when we want to store a list of elements in a dynamic array. In this article, let's learn how to get the first and last elements from an ArrayList in Java. The get() method of the ArrayList class accepts an integer representing the index value and returns the element of the current ArrayList object at the specified index. Therefore, if you pass ... Read More

How to remove a SubList from an ArrayList in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:52:19

2K+ Views

In this article, let's learn how to remove a sublist from an ArrayList in Java. The ArrayList class is a part of the Java Collections Framework. It is a resizable array and an implementation of the List interface. The ArrayList class is used when we want to store a list of elements in a dynamic array. Sub-list is a specific portion of an array list. Basically, it is a list of elements that are part of the original list but it is not the entire list itself. Using subList() and clear() method. ... Read More

How to copy a specific section of an array in Java?

Maruthi Krishna
Updated on 11-Oct-2019 07:38:13

428 Views

Using copyOf() methodThe copyOf() method of the Arrays class (java.util package) accepts two parameters −an array (of any type).an integer value representing length.And copies the contents of the given array from starting position to given length and returns the new array.Example Live Demoimport java.util.Arrays; public class CopyingSectionOfArray {    public static void main(String[] args) {       String str[] = new String[10];       //Populating the array       str[0] = "Java";       str[1] = "WebGL";       str[2] = "OpenCV";       str[3] = "OpenNLP";       str[4] = "JOGL";     ... Read More

How can a String be validated (for alphabets) in java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:45:51

5K+ Views

Problem Statement The given problem is to validate a string to check if that string contains only alphabets (both uppercase and lowercase) and does not contains any other characters like numbers, special characters, etc.. For example, the string "HelloWorld" is valid as it contains only alphabets, while "Hello123" is not valid as it contains numbers. Solution We can solve this problem by using following methods: Using Regular Expressions Manual Checking using loop Using Character.isAlphabetic() Using Regular Expressions We can use regular expressions to validate a string for alphabets. The regular expression ^[a-zA-Z]+$ matches a string that contains only ... Read More

Advertisements