
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is a constant and how to define constants in Java?
A constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants.
A constant can make our program more easily read and understood by others. In addition, a constant is cached by the JVM as well as our application, so using a constant can improve performance.
To define a variable as a constant, we just need to add the keyword “final” in front of the variable declaration.
Syntax
final float pi = 3.14f;
The above statement declares the float variable “pi” as a constant with a value of 3.14f. We cannot change the value of "pi" at any point in time in the program. Later if we try to do that by using a statement like “pi=5.25f”, Java will throw errors at compile time itself. It is not mandatory that we need to assign values of constants during initialization itself.
In the below example, we can define the primitive data type (byte, short, int, long, float, double, boolean and char) variables as constants by just adding the keyword “final” when we declare the variable.
Example
public class ConstantsDemo { public static void main(String args[]) { final byte var1 = 2; final byte var2; var2 = -3; final short var3 = 32; final short var4; var4 = -22; final int var5 = 100; final int var6; var6 = -112; final long var7 = 20000; final long var8; var8 = -11223; final float var9 = 21.23f; final float var10; var10 = -121.23f; final double var11 = 20000.3223; final double var12; var12 = -11223.222; final boolean var13 = true; final boolean var14; var14 = false; final char var15 = 'e'; final char var16; var16 = 't'; // Displaying values of all variables System.out.println("value of var1 : "+var1); System.out.println("value of var2 : "+var2); System.out.println("value of var3 : "+var3); System.out.println("value of var4 : "+var4); System.out.println("value of var5 : "+var5); System.out.println("value of var6 : "+var6); System.out.println("value of var7 : "+var7); System.out.println("value of var8 : "+var8); System.out.println("value of var9 : "+var9); System.out.println("value of var10 : "+var10); System.out.println("value of var11 : "+var11); System.out.println("value of var12 : "+var12); System.out.println("value of var13 : "+var13); System.out.println("value of var14 : "+var14); System.out.println("value of var15 : "+var15); System.out.println("value of var16 : "+var16); } }
Output
value of var1 : 2 value of var2 : -3 value of var3 : 32 value of var4 : -22 value of var5 : 100 value of var6 : -112 value of var7 : 20000 value of var8 : -11223 value of var9 : 21.23 value of var10 : -121.23 value of var11 : 20000.3223 value of var12 : -11223.222 value of var13 : true value of var14 : false value of var15 : e value of var16 : t
- Related Articles
- How to define constants in C++?
- How to define integer constants in JavaScript?
- How to define character constants in C#?
- How do I define string constants in C++?
- Can a final keyword alone be used to define a constant in Java?
- How to implement constants in java?
- What is string constant pool in Java?
- What are constants in Kotlin and how to create them?
- How to declare, define and call a method in Java?
- What is the difference between constants and variables?
- Difference between constants and final variables in Java?
- What is Productivity? How to Define and Measure It?
- How do I write constants names in Java?
- How to create Variables and Constants in C++?
- What is a constant acceleration and variable acceleration?
