Java Series Part 1: Java Expressions

A Java expression consists of variables, operators, literals, and method calls.

package com.java.basic;

public class ExpressionsExample {

	public static void main(String args[]) {

		// 5 + 2 is an expression
		int add = 5 + 2;
		System.out.println("Addition= " + add);

		// 5 - 2 is an expression
		int sub = 5 - 2;
		System.out.println("Substraction= " + sub);

		// 5 * 2 is an expression
		int mul = 5 * 2;
		System.out.println("Multiplication= " + mul);

		// 5 / 2 is an expression, this will print 2 and not 2.5 as its int return type
		int div = 5 / 2;
		System.out.println("Division= " + div);

		// double division example, below will print 2.5
		double d = 5.0 / 2.0;
		System.out.println("double return type decimal example= " + d);

		// 5%2 is an expression, prints remainder
		int mod = 5 % 2;
		System.out.println("Modulo= " + mod);

		// complex expression, will follow PRECEDENCE, first mul then sub
		int complexExp = 5 - (2 * 4);
		System.out.println("Complex exp ressult= " + complexExp);

		// to calculate number of mins in a day
		int numberOfMins = 24 * 60;
		System.out.println("Number of mins in a day are= " + numberOfMins);

		// to calculate number of seconds in a day
		int numberOfSec = 24 * 60 * 60;
		System.out.println("Number of seconds in a day are= " + numberOfSec);

	}
}

Expressions are essential building blocks of any Java program, usually created to produce a new value, although sometimes an expression assigns a value to a variable. Expressions are built using values, variables, operators and method calls.

Code at Github: https://github.com/Sarthakprof/SarthakJavaLearning/blob/master/src/com/java/basic/ExpressionsExample.java

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s