Java Articles

Page 31 of 450

Can we throw an Unchecked Exception from a static block in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.Examplepublic class MyClass {    static{       System.out.println("Hello this is a static block");    }    public static void main(String args[]){       System.out.println("This is main method");    } }OutputHello this is a static block This is main methodExceptions in static blockJust like any other method in Java when an exception occurs in static block you can handle it using try-catch pair.Exampleimport ...

Read More

Difference between a Static Queue and a Singly Linked List in Java.

Nitin Sharma
Nitin Sharma
Updated on 11-Mar-2026 953 Views

In Java List and Queue both are introduced as an ordered list of objects, where the same object may be added more than once. The difference between both comes in the manner of adding elements. In the queue, all the elements get inserted at the rear and removed from the front while we can add an element anywhere in the list.Sr. No.KeyStatic QueueSingly Linked List1Data initialization.Static Queue works in first out(FIFO) fashion as all the elements get inserted at the REAR and removed from the FRONT of the queue.In the case of Singly Linked List, one can add elements anywhere ...

Read More

Difference between an Iterator and ListIterator in Java

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

Java provided these two interfaces to traverse the data one by one stored in a collection. The internal implementation of iterator and list iterator makes them differ apart but the main agenda of both the iterators is the same.The following are the important differences between Iterator and ListIterator.Sr. No.KeyIteratorListIterator1ApplicableIterator can be used to traverse any collection irrespective of the type of collection.List iterator can only be used to iterate only List collection implemented classes like arraylist, linkedlist etc.2CallingAs mentioned Iterator must be used to enumerate elements in all Collections implemented interfaces like Set, List, Queue, Deque and also in all ...

Read More

Difference between Definition and Declaration in Java.

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

For the difference between definition and declaration, one should consider their literal meaning first which includes Declare means to announce or proclaim while Define means to describe some entity.The following are the important differences between the Definition and the Declaration.Sr. No.KeyDeclarationDefinition1ConceptThe concept of declaration includes informing the compiler about properties of the variable such as its name, type of value it holds and the initial value if any it takes.While the definition is basically the actual implementation and memory location of function and about memory for the variable is allocated during the definition of the variable.2Exception in CBoth declaration and ...

Read More

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 467 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 396 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
Showing 301–310 of 4,498 articles
« Prev 1 29 30 31 32 33 450 Next »
Advertisements