Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Basic Java

Constant

In Java, constants are variables whose values should not be altered once they are assigned. Constants are declared using the final keyword, which indicates that the value of the variable cannot be changed after initialization. Constants are often used for values that should remain fixed throughout the program, such as mathematical constants, configuration settings, or predefined limits.

Declaring Constants:

Syntax:

final data_type CONSTANT_NAME = value;

Example:

public class ConstantsExample {
    final double PI = 3.14159;
    final int MAX_SIZE = 100;

    public static void main(String[] args) {
        ConstantsExample example = new ConstantsExample();
        System.out.println("PI Value: " + example.PI);
        System.out.println("Maximum Size: " + example.MAX_SIZE);
    }
}

Explanation:

In this example, PI and MAX_SIZE are declared as constants. Once assigned a value, these variables cannot be reassigned to another value. They are effectively read-only and can be accessed throughout the class. Constants are often named using uppercase letters with underscores separating words, following the convention to distinguish them from regular variables.

Benefits of Constants:

  1. Readability: Constants improve code readability by providing meaningful names for fixed values, making it clear what the values represent.

  2. Maintainability: Constants make it easier to update and maintain code. If a constant value needs to change, you only need to update it in one place (where it's declared) rather than searching for all occurrences throughout the code.

  3. Prevents Modification: Constants prevent accidental or intentional modification of important values, ensuring that critical parameters remain constant during the program's execution.

Constants in Interfaces:

In Java, interfaces can also contain constants. All variables declared in an interface are implicitly public, static, and final. Therefore, any variable defined in an interface is a constant and must be initialized with a value.

Interface with Constants:

public interface Constants {
    double PI = 3.14159;
    int MAX_SIZE = 100;
}

In the above example, PI and MAX_SIZE are constants defined in the Constants interface. Implementing classes can use these constants without the need to redeclare them.

Using constants in Java helps improve code quality and maintainability by providing a clear and standardized way to define and use fixed values.

PreviousData TypeNextDate and Format

Last updated 1 year ago