Java Articles

Page 29 of 450

Which packages contain Wrapper class in Java?

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

Java provides certain classes called wrapper classes in the java.lang package. The objects of these classes wrap primitive datatypes within them. Following is the list of primitive data types and their respective classes −Primitive datatypeWrapper classcharCharacterbyteByteshortShortintIntegerlongLongfloatFloatdoubleDoublebooleanBooleanPackageWrapper classes in Java belong to the java.lang package, Therefore there is no need to import any package explicitly while working with them.ExampleThe following Java example accepts various primitive variables from the user and creates their respective wrapper classes.import java.util.Scanner; public class WrapperClassesExample {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter an integer value: ...

Read More

How can we split a string by sentence as a delimiter in Java?

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

The split() method of the String class accepts a String value representing the delimiter and splits into an array of tokens (words), treating the string between the occurrence of two delimiters as one token.For example, if you pass single space “ ” as a delimiter to this method and try to split a String. This method considers the word between two spaces as one token and returns an array of words (between spaces) in the current String.If the String does not contain the specified delimiter this method returns an array containing the whole string as an element.Examplepublic class SplitExample { ...

Read More

How can we check if specific string occurs multiple times in another string in Java?

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

You can find whether a String contains a specified sequence of characters using any of the methods −The indexOf() method − The indexOf() method of the String class accepts a string value and finds the (starting) index of it in the current String and returns it. This method returns -1 if it doesn’t find the given string in the current one.The contains() method − The contains a () method of the String class accepts a sequence of characters value and verifies whether it exists in the current String. If found it returns true else it returns false.In addition to these, you ...

Read More

How to trim white space in StringBuffer in Java?

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

The String class of the java.lang package represents a set of characters. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings objects are immutable, once you create a String object you cannot change their values, if you try to do so instead of changing the value a new object is created with the required value and the reference shifts to the newly created one leaving the previous object unused.The StringBuffer (and StringBuilder) class is used when there is a necessity to make a lot of modifications to a String.Unlike Strings, objects of ...

Read More

How to move files using FileUtils in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 902 Views

Using the File classThe class named File of the java.io package represents a file or directory (pathnames) in the system. This class provides various methods to perform various operations on files/directories.This class provides various methods to manipulate files, The rename() method of the File class accepts a String representing a destination file and, renames the abstract file path of the current file to the given one.This method actually moves the file from the source path to the destination path.Exampleimport java.io.File; public class MovingFile {    public static void main(String args[]) {       //Creating a source file object   ...

Read More

BufferedReader class in Java.

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

The BufferedReader class of Java is used to read the stream of characters from the specified source (character-input stream). The constructor of this class accepts an InputStream object as a parameter.This class provides a method named read() and readLine() which reads and returns the character and next line from the source (respectively) and returns them.Instantiate an InputStreamReader class bypassing your InputStream object as a parameter.Then, create a BufferedReader, bypassing the above obtained InputStreamReader object as a parameter.Now, read data from the current reader as String using the readLine() or read() method.ExampleThe following Java program demonstrates how to read integer data ...

Read More

What are variable arguments in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 753 Views

While defining a method, In general, we will specify the arguments it accepts along with the type as −myMethod(int a, String b){ }Suppose if you need to accept more than one variable of the same type you need to specify the variables one after the other as −myMethod(int a, int b, int c){ }You can also pass a variable number of arguments of a particular type, to a method. These are known as variable arguments or, varargs. They are represented by three dots (…)Syntaxpublic myMethod(int ... a) {    // method body }Once you use variable arguments as a parameter ...

Read More

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 936 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
Showing 281–290 of 4,498 articles
« Prev 1 27 28 29 30 31 450 Next »
Advertisements