Learnitweb

Category: Java tutorial

  • Java Functional Interface

    An interface with only single abstract method is called functional interface. A functional interface can contain default and static methods which do have an implementation in addition to single unimplemented method. Functional interface acts as a type for lambda expression and can be used to invoke lambda expression There are some predefined Functional interfaces in…

  • 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…

  • Externalization in Java

    Externalizable interface extends java.io.Serializable interface. Externalizable interface is used to provide the implementing class complete control over the format and contents of the stream for an object and its supertypes. Externalization in Java is used for customized serialization. If you want to control the process of reading and writing the objects during serialization and de-serialization…