Learnitweb

Category: Java tutorial

  • Predefined annotation types in Java

    The predefined annotation types defined in java.lang are @Deprecated, @Override, @SuppressWarnings, @SafeVarargs and @FunctionalInterface. @Deprecated @Deprecated annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the @Deprecated annotation. When an element is deprecated, it should…

  • Custom annotation in Java

    In the following example, we are creating an annotation TestAnnotationForClass. Now, we’ll use TestAnnotationForClass annotation for Test class. In this example, we have used RetentionPolicy.RUNTIME because we want to demonstrate and read annotation at runtime. RetentionPolicy.RUNTIME indicates that annotations are to be recorded in the class file by the compiler and retained by the virtual…

  • Annotations in Java – an introduction

    Annotations were introduced in Java 5. Annotation is also know as metadata. Metadata is a set of data that describes and gives information about other data. Annotations are only metadata and do not(and should not) contain any business logic. For example: In this code, @Override is the annotation. When this annotation is used with the…

  • Packages in Java

    There are millions (or more) Java classes containing code. Java library itself has thousands of classes. Consider a case when a programmer writes a class having class name HashMap. Java library itself has HashMap class. If programmer uses HashMap class in code, then the question is which HashMap class to use out of the two.…

  • Java 8 Optional

    1. Introduction Those who are coding in Java know that null reference which results in NullPointerException is a big problem. Optional class was introduced in Java SE 8. This class is available in java.util package. Optional is a container object which may or may not contain a non-null value. This class has a method isPresent()…

  • Java 8 Predicate with example

    In mathematics, a predicate is a Boolean-valued function P: X→ {true, false}, called a predicate on X. In Java, we can say that Predicate represents a boolean-valued function of one argument. Predicate has a functional method test(Object). Interface Predicate<T> is a predefined functional interface available in package java.util.function. Predicate’s usage Predicate can be used to…

  • main method inside interface

    Yes, you can defined main method inside interface in Java 8. Prior to Java 8, static methods were not allowed in interface. In Java 8, you can define static method inside interface. main method is just another static method, so you can define main method inside interface. Since an interface is saved as .java file…

  • static methods in interface

    In Java 8, you can define static methods in interfaces. The use of static methods is to write helper methods. A static method is associated with the class in which it is defined rather than the object of that class. Java 8 has introduced static methods in interfaces. Now you can define static methods specific…

  • Difference between interface with default method and abstract class or Java 8 interface vs abstract class

    After introducing default method in Java 8, interfaces and abstract class seems to look same. However, they are actually different even in Java 8. The purpose of these two is still different. The main purpose of introducing default method is to introduce new features but maintaining backward compatibility. If you wish to add a method…

  • default methods in interface in case of multiple inheritance

    When a class implements two interfaces, each having default methods with same signature, it results in compile time error. To understand this with an example, we have created two interfaces: Left and Right. Class Test implements these two interface. On compilation, above code will result in following error: The reason for above error is that…