Enums , Keywords & Comments (Part 3)
1. Java Enum
A Java Enum is a special Java type used to define collections of constants. More precisely, a Java enum type is a special kind of Java class. An enum can contain constants, methods etc. Java enums were added in Java 5.
Let us look at a simple code that will explain more clearly about Enum.
Eg:-
class Animal {
enum AnimalSize{ SMALL, MEDIUM, LARGE } //declaring enum
AnimalSize size;
}
public class AnimalTest {
public static void main(String args[]) {
//creating an object of type Animal
Animal animal = new Animal();
animal.size = Animal.AnimalSize.MEDIUM ; //logic
System.out.println("Size: " + animal.size);
}
}
Output:-
Size : Medium
2. Java Keywords
Java keywords are also known as reserved words. Keywords are particular words which acts as a key to a code. These are predefined words by Java so it cannot be used as a variable or object name.
3. Comments in Java
Any statements noted between " / / " or " /* */ " would be ignored by the Java compiler i.e would not be executed.
Eg:-
public class MyClass {
/* This is my first java program.
* This will print 'Hello World' as the output
* This is an example of multi-line comments.
*/
public static void main(String []args) {
// This is an example of single line comment
System.out.println("Hello World");
}
}
Output:-
Hello World