Public class main public static void main(String args short x 100 x = x 5 System out print x)

In order to do arithmetic in Java, one must first declare at least one variable. Typically one declares a variable and assigns it a value before any arithmetic is done. Here's an example of declaring an integer variable:

Public class main public static void main(String args short x 100 x = x 5 System out print x)
Code section 3.59: Variable assignation.

After creating a variable, one can manipulate its value by using Java's operators: + (addition), - (subtraction), * (multiplication), / (integer division), % (modulo or remainder), ++ (pre- & postincrement by one), -- (pre- & postdecrement by one).

The division operator rounds towards zero: 5/2 is 2, and -5/2 is -2. The remainder operator has the same sign as the left operand; it is defined such that ((a/b)*b) + (a%b) is always equal to a. The preincrement, predecrement, postincrement, and postdecrement operators are special: they also change the value of the variable, by adding or subtracting one. The only difference is that preincrement/decrement returns the new value of the variable; postincrement returns the original value of the variable.

Test your knowledge

Question 3.8: Consider the following code:

Public class main public static void main(String args short x 100 x = x 5 System out print x)
Question 3.8: Question8.java

public class Question8 { public static void main(String[] args) { int x = 10; x = x + 10; x = 2 * x; x = x - 19; x = x / 3; System.out.println(x); } }

What will be printed in the standard output?

Answer

Public class main public static void main(String args short x 100 x = x 5 System out print x)
Output for Question 3.87

int x = 10; => 10
x = x + 10; => 20
x = 2 * x; => 40
x = x - 19; => 21
x = x / 3; => 7

When using several operators in the same expression, one must consider Java's order of precedence. Java uses the standard PEMDAS (Parenthesis, Exponents, Multiplication and Division, Addition and Subtraction) order. When there are multiple instances of the same precedence, Java reads from left to right. Consider what the output of the following code would be:

The following chart shows how Java would compute this expression:

Figure 3.1: Computation of an arithmetic expression in the Java programming language

Public class main public static void main(String args short x 100 x = x 5 System out print x)


Besides performing mathematical functions, there are also operators to assign numbers to variables (each example again uses the variable initialized as x = 5):

Using bitwise operators within Java[edit | edit source]

Java has besides arithmetic operators a set of bit operators to manipulate the bits in a number, and a set of logical operators. The bitwise logical operators are

Operator Function Value of
x before
Example
input
Example
output
Value of
x after
& Bitwise AND 7 x&27 3 7
| Bitwise OR 7 x|27 31 7
^ Bitwise XOR 7 x^27 28 7
~ Bitwise inversion 7 ~x -8 7

Besides these logical bitwise functions, there are also operators to assign numbers to variables (x = -5):

Operator Function Example
input
Example output
&= Assign x bitwisely ANDed with another value to itself x &= 3 3
|= Assign x bitwisely ORed with another value to itself x |= 3 -5
^= Assign x bitwisely XORed with another value to itself x ^= 3 -8
<<= Assign x divided by another integer to itself x <<= 1 -10
>>= Assign x bitwisely negated with another value to itself x >>= 1 -3
>>>= Assign x bitwisely negated with another value to itself x >>>= 1 2,305,843,009,213,693,949 (64 bit)

The shift operators are used to shift the bits to the left or right, which is also a quick way to multiply/divide by two:

Operator Function Value of
x before
Example
input
Example output Value of
x after
<< Logical shift left -15 x << 2 -60 -15
>> Arithmetic shift right -15 x >> 3 -2 -15
>>> Logical shift right -15 x >>> 3 2,305,843,009,213,693,937 (64 bit) -15

What is public static void main String args?

Java :public static void main(String[] args) The main() method is a special method in Java Programming that serves as the externally exposed entrance point by which a Java program can be run. To compile a Java program, you doesn't really need a main() method in your program.

What is public static in public static void main () called?

Public- it is access specifier from anywhere we can access it Static- it is access modifier we can call the methods directly by class name without creating its objects Void- it is the return type Main- it is a method name String[]args- in java we accept only the string type of argument and store it.

What is public class and public static void main?

The keyword public static void main is the means by which you create a main method within the Java application. It's the core method of the program and calls all others. It can't return values and accepts parameters for complex command-line processing.

What are the String [] args for in the main method?

5. String[] args. It stores Java command-line arguments and is an array of type java.