1. int a;
  2. int b;
  3. int c;
  4. a = 2;
  5. b = 5;
  6. c = a * b;
  7. c = c * c;
  8. System.out.println("a = "+a+" b = "+b+" c = "+c);

The code above will output -

a = 2 b = 5 c = 100

Line 4 and line 5 are easy to understand. They are storing the values 2 and 5 into two varaibles, a and b. Line 6 and line 7 are using the variables in order to do further calcualtions. For line 6 a will have the value of 2 and b will have the value of 5. 2*5 = 10. Line 7 will then square this number. Why? Well c has the value of 10. 10 * 10 = 100 so the new value of c becomes 100.

Remember that with assignment values are copied from the right to the left. As such the right hand side is evaluated first and then stored in what ever varaible is on the left.

a*b = c

The above line of code is invalid as the expression (calculation) must be on the right hand side.

next defining variables >>

 
Basics
Menu
Search