Home > Java > javaTutorial > What are the basic operators in Java?

What are the basic operators in Java?

零下一度
Release: 2017-06-25 10:27:04
Original
1690 people have browsed it

Java Operator

Operator: It is a special symbol used to perform operations, assignments, and comparisons on data.

Operators in the Java language are divided into the following categories:

  • Arithmetic operators: + - * / ++ --

The example code is as follows:

 1 public class TestAir { 2     public static void main(String[] args) { 3         int i=12; 4         int j=5; 5         //加减法运算 6         int k=i+j; 7         System.out.println(k); 8         k=i-j; 9         System.out.println(k);10         //乘除运算11         k=i*k;12         System.out.println(k);13         double dou;14         dou=i*5.0;15         System.out.println(dou);16         //  /代表对某数去整17         double d=i/j;18         System.out.println(d);19         d=i/5.0;20         System.out.println(d);21         // %代表对某数取余数22         k=i%j;23         System.out.println(k);24         25         //++、--运算符26         System.out.println(i++);//++在变量后面时,是使用完这个变量才会进行+1操作27         System.out.println(++i);//++在变量前面时,是使用这个变量之前就进行+1操作28         //++、--运算符29         System.out.println(i--);//--在变量后面时,是使用完这个变量才会进行-1操作30         System.out.println(--i);//--在变量前面时,是使用这个变量之前就进行-1操作31     }32 }
Copy after login

  • Assignment operator

First It should be noted that the assignment operator has the lowest priority in Java, that is, the assignment operator is always executed last in an expression that contains other operators.

The example code is as follows:

 1 public class TestAssign { 2     public static void main(String[] args) { 3         //+=、-=、*=、/= 4         int a=10; 5         int b=2; 6         a+=b;//a=a+b; 7         System.out.println(a); 8         a-=b;//a=a-b; 9         System.out.println(a);10         a*=b;//a=a*b;11         System.out.println(a);12         a/=b;//a=a/b;13         System.out.println(a);14     }15 }
Copy after login

  • Comparison operator (relational operator)

Comparison operator is used to judge two The size of data, such as: greater than, equal to, not equal to. The result of the comparison is a Boolean value ( true or false ).

Commonly used comparison operators in Java are shown in the following table:

 

The sample code is as follows:

 1 public class TestCompare{ 2     public static void main(String[] args) { 3         int a=16; 4         double b=9.5; 5         String str1="hello"; 6         String str2="imooc"; 7         System.out.println("a等于b:" + (a==b)); 8         System.out.println("a大于b:" + (a>b)); 9         System.out.println("a小于等于b:" + (a<=b));10         System.out.println("str1等于str2:" + (str1==str2));11     }12 }
Copy after login

Note:

1, > , < , >= , <= only supports that the left and right operands are numeric types

2. == , != The operands on both sides can be either numerical types or reference types

  • Logical operators

Logical operators are mainly used to perform logical operations. The commonly used logical operators in Java are shown in the following table:

 

 

We can understand logical operators from the perspective of "voting":

1. With: All people are required to vote to approve an issue

2. Or: Only one person is required to vote to approve an issue.

3. Not: Someone Originally voted to agree, the non-operator can be used to invalidate the vote

4. XOR: Only one person can vote to approve an issue

When using logical operators When, we will encounter a "short circuit" phenomenon.

For example: ( a > b ) && ( a < c ), if it can be determined that the execution result of the expression on the left is false, the system will consider that it is no longer necessary to execute the expression on the right.

Similarly, in ( a > b ) || ( a < c ), if it can be determined that the execution result of the expression on the left is true, the system will also think that it is no longer necessary to execute the expression on the right expression!

  • Ternary operator

Also known as conditional operator boolean expression? Expression a: Expression b When the boolean expression result is true, expression a is executed. When the boolean expression result is false, expression b is executed.

The sample code is as follows:

1 public class TestCondition{2     public static void main(String[] args) {3         int score=68;4         String mark =(score>=60)?"及格了!!":"很遗憾,没及格!!";5         System.out.println("考试成绩如何:"+mark);6     }7 }
Copy after login

The above is the detailed content of What are the basic operators in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template