Basic Arithmetic in Java

You can do basic arithmetic in Java but before we get to that point, we need to learn about variables in Java. Check the following code –

 
class basic {
	public static void main (String args[]) {
		int a = 3;
		int b = 2;
		System.out.println(a+b);
	}
}

Ignore public static void main part, that’s something which you need to include in every program and we’ll get to that sometime later in the future. For now, just know that you need to have it in your program to run it. Everything that matters is contained within the main block. And the program execution begins from the main block!

So we initiate variable a as integer with value 3 and we do the same for b with value 2. Then using a print command we print the sum of a and b using an arithmetic operator (a+b).

Similarly we can subtract, multiply, divide numbers in the program –

class basic {
	public static void main (String args[]) {
		int a = 3;
		int b = 2;
		System.out.println(a+b);
		System.out.println(a-b);
		System.out.println(a*b);
		System.out.println(a/b);
	}
}

In the next post, I’ll share a bit about decision making statements and conditional operators in Java!