Java Articles

Page 30 of 450

Difference between notify() and notifyAll() in Java

Nitin Sharma
Nitin Sharma
Updated on 11-Mar-2026 7K+ Views

Both notify and notifyAll are the methods of thread class and used to provide notification for the thread.But there are some significant differences between both of these methods which we would discuss below.Following are the important differences between notify and notifyAll.Sr. No.KeynotifynotifyAll1NotificationIn case of multiThreading notify() method sends the notification to only one thread among the multiple waiting threads which are waiting for lock.While notifyAll() methods in the same context sends the notification to all waiting threads instead of single one thread.2Thread identificationAs in case of notify the notification is sent to single thread among the multiple waiting threads so ...

Read More

How to sort HashSet in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ Views

To sort HashSet in Java, you can use another class, which is TreeSet.Following is the code to sort HashSet in Java −Exampleimport java.util.*; public class Main {    public static void main(String args[]) {       Set hashSet = new HashSet();       hashSet.add("green");       hashSet.add("blue");       hashSet.add("red");       hashSet.add("cyan");       hashSet.add("orange");       hashSet.add("green");       System.out.println("HashSet elements"+ hashSet);       Set treeSet = new TreeSet(hashSet);       System.out.println("Sorted elements"+ treeSet);    } }OutputHashSet elements [red, orange, green, blue, cyan] Sorted elements [blue, cyan, green, ...

Read More

Differences between wait() and join() methods in Java

Nitin Sharma
Nitin Sharma
Updated on 11-Mar-2026 3K+ Views

In multithreading when we deal with threads there comes the requirement of pause and start a thread for this Threading provides two methods wait and join which are used for the same.The following are the important differences between wait() and join().Sr. No.Keywait()join()1Declarationwait() method is defined in Object class and hence the wait() method is declared in java.lang package.join() method, on the other hand, is also defined in java.lang package but in Thread class.2Usagewait() method is primarily used for the inter-thread communication.On the other hand join() is used for adding sequencing between multiple threads, one thread starts execution after first thread ...

Read More

Check if a string contains only alphabets in Java using Regex

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

At first, convert the string into character array. Here, name is our string −char[] ch = name.toCharArray();Now, loop through and find whether the string contains only alphabets or not. Here, we are checking for not equal to a letter for every character in the string −for (char c : ch) {    if(!Character.isLetter(c)) {       return false;    }Following is an example to check if a string contains only alphabets using RegexExamplepublic class Main {    public static boolean checkAlphabet(String name) {       char[] ch = name.toCharArray();       for (char c : ch) {   ...

Read More

Compare two Strings in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 447 Views

Compare two strings using compareTo() method in Java. The syntax is as follows −int compareTo(Object o)Here, o is the object to be compared.The return value is 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.ExampleLet us now see an example −public class Demo {    public static void main(String args[]) {       String str1 = "Strings are immutable";       String ...

Read More

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 383 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.Exampleimport 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

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 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

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 511 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.Exampleimport 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 do we initialize an array within object parameters in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 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.public 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

Is it possible to check if a String only contains ASCII in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 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.Exampleimport 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
Showing 291–300 of 4,498 articles
« Prev 1 28 29 30 31 32 450 Next »
Advertisements