DEV Community

Cover image for JAVA Basics #8 - Constants
Chathumi Kumarapeli
Chathumi Kumarapeli

Posted on • Updated on

JAVA Basics #8 - Constants

We are in the 8th article of this series. We are going to learn more and more facts about Java in this article as well.

Constants

A constant is a data item where its value does not change during the execution of the program. Sometimes you may want your variables to have constant values. For example if you are handling a system related to an educational institution, the fee for a particular course is a constant. So you should not allow it to be changed during the execution of the program. To do this you have to declare the variable as follows;

final int FEES = 150_000;
Enter fullscreen mode Exit fullscreen mode

'final' is a keyword in java which makes the program know that this value should not be changed. Try the following code.

final float PI = 3.14F;
PI = 4.01F;
System.out.println(PI);
Enter fullscreen mode Exit fullscreen mode

You will encounter an error saying that 'cannot assign a value to final variable PI'. So when you declare a variable as final it becomes immutable.

Usually we use Capital Letters to name constants :)

Arithmetic Expressions

You can perform arithmetic operations like additions, subtraction, multiplication, division etc. in java as well.

int number = 10 + 3;
System.out.println(number);
number = number + 10;
System.out.println(number);
Enter fullscreen mode Exit fullscreen mode

The above code will add '10' and '3' and store '13' in the variable number. After that another 10 is added to the same variable and stored in itself. Now its value is '23'. Likewise you can perform other arithmetic operations as well.

Run the below code and see the output.

double div = 10/3;
System.out.println(div);
Enter fullscreen mode Exit fullscreen mode

You may get '3.0' as the output. But you know it has to be something like 3.333... So how can you get the accurate answer?
For that you need to tell java that not only the div variable but also 10 and 3 are of type double. So how can we say it? Look at the below code.

double div = (double) 10 / (double) 3;
System.out.println(div);
Enter fullscreen mode Exit fullscreen mode

When you run above code it will print '3.3333333333333335' as the output. So what you have done is type casting an integer to a double. We will talk about casting in detail in future tutorials.

Task

Check whether the operations '(double) 10 / (double) 3' and '(double)(10/3)' give the same output. If not reason why they do not.

If you want to increment or decrement the value of a variable by '1' then there's an easy way.

int x = 251;
x++;
System.out.println(x);
x--;
System.out.println(x);
Enter fullscreen mode Exit fullscreen mode

Here first println line will print 251 and the second one will print '252' by adding 1 to 'x'. Now the value of 'x' is 252, so when you do x-- the answer is 251 because it substract 1 from '252'.

Task

Go through the following code and guess the output before running the program. Try to understand the different between your guessing and the output if there aren't the same.

package com.company;
public class Main {
    public static void main(String[] args) {
        int x = 251;
        int y = ++x;
        System.out.println(x);
        System.out.println(y);
        y = x++;
        System.out.println(y);
        System.out.println(x);
    }
}
Enter fullscreen mode Exit fullscreen mode

Order of Operators

There is a precedence level given for each and every operator. According to that level the order that we have to perform a particular operation is evaluated. This is just like in Mathematics. If we are given an expression like 'y = 3+(4+6)*2' we know that the answer is 'y = 23'. That is because order of an expression is evaluated according to the rule BODMAS in math.

The precedence and the associativity of operators in java is given by the table below.

Precedence Associativity
++ -- left to right
+ - ~ ! right to left
* / % left to right
+ - left to right
<< >> <<< left to right
< > <= >= left to right
== != left to right

Task

Guess the output of the below code before running the program and evaluate the output.

package com.company;
public class Main {
    public static void main(String[] args) {
        int x = 10 + 3 * 2;
        System.out.println(x);
        x = (10 + 3) * 2;
        System.out.println(x);
    }
}
Enter fullscreen mode Exit fullscreen mode

If you are eager to learn about casting and type conversion in java, you will find the next article useful :)

Top comments (2)

Collapse
 
the_max_empire profile image
MaxⓂ

Thank you very much.

Collapse
 
chathurashmini profile image
Chathumi Kumarapeli

My pleassure :)