Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java Fundamentals

You're reading from   Java Fundamentals A fast-paced and pragmatic introduction to one of the world's most popular programming languages

Arrow left icon
Product type Paperback
Published in Mar 2019
Publisher
ISBN-13 9781789801736
Length 408 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (5):
Arrow left icon
Miles Obare Miles Obare
Author Profile Icon Miles Obare
Miles Obare
Basheer Ahamed Fazal Basheer Ahamed Fazal
Author Profile Icon Basheer Ahamed Fazal
Basheer Ahamed Fazal
Rogério Theodoro de Brito Rogério Theodoro de Brito
Author Profile Icon Rogério Theodoro de Brito
Rogério Theodoro de Brito
Gazihan Alankus Gazihan Alankus
Author Profile Icon Gazihan Alankus
Gazihan Alankus
Vinicius Isola Vinicius Isola
Author Profile Icon Vinicius Isola
Vinicius Isola
+1 more Show less
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Java Fundamentals
Preface
1. Introduction to Java FREE CHAPTER 2. Variables, Data Types, and Operators 3. Control Flow 4. Object-Oriented Programming 5. OOP in Depth 6. Data Structures, Arrays, and Strings 7. The Java Collections Framework and Generics 8. Advanced Data Structures in Java 9. Exception Handling Appendix

Enumerations


Enumeration in Java (or enum) is a special type in Java whose fields consist of constants. It is used to impose compile-time safety.

For example, consider the days of the week, they are a set of fixed constants, therefore we can have an enum defined:

public enum DayofWeek { 

 SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY  

} 

Now we can simply check if a variable that stores a day is part of the declared enum. We can also declare enums for non-universal constants, such as:

public enum Jobs { 

  DEVELOPER, TESTER, TEAM LEAD, PROJECT MANAGER 

}

This will enforce the job-type to be the constants declared in the Jobs enum. Here's an example enum holding currencies:

public enum Currency {
    USD, INR, DIRHAM, DINAR, RIYAL, ASD 
}

Exercise 33: Using Enum to Store Directions

We will create an enum and will find values and compare enums.

  1. Create a class EnumExample and in the main method. Get and print the enum using the value as enum. Get and print enum using the values...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image