Java operator

Daily nuts 2020-11-06 22:48:57
java operator


Catalog :

1. Concept

2. Arithmetic operator

3.  Assignment operator

4.  Comparison operator

5.  Logical operators

6.  Ternary operator

Concept

Operator : A symbol for a specific operation, such as :+ - * /

expression : Expressions connected by operators are called expressions

stay Java Divided into : Arithmetic operator 、 Assignment operator 、 Comparison operator 、 Logical operators 、 Ternary operator

Arithmetic operator

 

Basic usage

public static void main(String[] args) {
//  Attribute operations can be performed between constants 
 System.out.println(20 + 30);

 //  Variables can also be operated between mathematics 
 int a = 20;
 int b = 30;
 System.out.println(a + b);
 //  Variables and constants can be mixed 
 System.out.println(a * 10);

 //  division 
 // 1  Are integers. , The result is also an integer , Just look at the business , There is no remainder 
 int x = 10;
 int y = 3;
 System.out.println(x / y); // 3

 //  modulus ( Take the remainder ) Only the division modulus of the corresponding integer is meaningful 
 int result2 = x % y;
 System.out.println(result2); // 1

 //  If the operation has different data types , The result is a wide range of data types , First raise the data type , Re operation 
 double result = x / y;
 System.out.println(result); // 3.0

}

+ Three uses of

public static void main(String[] args) {
//  usage 1: For numbers , Namely + Law 
 int x = 30;
 int y = 40;
 int result = x + y;
 System.out.println(result);

 //  usage 2: For characters char In terms of type ,char Will be prompted to be int. And then calculate 
 char c = 'A';
 int result2 = c + 1;
 System.out.println(result2);

 //  usage 3: Corresponding string String(),+  Represents a string join operation 
 String s1 = "Hello ";
 String s2 = "Word";
 String result3 = s1 + s2;
 System.out.println(result3);

 //  Add any data type to a string , The results are all strings 
 int z = 300;
 String s3 = " individual ";
 System.out.println(z + s3);

 //  priority 
 String s4 = s3 + z + x;
 System.out.println(s4);

 //  Change priorities 
 System.out.println(s3 + (x + y));

}

Auto increment and auto decrement operator ++、--

meaning : Let's make a variable +1 perhaps -1

Format :++ Variable name , Variable name ++

public static void main(String[] args) {
//  Use alone 
 int a = 100;
 a++; //  after ++
 System.out.println(a); // 101
 ++a; //  front ++
 System.out.println(a); // 102

 //  A mixture of 
 // 1  If it's before ++, Variables are calculated first , Reuse 
 int num1 = 100;
 int num2 = 100;
 num1 = num1 + ++num2;
 System.out.println(num1); // 201
 System.out.println(num2); // 101
 // 2  If it's post ++, Variable first uses , Calculate again 
 int b = 100;
 int z = 100;
 b = b + z++;
 System.out.println(b); // 200
 System.out.println(z); // 101
}

Note the arithmetic operator :

1. Both constant and variable can be operated between mathematics  

2. Constants cannot use the increment and decrement operator , Because constants themselves are immutable

2. Java In the use of the assignment operator operation , You don't get a decimal

Assignment operator  

Basic assignment operators (=)

meaning : Put the amount to the right of the symbol , Assign a value to the variable on the left

Sign assignment operator

public static void main(String[] args) {
// =
 int a = 100; //  Put the... On the right 100 Give it to the left a Storage 

 // +=  amount to b = b + b;
 int b = 100;
 b += b;
 System.out.println(b); // 200

 // -=
 int num1 = 100;
 num1 -= 100;
 System.out.println(num1); // 0

 // /=
 int num2 = 10;
 num2 /= 3;
 System.out.println(num2); // 3

 // %=
 int num3 =10;
 num3 %= 3;
 System.out.println(num3); // 1

 //  Symbolic operators include cast 
 byte num4 = 30;
 num4 += 5;
 System.out.println(num4); // 35
}

Note the assignment operator :

1.  Constants cannot be used for assignments , It cannot be written to the left of the assignment operator

2. The compound assignment operator implies cast

int num4 = 30;
num4 += 5;
// The following transformation will occur
num4 = byte + int;
num4 = int + int;
num4 = (byte)int;

Comparison operator

public static void main(String[] args) {
//  Constant comparison 
 System.out.println(1< 2);

 //  Variable comparison 
 int a = 10;
 int b = 20;
 System.out.println(a < b);

 // >=  Greater than or greater than = All for true
 System.out.println(a > 11); // false
 System.out.println(a >= 10); // true

 // ==  be equal to 
 System.out.println(a == b); // false

 // !=  It's not equal to 
 System.out.println(a != b); // true
}

Note the assignment operator

1. The result must be boolen value

2. You can't judge more than once in a row 10 < a < 20( Wrong writing ), You need to use logical operators

Logical operators

        &&                                  ||                    !                    
     And ( also )          or ( perhaps )                             Not ( Take the opposite )
Is full of true It's just true There is one true by true,     Is full of false by false If true Take instead false
public static void main(String[] args) {
int a = 10 ;
 int b = 20;
 int c = 30;

 // &&  And 
 boolean flag = a > b && b > c;
 System.out.println(flag);

 // ||  or 
 boolean flag1 = a > b || b > c;
 System.out.println(flag1);

 // !  Not 
 boolean flag2 = !(a > b || b > c);
 System.out.println(flag2);

 //  Logic short circuit 
 System.out.println(a > b && ++a >b);
 System.out.println(a);  //a = 10, It's not implemented && Later code 
}

Note the logic operator

1. It must be boolean value

2. && 、|| It has the effect of short circuit

3. && 、|| There should be... On both sides boolean value ,! Just have to have one boolean value

4. You can write multiple conditions

 Logic short circuit : If the code on the left can already judge the final result , Then the code on the right will no longer execute

Ternary operator

Unary operator : An operator that can operate with only one data , Such as : Take the opposite !、 Self increasing ++、 Self reduction --

Binary operator : An operator that requires two operators to operate , Such as : Add +、 assignment =

Ternary operator : An operator that requires three data to operate on

 Ternary operator format : data type Variable name = conditional ? expression A : expression B

technological process :

public static void main(String[] args) {
int a = 10;
 int b = 20;
 int result = a > b ? a : b;
 System.out.println(result);    // 20
}

Note the ternary operator

1. The expression on the right must be guaranteed A And expressions B All match the data type on the left

2. Ternary operator results must be used

//  Error model 
double d = 2.5;
int result2 = a > b ? a : d;    // Type mismatch 
a > b ? a : d; // not used 

 

版权声明
本文为[Daily nuts]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云