Computer Science, asked by manpreetanand722, 9 months ago

In java the constant are also called ____________

Answers

Answered by ItarSvaran
7

Explanation:

What is Constant in Java?

Constants in Java are used when a ‘static‘ value or a permanent value for a variable has to be implemented. Java doesn’t directly support constants. To make any variable a constant, we must use ‘static’ and ‘final’ modifiers in the following manner:

Syntax to assign a constant value in java:

static final datatype identifier_name = constant;

The static modifier causes the variable to be available without an instance of it’s defining class being loaded

The final modifier makes the variable unchangeable

The reason that we have to use both static and final modifiers is that if we declare a variable ‘var’ only as static, all the objects of the same class will be able to access this ‘var’ and change its values. When we declare a variable only as final, then multiple instances of the same constant value will be created for every different object and that isn’t efficient/desirable. When we use both static and final, then, the ‘var’ remains static and can be initialized only once, thereby making it a proper constant which has a common memory location for all objects of it’s containing class.

Example for a constant declaration: static final int MIN_AGE = 18; Let’s say we need to determine who all are eligible to get a permanent driving license in a group of people. We already know that the minimum age requirement for a permanent driving license is 18. So instead of asking the user to enter the minimum age for comparison, we declare the ‘MIN_AGE’ identifier as a constant integer with value 18.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

import java.util.*;

public class DrivingLicense{

public static void main(String [] args){

Scanner sc = new Scanner(System.in);

static final int MIN_AGE = 18; //Minimum age requirement

int[] list = new int[5];

System.out.println("Enter the age of people:");

for(int i=0;i<5;i++){

list[i] = sc.nextInt();

}

System.out.println("Result for eligibility:");

for(int i=0;i<5;i++) {

if(list[i] >= MIN_AGE)

System.out.println(i + " is Eligible");

else

System.out.println(i + " is Not Eligible");

}

}

}

Output:

image1- Constant in Java- Edureka

Now let us see why Constants.

Why constants?

Constants make your program more easy to read and understand when read by others.

Using a constant also improves performance, as constants are cached by both the JVM and your application.

Let us check the Static and Final Modifiers.

Answered by Debom
28

Answer:

Literals is the correct answer

The above fell9w has written too long no need of this nonscence one.

so like my answer.

Thank you.

Similar questions