Java.math - SPLessons

Java.math Enumeration

Home > Lesson > Chapter 5
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.math Enumeration

Java.math Enumeration

shape Description

An Java.math Enumeration is a feature and data type that will have a fixed number of values. For example Number of days in a week, this enumeration in Java was started in JDK 1.5 and it is one of the excellent feature of Java among generics, autoboxing, unboxing. Actually, Enumeration was not available Java, originally it was in C and C++ languages. Java.math Enumeration provides better type safety, it will have methods and constructors. Java Enum has the ability to implement more interfaces, but it cannot extend class why because it internally uses the enum class. The java.math.RoundingMode list determines an adjusting conduct for numerical sorts equipped for disposing of precision. Every running mode demonstrates how the slightest huge returned digit of an adjusted result is to be figured. Following is the declaration of the java.math.RoundingMode class. [java] public enum RoundingMode extends Enum<RoundingMode> [/java]

Constants Of java.math.RoundingMode Enumeration

shape Constants

Following are the constants of java.math.RoundingMode Enumeration.
Constants Description
CEILING To round towards positive infinity.
DOWN To round towards positive zero.
FLOOR To round towards negtive infinity.
UP To round away from zero.

Methods Of java.math.RoundingMode Enumeration

shape Methods

Following are the methods of java.math.RoundingMode Enumeration.
Methods Description
static RoundingMode valueOf(int rm) gives back the object of RoundingMode relating to a legacy whole number running mode consistent in BigDecimal.
static RoundingMode[ ] values() To give back an array containing the constants of this enum sort.
static RoundingMode valueOf(String name) To return the string.

shape Example

Following is an example. DemoRoundingMode.java [java] package com.splessons; import java.math.*; public class DemoRoundingMode { // create 2 RoundingMode objects RoundingMode rm1, rm2; // create and assign values to rm and name int rm = 7; String name = "UP"; // static methods are called using enum name // assign the the enum constant of rm to rm1 rm1 = RoundingMode.valueOf(rm); // assign the the enum constant of name to rm2 rm2 = RoundingMode.valueOf(name); String str1 = "Enum constant for integer " + rm + " is " +rm1; String str2 = "Enum constant for string " + name + " is " +rm2; // print rm1, rm2 values System.out.println( str1 ); System.out.println( str2 ); String str3 = "Enum constants of RoundingMode in order are :"; System.out.println( str3 ); // print the array of enum constatnts using for loop for (RoundingMode c : RoundingMode.values()) System.out.println(c); } } [/java] Following is the functionality of methods. Output: Now compile the code result will be as follows. [java] Enum constant for integer 7 is HALF_DOWN Enum constant for string UP is UP Enum constants of RoundingMode in order are : UP DOWN CEILING FLOOR HALF_UP HALF_DOWN HALF_EVEN UNNECESSARY [/java]

Summary

shape Key Points

  • Java.math Enumeration - Every running mode demonstrates how the minimum huge returned digit of an adjusted result is to be computed.
  • Java.math Enumeration - Enum can be traversed, enum is freely used in Switch case.