about java calculation expressions

59 views Asked by At
import java.util.\*;

class Example{    
  public static void main(String asrg\[\]){    
    boolean b = false;    
    System.out.println(10\>4 && true !=b==(10+3/2==8)==true); //Line 1    
}

can anyone explain this expression?

expecting the steps how to calculate these

2

There are 2 answers

2
Z-100 On

Your code won't compile; It's missing a } at the end of the class.

Also, did you copy and past this from a website? Why are there \ all over the place?

For better readability, use the Java-style array declaration: String[] args instead of String args[].

To check a boolean value, you don't have to compare it to true or false. This includes any actual booleans, or expressions outputting a boolean, such as: 10 + 3 / 2 == 8, which is false.

0
aidanmclean On

The code provided is missing a final curly brace ('}') and will not compile. There are also some backslashes ('\') strung about that will result in syntax errors. Additionally, 'args' is misspelled but it is not necessary in order for code to compile.

With the final brace added and backslashes removed, the code is:

import java.util.*;

class Example
{    
  public static void main(String args[])
    {    
    boolean b = false;    
    System.out.println(10>4 && true !=b==(10+3/2==8)==true); //Line 1    
    }
}

Running the above code will display the answer in console. However the steps can be explained below.

First, we need to simplify our boolean expression into just terms of true and false. Since we declare that b = false, and the fact that10 > 4 == true and (10+3/2==8) == false, the expression that is printed can be simplified to (true && true != false == false == true).

The expression can then be evaluated according to Java's operator precedence, and will evaluate to false.