Posts

Java Enum Tutorial with examples

An enum is a special type of data type which is basically a collection (set) of constants. In this tutorial we will learn how to use enums in Java and what are the possible scenarios where we can use them. This is how we define Enum public enum Directions{   EAST,   WEST,   NORTH,   SOUTH } Here we have a variable Directions of enum type , which is a collection of four constants EAST, WEST, NORTH and SOUTH. How to assign value to a enum type ? Directions dir = Directions.NORTH; The variable dir is of type Directions (that is a enum type). This variable can take any value, out of the possible four values (EAST, WEST, NORTH, SOUTH). In this case it is set to NORTH. Use of Enum types in if-else statements This is how we can use an enum variable in a if-else logic. /* You can assign any value here out of   * EAST, WEST, NORTH, SOUTH. Just for the   * sake of example, I'm assigning to NORTH   */ Directions...

Java Annotations tutorial with examples

Java Annotations allow us to add metadata information into our source code, although they are not a part of the program itself. Annotations were added to the java from JDK 5. Annotation has no direct effect on the operation of the code they annotate (i.e. it does not affect the execution of the program). In this tutorial we are going to cover following topics: Usage of annotations, how to apply annotations, what predefined annotation types are available in the Java and how to create custom annotations. What’s the use of Annotations? 1) Instructions to the compiler : There are three built-in annotations available in Java ( @Deprecated , @Override & @SuppressWarnings ) that can be used for giving certain instructions to the compiler. For example the @override annotation is used for instructing compiler that the annotated method is overriding the method. More about these built-in annotations with example is discussed in the next sections of this article. 2) ...