Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Articles - Page 223 of 745
2K+ Views
Using regular expressionYou can find whether a particular String value contains ASCII characters using the following regular expression −\A\p{ASCII}*\zThe matches() method of the String class accepts a regular expression and verifies whether the current string matches the given expression if so, it returns true, else it returns false.Therefore, Invoke the matches() method on the input/required string by passing the above specified regular expression as a parameter.Example Live Demoimport java.util.Scanner; public class OnlyASCII { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a string value: "); String input ... Read More
5K+ Views
You can initialize the array variable which is declared inside the class just like any other value, either using constructor or, using the setter method.ExampleIn the following Java example, we are declaring an instance variable of array type and initializing it from the constructor. Live Demopublic class Student { String name; int age; String subs[]; Student(String name, int age, String subs[]){ this.name = name; this.age = age; this.subs = subs; } public void display() { System.out.println("Name: "+this.name); System.out.println("Age :"+this.age); System.out.print("Subjects: "); for(int i = 0; i < subs.length; i++) { ... Read More
961 Views
ArrayList to ArrayList Instead of the typed parameter in generics (T) you can also use “?”, representing an unknown type. These are known as wild cards you can use a wild card as − Type of parameter or, a Field or, a Local field. Using wild cards, you can convert ArrayList to ArrayList as − ArrayList stringList = (ArrayList)(ArrayList)(list); Example import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; public class ArrayListExample { public static void main(String args[]) { //Instantiating an ArrayList object ... Read More
9K+ Views
Jackson is a Java-based library, and it can be useful for converting Java objects to JSON and JSON to Java objects. A Jackson API is faster than other API, needs less memory, and is good for large objects. We can convert a JSON array to a list using the ObjectMapper class. It has a useful method, readValue(), which takes a JSON string and converts it to the object class specified in the second argument. Before proceeding further, first of all, we need to import the Jackson library into our project. To do this, you can add the following dependency to ... Read More
1K+ Views
The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. We can serialize a list of objects using the serialize() method of JSONSerializer class. This method can perform a shallow serialization of the target instance. We need to pass a list of objects of List type as an argument to the serialize() method.Syntaxpublic String serialize(Object target)Exampleimport flexjson.JSONSerializer; import java.util.*; public class JsonSerializeListTest { public static void main(String[] args) { JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print JSON Student s1 = new Student("Raja", "Ramesh", 28, "Hyderabad"); Student s2 = new Student("Suresh", "Kumar", 30, "Chennai"); ... Read More
7K+ Views
JSON is a format for storing and exchanging data. It is easily readable and also easy to parse. Creating JSON Using Java JsonGenerator In Java, we can create a JSON object using the JsonGenerator class. JsonGenerator is class in the javax.json.stream package that is used to create JSON data. It provides methods to write JSON objects, arrays, and values. To use the JsonGenerator class, we need to add the javax.json library to our project. If you are using Maven, add this to your pom.xml file: javax.json javax.json-api 1.1.4 If you are not using Maven, you can download ... Read More
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
837 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
995 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
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