Javascript Enum Tostring
__main__ Python module |
array Python module |
code Python module |
COM PHP module |
enum Python module |
Ev PHP module |
exp |
FFI PHP module |
iat |
imp Python module |
io Python module |
JavaScript |
keyword Python module |
Lua PHP module |
os Python module |
Python functions |
re Python module |
select Python module |
sep |
StackOverflow |
stat Python module |
string Python module |
time Python module |
UI PHP module
Michael Zippo
04.11.2021
👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
A Java enumeration is a data type that stores a list of constants. You can create an ENUM object using the enum keyword. Enum constants appear in the form of a separate list by commas in a set of braces.
An enum, which is the abbreviation for enumeration, is a data type that has a fixed set of possible values.
Enumerations are useful if you are working with a value that should only contain a specific value contained in a list of values. For example, an ENUM would be used if you want to store a list of the sizes of coffee sold in a store.
This tutorial will cover the basics of enumeration in Java. We will refer to some examples of an ENUM class in a Java program to help you get started.
Java Enum Syntax
A Java ENUM represents a list of constants. A variable assigned by an enumeration can have only one value that appears in the enumeration. Enumerations help developers store data that they know will not change.
Suppose you decide that a Java variables store employee compensation degrees can only have one of five values. Or, decide that a variable that store employee contracts can only store part-time, full-time, or zero hours. In these cases, you need to use an ENUM to store the data.
enumerations are declared using "ENUM" kind. The syntax of "ENUM " word - key:
Let’s analyze this syntax:
- ENUM indicates our program that we want to declare an enumeration .
- name is the name of our ENUM.
- VALUE1, VALUE2, VALUE3 are the set of constant values ​​that our stores enum. These values ​​are usually written in upper case.
Why use Enum in Java?
Using ENUM allows you to express an algorithm in a way that is more readable both for you and for the computer.
Writing an ENUM tells the computer that a variable cannot have a specific number of values. It also tells you, the programmer, that this is the case, which will make your code easier to understand. If you see a variable that uses an ENUM, you know that the variable can only have one of a limited number of values ​​
In addition, the enumerations allow you to use constants more efficiently. in fact, ENUM was introduced to replace the int constants in Java, which spanned multiple lines and were difficult to read. Here is an example of an old int constant in Java:
Our code takes five lines to declare these constants. But by using an ENUM, we can reduce our code to three lines. In the following example, we declare a list of enum constants:
Our code takes five lines to declare these constants. But by using an ENUM, we can reduce our code to three lines. In the following example, we declare a list of enum constants:
AirplaneClasses class {Economy, FIRST_CLASS}
This code is simpler and easier to read.
Declaring Java Enum
We are creating an application that waiters can use to send coffee to baristas in a coffee shop.
When a barista enters a value for the size of the drink, we want there to be only three possible options. These options are: small, regular, and LARGE. We could use an ENUM to limit the possible sizes of a drink to these options:
In this example, we have declared an ENUM called Sizes which has three possible values. Now that we have declared an ENUM, we can refer to its values ​​in our code.
Java Enum Example
We write a program that prints the size of the coffee a customer has ordered from the console. This value will be read by the bartender preparing the customer’s drink
We could use the following code to print the size of the coffee a customer ordered on the console:
Our code returns:.
This cafe must be small
First, we declare an ENUM called Sizes. Power have three values: SMALL, REGULAR or LARGE. Next, we declared a class called PrintSize. This class accepts the customer’s drink size and prints the glass size to the console.
In our main program, we declare an object called order173, ‚" which uses the PrintSize class. We went through our code Java parameter Sizes.SMALL. This tells our program to assign the value SMALL to the coffeeSize variable in the PrintSize class.
Next, we use order173.placeOrder () to run the code in the switch case statement in the PrintSize class. This evaluates the variable value "coffeeSize" against three cases. A message is printed on the console based on the size of the coffee a customer has ordered.
We indicated that the customer ordered a small coffee. Our code impressions "This coffee should be weak." » on the console.
If you are looking to learn more about Java switch case statements, you can read them in our tutorial on the switch statement Java < / a>.
Java Enum Methods
The Java Enum class has a number of predefined methods used to extract and manipulate values using the ENUM class. We have broken five of the most commonly used enum methods below
compareTo ()
compareTo () compares constants in a lexicographic ENUM and returns the difference between their ordinal values. Here is an example of compareTo () used with an ENUM value from our previous example:
Our code returns the difference between the SMALL
and GRAND
tags according to their value In this case , our code returns:
-2
toString ()
toString () converts the name of an ENUM to a string Here is an example of toString () used to convert the ENUM LARGE
value to a string:.
Our code returns:
"LARGE"
name ()
the name () method returns the name used to define a constant in an enum class. Here is an example of the name of the method () used to return the defined name of the regular coffee size:
Our code returns:
"regular"
Values ​​()
The values of the process yields (a) Java array that stores all the constants in an enum. Here’s an example of the values ‚Äã‚Äã() method in action:
Sizes [] = sizeList Sizes.value s ();
valueOf ()
valueOf () accepts a string and returns the constant ENUM, which has the same name. So if we wanted to retrieve the constant enum with the name REGULAR,
we could do it using this code:
Our code returns:
REGULAR
Conclusion
A ENUM, short for enumeration, is a Java data type that has a fixed set of values.
Enumerations are useful if you are working with variables that should only be able to store one in a selected range of values.
After practicing what you have read in this tutorial, you will be an expert at using enumerations in Java. To learn more about coding in Java, see our complete guide How to code Java .
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Javascript Enum Tostring, check other __main__ Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
Davies Richtgofen
Tallinn | 2023-03-21
Thanks for explaining! I was stuck with Javascript Enum Tostring for some hours, finally got it done 🤗. Will use it in my bachelor thesis
Angelo Nickolson
Massachussetts | 2023-03-21
Simply put and clear. Thank you for sharing. Javascript Enum Tostring and other issues with enum Python module was always my weak point 😁. I am just not quite sure it is the best method
Chen Porretti
Prague | 2023-03-21
I was preparing for my coding interview, thanks for clarifying this - Javascript Enum Tostring in Python is not the simplest one. Will use it in my bachelor thesis
Shop
Learn programming in R: courses
$FREE
Best Python online courses for 2022
$FREE
Best laptop for Fortnite
$399+
Best laptop for Excel
$
Best laptop for Solidworks
$399+
Best laptop for Roblox
$399+
Best computer for crypto mining
$499+
Best laptop for Sims 4
$
Latest questions
PythonStackOverflow
Common xlabel/ylabel for matplotlib subplots
1947 answers
PythonStackOverflow
Check if one list is a subset of another in Python
1173 answers
PythonStackOverflow
How to specify multiple return types using type-hints
1002 answers
PythonStackOverflow
Printing words vertically in Python
909 answers
PythonStackOverflow
Python Extract words from a given string
798 answers
PythonStackOverflow
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
606 answers
PythonStackOverflow
Python os.path.join () method
384 answers
PythonStackOverflow
Flake8: Ignore specific warning for entire file
360 answers
Wiki
Python | How to copy data from one Excel sheet to another
Common xlabel/ylabel for matplotlib subplots
Check if one list is a subset of another in Python
How to specify multiple return types using type-hints
Printing words vertically in Python
Python Extract words from a given string
Cyclic redundancy check in Python
Finding mean, median, mode in Python without libraries
Python add suffix / add prefix to strings in a list
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
Python - Move item to the end of the list
Python - Print list vertically