Named constants:
During the execution of a program, the value of a variable can be altered, whereas a named constant, or simply a constant, represents data that remains unchanged permanently. In our "ComputeArea" program, the constant "PI" retains its value consistently. To avoid repeatedly typing the value "3.14159" for "PI," it is possible to declare a constant for it. The following syntax is used to declare a constant:
final datatype CONSTANTNAME = value;
A constant must be declared and initialized in the same statement. The keyword "
final" in Java is used to declare a constant. For example, "PI" can be declared as a constant.
The code for the "ComputeAreaWithConstant"
- import java.util.Scanner;
- public class ComputeAreaWithConstant {
- public static void main(String[] args) {
- final double PI = 3.14159; // Declare a constant
- // Create a Scanner object
- Scanner input = new Scanner(System.in);
- // Prompt the user to enter a radius
- System.out.print("Enter a number for radius: ");
- double radius = input.nextDouble();
- // Compute area
- double area = radius * radius * PI;
- // Display result
- System.out.println("The area for the circle of radius " +
- radius + " is " + area);
- }
- }
There are three advantages of utilizing constants: Firstly, if a value is used multiple times, you can avoid repetitive typing. Secondly, if you need to change the value of a constant (e.g., changing the value of "PI" from 3.14 to 3.14159), you only need to modify it in a single location in the source code. Thirdly, using a descriptive name for a constant enhances the readability of the program.
Naming conventions:
Adhering to Java's naming conventions is crucial for ensuring readability and minimizing errors in your programs.
To enhance clarity, it is recommended to choose descriptive names with straightforward meanings for variables, constants, classes, and methods in your program. Remember that names in Java are case-sensitive. The following conventions outline the naming guidelines for variables, methods, and classes:
- - Variables and methods should be written in lowercase. If a name consists of multiple words, they should be combined into a single word, with the first word in lowercase and the first letter of each subsequent word capitalized. For instance, variables like "radius" and "area," and methods like "print" should follow this convention.
- - Class names should capitalize the first letter of each word. For example, class names like "ComputeArea" and "System" should have their initial letters capitalized for each word.
- - Constants should have every letter capitalized, and words should be separated by underscores. For example, constants like "PI" and "MAX_VALUE" should follow this convention.
By adhering to these naming conventions, you ensure that your programs are easily readable and maintainable. Do not choose class names that are already used in the Java library. For example, since
the System class is defined in Java, you should not name your class System
Comments
Post a Comment