Java-Force Precedence

Force Precedence

Parentheses( ) may be used to force precedence, if necessary. For example :

int totel=5*(5+15)/2;

Generally multiplication and division operation has a higher precedence than the addition operation. But, addition operation executing first. Because, addition operation covered by parentheses. Parentheses may be used to force precedence, if necessary.

Program

final

Program Source

public class Javaapp {
    
    public static void main(String[] args){
        
        int totel1 = 5*6+16/2;
        int totel2 = 5*(6+16)/2;
        System.out.println("totel1 = "+totel1);
        System.out.println("totel2 = "+totel2);
   }
}

Leave a Comment