Exploring Java : My Learning Adventure, Part - 3

Exploring Java : My Learning Adventure, Part - 3

Operators and expressions

Welcome back. I hope you have gone through the second part of my blog series. If not, here's the link https://lnkd.in/eu9f_JS8

Operators and Expression

Arithmetic Operators

Addition, subtraction, division, multiplications - they are the basic arithmetic operators. Except Boolean, arithmetic operations are applicable for all the data types.

Also, about mod (modulus) i will show you how it works. I mean, x = 10%3 and y=10/3. What is x and y?

Mod gives the remainder of a division and divide operator gives quotient of a division. Mod can be used on decimals data type too.

Also, never put decimals on the quotient.

Arithmetic Expression

Operators have a specific order of precedence. First highest precedence is Division. If you write a + b / 2 then the division will take place first, then the addition with will happen. % ,/,* has equal higher precedence, then + and -. And if you want to change the order of precedence then put them into parathesis. On the other hand, if you find division, multiplication and mod - all of them present then you continue doing the calculation from left to right associatively. For example, a+b/2*c, here b gets divided with 2 then gets multiplied by c. a*2/b%5 , here a gets multiplied with 2, then it gets divided by b and then finally mod with 5 takes place, understood?

Now it's time for some interesting things. If you add byte with byte, it will not result with a byte type value. It will be an integer value. byte+byte, byte+short, short+short, short+int, byte+int, int+ int , char + int, char + short they all are going to be converted into an int value. Next, int + float will be float. The addition will convert int into float, then it will add them to give a float type result. long + float follow the same. Lastly, float + double and long + double gives double type result

class Arithmetic
{
    public static void main(String[] args)
    {
        float a = 12.34f;
        long b = 1222223L ; 
        float c = a*b ;
        System.out.println("sum " + c)
        System.out.println(10/(2*%));
    }
}

Homework

  1. Write a code that will calculate the area of a triangle.

    Formula: 1/2*base*height.

    Formula : s = 1/2(a+b+c), area = √s(s-a)(s-b)(s-c)

Increment and Decrement Operators

We have some very important operators in Java : post increment, post decrement, pre increment and pre decrement operators. It simply means increasing or decreasing by adding 1.

Post increment : first assign the value of x to y then increase the value of y by 1.

Pre increment : first increase by one then assign the value of x to y.

Here, the byte type will remain same because here you are not adding 1 to the byte type, you are increasing the byte. Modifying a data type does not change its type. It's applicable for the other data types too.

That's it for today and be sure to try them out.

In the next blog, i will talk about Strings. In the meantime, don't forget to stay hydrated! Happy learning!

Reminder : Don't forget about your homework. I will attach the answer in the next blog.