Found 9150 Articles for Object Oriented Programming

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

Vivek Verma
Updated on 29-May-2025 18:58:51

2K+ Views

The given task is to find the number of days in a month of a particular year. To understand this, consider that if we can determine the maximum day value in a month, we can use that as the number of days in that month. To do this, we can use the GregorianCalendar class. It is a concrete subclass of the 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. Following are the different ways to ... Read More

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

Vivek Verma
Updated on 29-May-2025 17:38:52

399 Views

In Java, both deepToString() and asList() methods are static methods of the Arrays class. An array is an object that holds a fixed number of values of a similar type in a contiguous memory location. The deepToString() Method In Java, the deepToString() method is used to convert a multi-dimensional array into a string. It checks if any element in the array is also an array; it will convert that inner array to a string as well. Syntax Following is the syntax of the Arrays.deepToString() method: public static String deepToString(Object[] a) Here, a: An array ... Read More

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

Vivek Verma
Updated on 27-May-2025 16:16:05

4K+ Views

This article will use an anonymous class to create a Thread in Java. An Anonymous class is a class that does not have a name. Thread in Java In Java, a Thread is a part of a program that can be executed independently. All Java programs have at least one thread, known as the main thread, which is created by the Java Virtual Machine (JVM), when the main() method is invoked. Creating a Thread by Using Anonymous Class In Java, the basic way to create a thread is to either extend the Thread class or implement the Runnable interface. However, we ... Read More

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

Vivek Verma
Updated on 28-Aug-2025 17:06:02

20K+ 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 the following approaches: Using replaceAll() Method Using Pattern and Matcher Using replaceAll() Method We can remove the HTML tags from a given string by using the replaceAll() method. The replaceAll() method accepts two parameters: a "regular expression" and a "replacement string". It replaces all substrings that match the ... Read More

How to find If a given String contains only letters in Java?

Maruthi Krishna
Updated on 07-Aug-2019 12:13:04

2K+ Views

To verify whether a given String contains only characters −Read the String.Convert all the characters in the given String to lower case using the toLower() method.Convert it into a character array using the toCharArray() method of the String class.Find whether every character in the array is in between a and z, if not, return false.ExampleFollowing Java program accepts a String from the user and displays whether it is valid or not.import java.util.Scanner; public class StringValidation{    public boolean validtaeString(String str) {       str = str.toLowerCase();       char[] charArray = str.toCharArray();       for (int i ... Read More

While chaining, can we throw unchecked exception from a checked exception in java?

Maruthi Krishna
Updated on 03-Jul-2020 08:32:08

772 Views

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects).While re-throwing exceptions you can throw the same exception as it is with out adjusting it as −try {    int result = (arr[a])/(arr[b]);    System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); }catch(ArithmeticException e) {    throw e; }Or, wrap it within a new exception and throw it. When you wrap a cached exception within another exception and throw it, it is known as exception chaining or, exception wrapping, by doing this you can adjust your exception, throwing higher ... Read More

How to check if an URL is valid or not using Java?

Maruthi Krishna
Updated on 09-Sep-2019 07:30:14

3K+ Views

The URL class of the java.net package represents a Uniform Resource Locator which is used to point a resource(file or, directory or a reference) in the worldwide web.This class provides various constructors one of them accepts a String parameter and constructs an object of the URL class. While passing URL to this method if you used an unknown protocol or haven’t specified any protocol this method throws a MalformedURLException.Similarly, the toURI() method of this class returns an URI object of the current URL. If the current URL is not properly formatted or, syntactically incorrect according to RFC 2396 this method throws ... Read More

Comparing Strings with (possible) null values in java?

Maruthi Krishna
Updated on 07-Aug-2019 11:59:25

6K+ Views

Strings in Java represents an array of characters. They are represented by the String class.Using compareTo() methodThe compareTo() method of the String class two Strings (char by char) it also accepts null values. This method returns an integer representing the result, if the value of the obtained integer is −0: Given two Strings are equal or, null.1 or less: The current String preceeds the argument.1 or more: The current String succeeds the argument.Exampleimport java.util.Scanner; public class CompringStrings {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter your first string ... Read More

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

Vivek Verma
Updated on 29-May-2025 17:05:50

54K+ 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 Using ASCII value comparison Using isDigit() Method To check whether a given character is a number or not, Java provides a method called isDigit(). This method is a static method of the Character class and determines whether ... Read More

What is the difference between Enumeration interface and enum in Java?

Maruthi Krishna
Updated on 07-Aug-2019 12:03:00

2K+ Views

Enum in Java is a datatype which stores a set of constant values. You can use these to store fixed values such as days in a week, months in a year etc.You can define an enum using the keyword enum followed by the name of the enumeration as −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Just like arrays, the elements/constants in an enum are identified using numbers starting from 0 in the above example the days are identified using numbers as shown in the following illustration −Retrieving values from an enumYou can retrieve all the elements ... Read More

Advertisements