Found 2616 Articles for Java

How can we Implement a Stack using Queue in Java?

raja
Updated on 11-Feb-2020 05:21:33

979 Views

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.A Queue class extends Collection interface and it supports the insert and removes operations using a first-in-first-out (FIFO). We can also implement a Stack using Queue in the below program.Exampleimport java.util.*; public class StackFromQueueTest {    Queue queue = new LinkedList();    public void push(int value) {       int queueSize = queue.size();       queue.add(value);       for (int i = 0; i < queueSize; ... Read More

How many ways to make an object eligible for GC in Java?

raja
Updated on 23-Nov-2023 09:07:55

307 Views

The process of destroying unreferenced objects is called a Garbage Collection(GC). Once an object is unreferenced it is considered as an unused object, hence JVM automatically destroys that object. There are various ways to make an object eligible for GC. By nullifying a reference to an object We can set all the available object references to "null" once the purpose of creating an object is served. Example public class GCTest1 { public static void main(String [] args){ String str = "Welcome to TutorialsPoint"; // String object referenced by variable str and it is not eligible for GC yet. ... Read More

Why the transient variable is not serialized in Java?

raja
Updated on 11-Feb-2020 05:25:59

654 Views

The Serialization is a process to persists java objects in the form of a sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object. The Serialization is the translation of Java object’s values/states to bytes to send it over the network or to save it. On the other hand, the Deserialization is the conversion of byte code to corresponding java objects.The Transient variable is a variable whose value is not serialized during the serialization process. We will get a default value for this variable when we ... Read More

Importance of StringReader class in Java?

raja
Updated on 23-Nov-2023 09:13:26

190 Views

The StringReader class is s subclass of a Reader class and it can be used to read the character stream in the form of a string which acts as a source to StringReader. The StringReader class overrides all the methods from a Reader class. The important methods of StringReader class are skip(), close(), mark(), markSupported(), reset() and etc. Syntax Public class StringReader extends Reader Example import java.io.StringReader; import java.io.IOException; public class StringReaderTest { public static void main(String[] args) { String str = "Welcome to Tutorials Point"; StringReader strReader = new StringReader(str); ... Read More

When to use fillInStackTrace() method in Java?

raja
Updated on 23-Nov-2023 09:28:44

1K+ Views

The fillInStackTrace() is an important method of Throwable class in Java. The stack trace can be useful to determine where exactly the exception is thrown. There may be some situations where we need to rethrow an exception and find out where it is rethrown, we can use the fillInStackTrace() method in such scenarios. Syntax public Throwable fillInStackTrace() Example public class FillInStackTraceTest { public static void method1() throws Exception { throw new Exception("This is thrown from method1()"); } public static void method2() throws Throwable { try { ... Read More

Can I import same package twice? Will JVM load the package twice at runtime?

Maruthi Krishna
Updated on 23-Nov-2023 10:04:58

1K+ Views

In Java classes and interfaces related to each other are grouped under a package. Package is nothing but a directory storing classes and interfaces of a particular concept. For example, all the classes and interfaces related to input and output operations are stored in java.io package. Creating a Package You can group required classes and interfaces under one package just by declaring the package at the top of the Class/Interface (file) using the keyword package as − Example public class Sample{ public void demo(){ System.out.println("This is a method of the sample class"); } ... Read More

What is the importance of a StringWriter in Java?

raja
Updated on 22-Nov-2023 12:03:35

112 Views

The StringWriter class is s subclass of Writer class and it writes the String to an output stream. To write a string, this character stream collects the string into a string buffer and then constructed a string. The buffer of StringWriter automatically grows according to data. The important methods of StringWriter class are write(), append(), getBuffer(), flush() and close(). Syntax public class StringWriter extends Writer Example import java.io.*; public class StringWriterTest { public static void main(String args[]) { String str = "Welcome to Tutorials Point"; try { ... Read More

How to make a singleton enum in Java?

raja
Updated on 22-Nov-2023 12:08:04

2K+ Views

The singleton pattern restricts the instantiation of a class to one object. INSTANCE is a public static final field that represents the enum instance. We can get the instance of the class with the EnumSingleton.INSTANCE but it is better to encapsulate it in a getter in case if we want to change the implementation. There are a few reasons why we can use an enum as a singleton in Java. Guaranteed one instance (Cannot instantiate more than one enum even through reflection). Thread-safe. Serialization. Syntax public enum Singleton { INSTANCE; private singleton() { } } Example public enum EnumSingleton { ... Read More

How can we implement a timer thread in Java?

raja
Updated on 22-Nov-2023 12:13:21

3K+ Views

The Timer class schedules a task to run at a given time once or repeatedly. It can also run in the background as a daemon thread. To associate Timer with a daemon thread, there is a constructor with a boolean value. The Timer schedules a task with fixed delay as well as a fixed rate. In a fixed delay, if any execution is delayed by System GC, the other execution will also be delayed and every execution is delayed corresponding to previous execution. In a fixed rate, if any execution is delayed by System GC then 2-3 execution happens consecutively to ... Read More

How can we read from standard input in Java?

raja
Updated on 22-Oct-2023 13:24:29

28K+ Views

The standard input(stdin) can be represented by System.in in Java. The System.in is an instance of the InputStream class. It means that all its methods work on bytes, not Strings. To read any data from a keyboard, we can use either a Reader class or Scanner class.Example1import java.io.*; public class ReadDataFromInput {    public static void main (String[] args) {       int firstNum, secondNum, result;       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));       try {          System.out.println("Enter a first number:");          firstNum = Integer.parseInt(br.readLine());         ... Read More

Advertisements