Found 2616 Articles for Java

Importance of deepToString() and asList() methods in Java?

raja
Updated on 23-Nov-2023 10:13:54

225 Views

An array is an object that holds a fixed number of values of a single type in a contiguous memory location. Both deepToString() and asList() methods are static methods of Arrays class. The deepToString() method converts multi-dimensional array to string and it checks if an array has the element as an array then it converts that array in the string format. The asList() creates a list with a fixed size, means that we cannot add an element by add() method in the returned list by Arrays.asList(). The asList() method acts as a bridge between an array and a list because the ... Read More

How to create a thread by using anonymous class in Java?

raja
Updated on 23-Nov-2023 10:17:47

3K+ Views

A Thread is a functionality which can be executed simultaneously with the other part of the program. All Java programs have at least one thread, known as the main thread, which is created by the Java Virtual Machine (JVM) at the program start when the main() method is invoked with the main thread. In Java, we can create a thread by extending a Thread class or by implementing the Runnable interface. We can also create a thread by using the anonymous class without extending a Thread class in the below program. Example public class AnonymousThreadTest { public static void main(String[] args) ... Read More

How to remove the HTML tags from a given string in Java?

raja
Updated on 01-Jul-2020 07:57:01

18K+ Views

A String is a final class in Java and it is immutable, it means that we cannot change the object itself, but we can change the reference to the object. The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.Syntaxpublic String replaceAll(String regex, String replacement)Examplepublic class RemoveHTMLTagsTest {    public static void main(String[] args) {       String str = "Welcome to Tutorials ... Read More

How to check if a given character is a number/letter in Java?

raja
Updated on 23-Nov-2023 10:24:03

44K+ Views

The Character class is a subclass of Object class and it wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char. We can check whether the given character in a string is a number/letter by using isDigit() method of Character class. The isDigit() method is a static method and determines if the specified character is a digit. Example public class CharacterIsNumberOrDigitTest { public static void main(String[] args) { String str = "Tutorials123"; for(int i=0; i < str.length(); ... Read More

Importance of isDaemon() method in Java?

raja
Updated on 23-Nov-2023 10:29:10

402 Views

A daemon thread is a low-priority thread in java which runs in the background and mostly created by JVM for performing background tasks like Garbage Collection(GC). If no user thread is running then JVM can exit even if daemon threads are running. The only purpose of a daemon thread is to serve user threads. The isDaemon() method can be used to determine the thread is daemon thread or not. Syntax Public boolean isDaemon() Example class SampleThread implements Runnable { public void run() { if(Thread.currentThread().isDaemon()) System.out.println(Thread.currentThread().getName()+" is daemon thread"); ... Read More

Can we define a package after the import statement in Java?

raja
Updated on 03-Jul-2020 08:09:50

469 Views

No, we cannot define a package after the import statement in Java. The compiler will throw an error if we are trying to insert a package after the import statement. A package is a group of similar types of classes, interfaces, and sub-packages. To create a class inside a package, declare the package name in the first statement in our program.Exampleimport java.lang.*; package test; public class PackageAfterImportTest {    public static void main(String args[]) {       System.out.println("Welcome to Tutorials Point !!!");    } }OutputPackageAfterImportTest.java:3: error: class, interface, or enum expected package test; ^ 1 error

What is a Type-safe Enum in Java?

raja
Updated on 23-Nov-2023 10:34:48

1K+ Views

The enums are type-safe means that an enum has its own namespace, we can’t assign any other value other than specified in enum constants. Typesafe enums are introduced in Java 1.5 Version. Additionally, an enum is a reference type, which means that it behaves more like a class or an interface. As a programmer, we can create methods and variables inside the enum declaration. Example 1 import java.util.*; enum JobType { permanent, contract } public class EnumTest1 { public static void main(String []args) { print(JobType.values()); } public static void print(JobType[] list) ... Read More

How can we Implement a Queue using Stack in Java?

raja
Updated on 23-Nov-2023 11:53:18

2K+ Views

A Queue class extends Collection interface and it supports the insert and removes operations using a first-in-first-out (FIFO). A Stack is a subclass of Vector class and it represents last-in-first-out (LIFO) stack of objects. The last element added at the top of the stack (In) can be the first element to be removed (Out) from the stack. We can also implement a Queue using Stack in the below program. Example import java.util.*; public class QueueUsingStackTest { private Stack stack1 = new Stack(); private Stack stack2 = new Stack(); public void enqueue(int element) { ... Read More

How can we convert character array to a Reader in Java?

raja
Updated on 11-Feb-2020 05:55:23

276 Views

The CharArrayReader is a subclass of Reader class and it can implement a character buffer that can be used as a character input stream. The CharArrayReader reads characters from a character array either completely or partially starting from an offset. The important methods of a CharArrayReader class are close(), mark(), read(), skip() and reset().Syntaxpublic class CharArrayReader extends ReaderExampleimport java.io.*; public class CharArrayReaderTest {    public static void main(String args[]) throws Exception {       char array[] = { 'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', ' ', 'P', 'o', 'i', 'n', 't', '!'};    CharArrayReader car = new CharArrayReader(array);   ... Read More

Importance of the parseBoolean() method in Java?

raja
Updated on 01-Jul-2020 06:13:23

112 Views

The parseBoolean() method is an important method of a Boolean class. The parseBoolean() is a static method and can parse the String method argument into a Boolean object. The parseBoolean() method of Boolean class returns the boolean represented by the string argument.Syntaxpublic static boolean parseBoolean(String s)Exampleimport java.util.Scanner; public class ParseBooleanMethodTest {    public static void main(String[] args) {       System.out.print("Are you ready to play cricket(true/false)?");       Scanner scanner = new Scanner(System.in);       String str = scanner.nextLine();       scanner.close();       // Convert the user input into boolean       boolean answer ... Read More

Advertisements