Found 2619 Articles for Java

How to print a formatted text using printf() method in Java?

raja
Updated on 01-Jul-2020 08:12:21

424 Views

The printf() method allows us to format output to a java.io.PrintStream or java.io.PrintWriter. These classes also contain a method called format() which can produce the same results, so whatever we read here for the printf() method can also be applied to the format() method.SyntaxSystem.out.printf(“format-string” [, arg1, arg2, … ]);Example1import java.io.PrintStream; public class PrintfTest1 {    public static void main(String[] args) {       int i = 1234;       System.out.printf("Decimal: %1$, d Octal: %1$o Hex: %1$x", i);       String str = "Tutorials Point";       System.out.printf("%15s", str);    } }OutputDecimal: 1, 234 Octal: 2322 Hex: ... Read More

How to find the number of days in a month of a particular year in Java?

raja
Updated on 23-Nov-2023 10:08:36

982 Views

A GregorianCalendar is a concrete subclass of Calendar class and it provides the standard calendar system used by most of the world. In Java, this GregorianCalendar can handle both the Gregorian calendar as well as Julian calendar. We can determine or find the number of days in a month of a particular year by using the getActualMaximum() method of GregorianCalendar class. This method returns the maximum value that the GregorianCalendar field can have. The parameter can be any field of a Calendar class. Syntax public int getActualMaximum(int field) Example import java.util.*; public class NoOfDaysInAMonthOfAYearTest { public static void main(String []args) { ... Read More

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

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

207 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

2K+ 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

42K+ 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

373 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

453 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

961 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

1K+ 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

Advertisements