Constants in Java
Constants in Java
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:
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:
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:
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!
5. Numeric Literals:
Numeric literals are constant values that represent numbers. They can be
integers, floating-point numbers, or scientific notation.
NumericLiterals.java
Non-Numeric Constants:
Non-numeric constants represent data other than numbers and have
various applications in programming:
DecimalIntegerConstantExample.java
Output:
Age: 25
OctalIntegerConstantExample.java
Output:
Octal number: 10
HexadecimalIntegerConstantExample.java
1. public class HexadecimalIntegerConstantExample {
2. public static final int HEX_NUMBER = 0x1A; // Hexadecimal rep
resentation
3. public static void main(String[] args) {
4. System.out.println("Hexadecimal number: " + HEX_NUMBER); //
Output: "Hexadecimal number: 26"
5. }
6. }
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:
CharacterAsNumericExample.java
Output:
Output:
Permission value: 1
Non-Numeric Constants
Non-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:
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.
Gender.java
EnumConstantNonNumericExample.java
Output:
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.