Learnitweb

Author: Editorial Team

  • Java program to check whether an alphabet is vowel or consonant

    The alphabets A, E, I, O and U (small case and upper case) are known as vowels and rest of the alphabets are known as consonants. Method 1: Program to check if character is vowel or consonant using switch case Output Method 2: Program to check if character is vowel or consonant using using if..else…

  • Difference between HashTable and HashMap in Java

    Both HashMap and HashTable map keys to values. HashTable was introduced in JDK 1.0. As of JDK 1.2, this class was retrofitted to implement the Map interface. Following are the differences between HashTable and HashMap: HashMap HashTable HashMap extends java.util.AbstractMap HashTable extends java.util.Dictionary HashMap allows only one null key and any number of null values.…

  • Random number generation in Java

    Random numbers can be generated in Java using methods listed below: Using java.util.Random class This class can be used to create random numbers. Instances of java.util.Random are threadsafe. It provides several methods to generate random integer, long, double etc. This class provides two constructors: Random() Creates a new random number generator. Random(long seed) Creates a…

  • Java 8 Lambdas – an introduction

    To understand lambdas introduced in Java 8, let us first see how things were implemented before Java 8 and then we’ll see how lambda expressions make our life easy. We believe everyone has heard about Swing in Java. Even if you haven’t heard, you must be having some idea about a button and adding some…

  • Java enum ordinal() method

    The ordinal() method of Enum class returns the ordinal of enum constant. It represents the sequence in the enum declaration, where the initial constant is assigned an ordinal of 0. It is very much like array indexes. The ordinal() method returns the order of an enum instance. Following is the syntax: public final int ordinal()…

  • Iterating over Enum Values in Java

    An enum type enables for a variable to be a set of predefined constants. To demonstrate iterating over enum values let us first declare one: enum provides values() method which returns an array. We can then iterate this array of enum values like any other array. Iterate using for-each loop A shorter format of the…

  • Comparing enum members in Java

    You can use both == and equals() method to compare Enum. They will give the same result because equals() method of Java.lang.Enum internally uses == to compare enum in Java. Since every Enum in Java extends java.lang.Enum and equals() method is declared final, there is no way to override equals method in user defined enum.…

  • enum in Java – constructor, methods and interface

    enum with constructor Java enum can have a constructor to pass data while creating enum constants. For example, in case of seven days of a week, we can pass 1 to Sunday, 2 to Monday and so on. You can also provide one or more constructor to your enum as enum supports constructor overloading. Constructor…

  • enum in Java

    1. Introduction An enum type enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. You should use enum type when you need to represent a fixed set of constants and you know all possible values are compile…

  • Difference between Externalization and Serialization

    Difference between Serialization and Externalization can be described with the help of following points: Serialization Externalizable Serializable is a marker interface. Externalizable is not a marker interface. Externalizable interface contains two methods writeExternal() and readExternal(). Serializable is for default serialization. The purpose of Externalizable is for customized serialization. In case of serialization, the responsibility of…