Found 7442 Articles for Java

Importance of isDaemon() method in Java?

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

563 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?

Vivek Verma
Updated on 28-Aug-2025 16:13:17

653 Views

No, we cannot define a package after the import statement in Java. The compiler will throw an error if we try 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. Important! If you use the package statement in an online compiler, you may still get an error even if it is defined correctly. This is because many online compilers do not support custom package structures. Example In the following example, ... Read More

What changes has been introduced in JDK7 related to Exception handling in Java?

Maruthi Krishna
Updated on 07-Aug-2019 09:17:57

102 Views

Since Java 7 try-with resources was introduced. In this we declare one or more resources in the try block and these will be closed automatically after the use. (at the end of the try block)The resources we declare in the try block should extend the java.lang.AutoCloseable class.ExampleFollowing program demonstrates the try-with-resources in Java.import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopying {    public static void main(String[] args) {       try(FileInputStream inS = new FileInputStream(new File("E:\Test\sample.txt"));       FileOutputStream outS = new FileOutputStream(new File("E:\Test\duplicate.txt"))){          byte[] buffer = new byte[1024];     ... Read More

Are the instances of Exception checked or unchecked exceptions in java?

Maruthi Krishna
Updated on 07-Aug-2019 09:16:03

597 Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.There are two types of exceptions in Java.Unchecked Exception − An unchecked exception is the one which occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.Checked Exception − A checked exception is an exception that occurs at ... Read More

Why we can't initialize static final variable in try/catch block in java?

Maruthi Krishna
Updated on 07-Aug-2019 09:11:01

1K+ Views

In Java you can declare three types of variables namely, instance variables, static variables and, local variables.Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.Class (static) variables − Class variables are variables declared within a class, outside any method, with the static keyword.Static methods in try-blockIn the same way, static variables belong to the class and can be accessed anywhere within the class, which contradicts with the definition of the local variable. Therefore, declaring ... Read More

How can we decide that custom exception should be checked or unchecked in java?

Maruthi Krishna
Updated on 03-Jul-2020 08:02:42

6K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.User defined exceptionsYou can create your own exceptions in Java and they are known as user defined exceptions or custom exceptions.To create a user defined exception extend one of the above mentioned classes. To display the message override the toString() method or, call the superclass parameterized constructor by passing the message in String format.MyException(String msg){    super(msg); } Or, public String toString(){    return ... Read More

What is a Type-safe Enum in Java?

raja
Updated on 28-Aug-2025 14:47:43

2K+ Views

In Java, an enum is a special class that represents a "group of constants" that cannot be changed, just like a "final variables". Java provides an enum keyword to create an enum, and inside the class, the constants are written in uppercase letters and separated by commas (, ). Syntax to create an enum in Java: enum EnumName { CONST1, CONST2, CONST3 // ... } Here, enum: A predefined keyword used to define an enum. EnumName: The name ... Read More

How can we Implement a Queue using Stack in Java?

Vivek Verma
Updated on 22-Jul-2025 11:44:33

2K+ Views

This article will discuss how to implement a Queue using a Stack in Java. It will briefly introduce the Queues and Stacks, and provide a suitable example that shows their implementation. Queues in Java In Java, the Queue class extends the Collection interface, and it supports the insert and remove operations using a FIFO (First-in-First-Out). The last element added to the end of the queue can become the first element to be removed from the queue. The following diagram will give you a better understanding of the Queue: Stacks in Java In Java, a Stack is a subclass (child class) ... Read More

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

Vivek Verma
Updated on 28-Aug-2025 16:19:10

425 Views

Converting a character array to a reader in Java means treating the array as a stream of characters to be read sequentially. Let's quickly learn about character array and its creation in Java: In Java, a character array is an array that holds elements of only char type, and it is also used to store a sequence of characters. Following is the syntax to define a character array in Java: char char_array_name[] = {'char1', 'char2', 'char3'....} or char char_array_name[] = new char[size]; Here, char_array_name: The name of the character array. ... Read More

Can a method throw java.lang.Exception without declaring it in java?

Maruthi Krishna
Updated on 06-Aug-2019 11:39:12

231 Views

No, for that matter to throw any exception explicitly you need to create an object of that exception and throw it using the throw keyword.Without creating an object you cannot throw an exception explicitly, you might create a scenario that causes the respective exception.ExampleFollowing Java program throws a NullPointerExceptionpublic class ExceptionExample {    public static void main(String[] args) {       System.out.println("Hello");       NullPointerException nullPointer = new NullPointerException();       throw nullPointer;    } }OutputHello Exception in thread "main" java.lang.NullPointerException    at MyPackage.ExceptionExample.main(ExceptionExample.java:6)

Advertisements