What is a Constant in Java?
A constant in Java is used to map an exact and unchanging value to a variable name.
Constants are used in programming to make code a bit more robust and human readable. Here’s an example:
Imagine you are creating a program that needs to calculate areas and volumes of different shapes, it could look something like this, but this is an example of WHAT NOT TO DO:
- public class AreasAndVolumes
- {
- public double volumnOfSphere (double radius)
- {
- return (4/3) * Math.pow(3.14159 * radius, 3);
- }
- public double volumeOfCylinder (double radius, double height)
- {
- return Math.pow(radius * 3.14159, 2) * height;
- }
- public double areaOfCircle (double radius)
- {
- return Math.pow(radius * 3.14159, 2);
- }
- }
So, this above code will get the job done, but there’s something that can be improved. Can you guess how?
We should be using a constant! Look how many times we use the
double
value 3.14159
, this value represents pi (π). We should create a constant that will assign the value of π to a variable name. Here’s how we do it with some new code:- public class AreasAndVolumes
- {
- // here we've declared a new variable called PI and assigned
- // the value of 3.14159 to this new variable.
- private static final double PI = 3.14159;
- public double volumnOfSphere (double radius)
- {
- return (4/3) * Math.pow(PI * radius, 3);
- }
- public double volumeOfCylinder (double radius, double height)
- {
- return Math.pow(radius * PI, 2) * height;
- }
- public double areaOfCircle (double radius)
- {
- return Math.pow(radius * PI, 2);
- }
- }
So now, we have a variable named
PI
declared in the instance variable declaration space. We’ve done this because we need to use this new constant value throughout our AreasAndVolumes
class. This constant will function just like any other instance variable with one main exception… we’ve made the value final
.
Various Constant Types
Primitive Data Types |
byte |
short |
int |
long |
float |
double |
boolean |
char |
You can define any of these type variables as constants by just adding the keyword “final” when we declare the variable.
Eg1: final int distance = 30;
Eg2: final float pi = 3.14f;
Sample Program
Let’s see a program where we define constants belonging to all the above mentioned data types and display those values.
Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
| /* * File Name : ConstantsDemo.java */ public class ConstantsDemo { public static void main(String args[]) { /*Defining two byte type constant variables var1 and var2 * with initial values as 2 and -3 */ final byte var1 = 2 ; final byte var2; var2 = - 3 ; /*Defining two short type constant variables var3 and var4 * with initial values as 32 and -22 */ final short var3 = 32 ; final short var4; var4 = - 22 ; /*Defining two byte type constant variables var5 and var6 * with initial values as 100 and -112 */ final int var5 = 100 ; final int var6; var6 = - 112 ; /*Defining two long type constant variables var7 and var8 * with initial values as 20000 and -11223 */ final long var7 = 20000 ; final long var8; var8 = - 11223 ; /*Defining two float type constant variables var9 and var10 * with initial values as 21.23 and -121.23 * * Note the charcter 'f' which tells the compiler that this is a * float value. */ final float var9 = 21 .23f; final float var10; var10 = - 121 .23f; /*Defining two double type constant variables var11 and var12 * with initial values as 20000.3223 and -11223.222 */ final double var11 = 20000.3223 ; final double var12; var12 = - 11223.222 ; /*Defining two boolean type constant variables var13 and var14 * with initial values as 20000 and -11223 */ final boolean var13 = true ; final boolean var14; var14 = false ; /*Defining a charater type constant variable var15 * with initial values e. * * Note that the value e is given in single quotes */ final char var15 = 'e' ; final char var16; var16 = 'e' ; //Now 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
No comments:
Post a Comment