All Topics

More from Java - 3



Array List:-

The ArrayList class is a resizable array, which can be found in the java.util package.
The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).

While elements can be added and removed from an ArrayList whenever you want.



There are many methods to perform operations on ArrayList. Like

get() to access elements.
add() to add elements.
set() to change element.
remove() to remove element.
size() to get the length. We can perform looping on arraylists same as other arrays.


Enums:-

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).
To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma.
Note that they should be in uppercase letters


You can access enum constants with the dot syntax:
Level myVar = Level.MEDIUM;

We can add enums in classes, switch statements and can even loop through them.

Difference between Enums and Classes
An enum can, just like a class, have attributes and methods. The only difference is that enum constants are public, static and final (unchangeable - cannot be overridden).
An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).
Why And When To Use Enums?
Use enums when you have values that you know aren't going to change, like month days, days, colors, deck of cards, etc.