Constants in R Programming Language

Constatnts in Java

Java supports constants of character, string, boolean, and numeric values.

final declares a constant value.

public class ConstantsExample {

    final static String s = "constant";

    public static void main(String[] args) {
        System.out.println(s);

        final int n = 500000000;

        final double d = 3e20 / n;
        System.out.println(d);

        System.out.println((long) d);

        System.out.println(Math.sin(n));
    }
}

To run the program, compile the code into a .class file and then use java to execute it.

$ javac ConstantsExample.java
$ java ConstantsExample
constant
6.0E11
600000000000
-0.28470407323754404

In Java, the final keyword is used to declare constants. Constants in Java are typically static, and their names are usually in uppercase snake case.

In the example above:

  1. We declare a constant string s with the value "constant".
  2. Then, we declare an integer constant n with the value 500000000.
  3. Next, we declare a double constant d with the value 3e20 / n, showcasing arbitrary precision arithmetic.
  4. Lastly, we perform type conversion and use Math.sin to illustrate the usage of constants in function calls.

Now that we understand how to use constants in Java, let’s move on to learning more about the language.