Found 7442 Articles for Java

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

Sorting contents of a string that holds integer values in Java

Aishwarya Naglot
Updated on 01-Sep-2025 12:50:16

4K+ Views

Problem Statement Here we have a string that contains integer values, our task is to sort those integer values in ascending order. For example, if the string is "31134", the sorted string should be "11334". Solution We can solve the above problem using multiple ways: Using char array Using Stream API Using Bubble Sort Using char array We will use the toCharArray() method of the String class to convert the string into a character array. Then we will sort the character array using the Arrays.sort() method. ... Read More

How to check if String value is Boolean type in java?

Maruthi Krishna
Updated on 11-Oct-2019 07:00:42

12K+ Views

The Boolean class of the lang package provides two method namely parseBoolean() and valueOf().parseBoolean(String s) − This method accepts a String variable and returns boolean. If the given string value is "true" (irrespective of its case) this method returns true else, if it is null or, false or, any other value it returns false.valueOf(String s) − This method accepts a String value, parses it and returns an object of the Boolean class based on the given value. You can use this method instead of the constructor. If the given String value is "true" this method returns true or, it returns ... Read More

How to execute an external program like windows media player in Java?

Maruthi Krishna
Updated on 11-Oct-2019 06:56:18

1K+ Views

Using the Runtime classJava provides a class named java.lang.Runtime, using this class you can interface with the current environment.The getRunTime() (static) method of this class returns a Runtime object associated with the current application.The exec() method accepts a String value representing the command to execute a process in the current environment (system) and executes it.Therefore, to execute an external application using the Runtime class −Get the run time object using the getRuntime() method.Execute the required process by passing the path of it as a String value to the exec() method.Exampleimport java.io.IOException; public class Trail {    public static void main(String ... Read More

Advertisements