Types of Constants in JavaConstants play a pivotal role in programming as they allow developers to assign meaningful names to fixed values that remain unchanged throughout the execution of a program. In Java, a widely used object-oriented programming language, constants are integral for creating maintainable and readable code. This article delves into the different types of constants in Java, providing clear examples and comprehensive explanations. Types of constants involve classifying them based on their nature and usage. This categorization helps programmers better comprehend the purpose of constants and choose the appropriate ones for specific scenarios. 1. Final Variables:A final variable is a variable that, once assigned a value, cannot be changed. It's essentially a constant and is often used to represent values that shouldn't be altered during the execution of a program. In Java, you can declare a final variable using the final keyword. FinalExample.java Output: Maximum allowed value: 100 In the example above, MAX_VALUE is declared as a final variable, making it immutable once assigned. 2. Compile-Time Constants:Compile-time constants are constants that are evaluated by the compiler during the compilation process. These constants are typically used in situations where the value is known at compile-time and doesn't change during runtime. The final modifier is often applied to indicate compile-time constants. Circle.java Output: Area of the circle: 78.53981633974999 In this example, PI is a compile-time constant representing the mathematical constant pi. Since the value of pi is known and doesn't change, it's marked as a compile-time constant. 3. Enum Constants:Enums, short for enumerations, are a special data type used to define a collection of constant values. Enum constants offer more structured and type-safe alternatives to represent sets of related constants. Output: Today is: FRIDAY In this code snippet, DayOfWeek is an enum that defines constants for each day of the week. Enums enhance code readability and help prevent invalid values. 4. String Literals:String literals are sequences of characters enclosed in double quotes. While not inherently considered constants, string literals are often used as constants in Java. Output: Hello, World! Here, the MESSAGE variable holds a string literal, effectively acting as a constant greeting message. 5. Numeric Literals:Numeric literals are constant values that represent numbers. They can be integers, floating-point numbers, or scientific notation. NumericLiterals.java In this example, integerLiteral, floatingPointLiteral, and scientificNotation are all numeric literals that represent constant values. Numeric ConstantsNumeric constants in Java are crucial for performing various arithmetic operations, comparisons, and other calculations in your programs. By using integer constants, floating-point constants, and even considering character and boolean constants as numeric values in certain contexts, you can handle numerical data effectively and accurately in your Java code. Numeric constants are values that represent numbers and can be used in various mathematical operations. They can be further divided into subcategories:
Non-Numeric Constants:Non-numeric constants represent data other than numbers and have various applications in programming:
Numeric Constants1. Integer ConstantsInteger constants represent whole numbers without fractional or decimal parts. They can be written in different formats, such as decimal, octal, or hexadecimal. Decimal Integer Constants: DecimalIntegerConstantExample.java Output: Age: 25 Octal Integer Constants: OctalIntegerConstantExample.java Output: Octal number: 10 Hexadecimal Integer Constants: HexadecimalIntegerConstantExample.java Output: Hexadecimal number: 26 2. Floating-Point Constants:Floating-point constants represent numbers with fractional parts. They include a decimal point and can be written in scientific notation as well. Output: Value of PI: 3.14159 Scientific notation: 2500.0 3. Character Constants as Numeric Values:Character constants can also be interpreted as numeric values based on their Unicode code points. CharacterAsNumericExample.java Output: Numeric value of 'A': 65 4. Boolean Constants as Numeric Values:In Java, boolean constants true and false can be considered as numeric values, where true is equivalent to 1 and false is equivalent to 0. Output: Permission value: 1 Non-Numeric ConstantsNon-numeric constants in Java refer to constants that are not directly associated with numeric values. These constants are used to represent non-numeric data, such as textual information, characters, boolean values, and more. Let's explore some common types of non-numeric constants and their explanations. String Constants:String constants are used to represent sequences of characters or textual data. They are enclosed within double quotes and can include letters, numbers, symbols, and spaces. String constants are often used for displaying messages, labels, prompts, and any other form of textual information. StringConstantExample.java Output: Welcome to our application! 2. Character Constants:Character constants represent single characters and are enclosed within single quotes. They are commonly used to represent individual characters, such as letters, digits, and symbols. CharacterConstantExample.java Output: First letter: A Second letter: B 3. Boolean Constants:Boolean constants represent the two possible truth values: true and false. They are often used in conditional statements and logic operations to control the flow of a program. BooleanConstantExample.java Output: User is active. User does not have permission. 4. Enum Constants (Non-Numeric):While enums can represent numeric constants, they can also represent non-numeric constants. Enums provide a structured way to define a set of related constants with distinct names. Gender.java EnumConstantNonNumericExample.java Output: User gender: FEMALE 5. Null Constant:The null constant is a special constant that represents the absence of a value. It is often used to indicate that a reference type variable doesn't point to any object. Output: No value available. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India