Programming Articles - Page 2493 of 3366

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

How to convert an array to Set and vice versa in Java?

Maruthi Krishna
Updated on 03-Jul-2020 08:18:11

2K+ Views

Array is a container which can hold a fix number of entities, which are of the same type. Each entity of an array is known as element and, the position of each element is indicated by an integer (starting from 0) value known as index.Exampleimport java.util.Arrays; public class ArrayExample {    public static void main(String args[]) {       Number integerArray[] = new Integer[3];       integerArray[0] = 25;       integerArray[1] = 32;       integerArray[2] = 56;       System.out.println(Arrays.toString(integerArray));    } }Output[25, 32, 56]Whereas a Set object is a collection (object) stores ... Read More

Why TreeSet Does not allow null values in Java?

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

2K+ Views

TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in a sorted and ascending order.Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly.The reason is, if you look at the internal implementation of the TreeSet, it uses natural ordering, that means TreeSet uses Comparable interface by default to sort its value by comparing other value.Examplepublic class TreeSetDemo {    public static void main(String args[]) {       TreeSet treeSet = new TreeSet();       ... Read More

Is there any method to convert a Set to immutable in Java

Maruthi Krishna
Updated on 03-Jul-2020 08:20:05

2K+ Views

Whenever you need to create an object which cannot be changed after initialization you can define an immutable object. There are no specific rules to create immutable objects, the idea is to restrict the access of the fields of a class after initialization.A Set is an interface in collection framework, which does not allow duplicate values.Method to convert a set to immutableYes, Java provides a method named unmodifiableSet() in the Collections class. This method accepts a collection object as a parameter and returns an unmodifiable i.ie immutable form of it.ExampleIn the following Java program, we have created a HashSet object ... Read More

Can we add null elements to a Set in Java?

Maruthi Krishna
Updated on 07-Aug-2019 11:31:05

16K+ Views

A Set is a collection that cannot contain duplicate elements. It models the mathematical set abstraction.It does not allow duplicate elements and allow one null value at most.Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.There are three classes implementing this interface −HashSet − Set implementation based on hash table.LinkedHashSet − HashSet implementation based on linked list.TreeSet − Set implementation based on trees.Null values in a Set objectAs per the definition a set object does not allow duplicate values but it ... Read More

How to convert Date to String in Java 8?

Maruthi Krishna
Updated on 07-Aug-2019 11:27:30

626 Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.The toString() method of the LocalDate class converts the date value of the current Date object in to String and returns it.ExampleFollowing Java example accepts ... Read More

How to check Leap Year in Java 8 How to get current timestamp in Java 8?

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

970 Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.The isLeapYear() method of java.time.LocalDate verifies if the year in the current object is a leap year according to the ISO proleptic calendar system rules, returns ... Read More

Advertisements