Java Operator


One of the most basic uses of a computer is to perform mathematical operations. As a programming language, Java also provides a lot of operators. The main operators include the following:


Addition, subtraction, multiply, divide, and fetch operators

Addition, subtraction and multiplication

Plus (+), multiplication (-), multiplication (*) These three operators are used in the same way as the general usage of mathematics (the correct result can not exceed the range of data types).

public class OperatorBasis
{
	public static void main(String args[])
	{
		int a = 25;
		int b = 5;
		int sum = a + b;
		int difference = a - b;
		int product = a * b;
		System.out.print(
			a + "+" + b + "=" + sum + "\n" +
			a + "-" + b + "=" + difference + "\n" +
			a + "*" + b + "=" + product + "\n"
		);
	}
}

result :

25+5=30
25-5=20
25*5=125

Using this procedure, we have succeeded in calculating the sum of 25 and 5, and we have a lot of time, and we can assign a and b to a larger number Of the data, such as a = 299, b = 5449

This procedure also illustrates some of the other issues ...

  1. In Java, the "+" operator has another usage - stitching strings , and when you use this operator, you do not need to manually convert numeric types to strings .
  2. Java in a statement can be divided into multiple lines (but can not be a string in multiple lines of writing), because of this, Java at the end of each statement must have a semicolon.
  3. Since the string in Java can not contain the word "wrap", we need to express it in a different way. This is the escape character , say "\ n" for line feed, "\" "means double quotation marks (otherwise only A double quotation mark will be interpreted as the end of the string), "\\" means the backslash itself ...

Division and fetch operators

The division of the (/) operator in Java is not necessarily the same as its mathematical meaning. Read the following procedure, try to guess the results of the operation, and then try to compile and run it ...

public class DivisionOperator
{
	public static void main(String args[])
	{
		int a, b, quotient;
		a = 25;
		b = 5;
		quotient = a / b;
		System.out.println(a + "/" + b + "=" + quotient);
		a = 5;
		b = 3;
		quotient = a / b;
		System.out.println(a + "/" + b + "=" + quotient);
	}
}

result :

25/5=5
5/3=1

If you have not learned any computer language before, the result may surprise you - 25 divided by 5 equals 5, no doubt, but 5/3 = 1 is how is it?

In Java, if both operands are integers, the result of the divide operator is an integer, and the value after all decimal places (or a quotient of two integers) is omitted. And the remainder operator (%) corresponds to the remainder of the two integers.

You may find it difficult to understand that the following program (given the division of the business and the remainder) can help you understand the division operator and the meaning of the operator ... ...

public class Division
{
	public static void main(String args[])
	{
		int m,n;
		m = 99;
		n = 10;
		System.out.println ( m + "/" + n + "=" + m/n + "......" + m%n );
		m = 15;
		n = 4;
		System.out.println ( m + "/" + n + "=" + m/n + "......" + m%n );
		m = 9;
		n = 3;
		System.out.println ( m + "/" + n + "=" + m/n + "......" + m%n );
		m = 0;
		n = 10;
		System.out.println ( m + "/" + n + "=" + m/n + "......" + m%n );
		m = -1;
		n = 3;
		System.out.println ( m + "/" + n + "=" + m/n + "......" + m%n );
		m = -10;
		n = 3;
		System.out.println ( m + "/" + n + "=" + m/n + "......" + m%n );
		m = 10;
		n = -3;
		System.out.println ( m + "/" + n + "=" + m/n + "......" + m%n );
		m = -10;
		n = -3;
		System.out.println ( m + "/" + n + "=" + m/n + "......" + m%n );
	}
}

This program covers a variety of special circumstances ... ... For example, the case of divisors in the case of the remainder of 0, the divisor is 0 in the case of the quotient and the remainder are 0 , there are divisor / dividend is negative case - it seems a bit weird, But it can really run.

Note that in these two operators, trying to divide by 0 will cause the system to throw an exception.


Comparison operator

  • == equals
  • != Not equal to
  • > Greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to

Logical operator

  • && and
  • || or
  • ! not

Bit operator

  • >> right (signed)
  • << left (signed)
  • >> > padding right shift
  • & bitwise and
  • | Bitwise or
  • ~ Bit by bit
  • ^ Bitwise Exclusive or

The precedence of the operator

Unary Operators> Arithmetic Operators> Comparison Operators> Logical Operators> Bit Operators> Assignment Operators




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook