当前位置:网站首页>Variables, constants, operators
Variables, constants, operators
2022-04-23 03:34:00 【Chen Yu】
Java Summary of knowledge points : You can enter from here if you want to see
2.4 Variable 、 Constant
2.4.1 Variable
It's essentially a storage space , Determine the location , Value uncertainty . Access storage space by variable name , So as to operate the value stored inside .
java As a strongly typed language , Data type must be declared before definition
, Storage space of corresponding length can be allocated only after declaration .
1、 Before each variable is defined, the type must be declared , It can be of any type
2、 The variable name is a legal identifier
3、 The definition of variables belongs to a complete statement , Must be ; ending
Differences between static variables and instance variables ?
answer : Static variables should be matched with keywords static Use . Create in class , Become a class variable , As long as such bytecode is loaded , You don't have to create objects , It will also be divided With storage space , It can be directly referenced by class name . No matter how many objects are created , The static variable of the operation is a , Generally use capital letters
Instance variables are declared outside the French sentence block of the class , Properties for class , When an instance object is created , Destroy when the object is destroyed .
2.4.2 Constant
The amount that will not be changed during the operation of the program . With keywords final Constants can only be assigned once in a program , Share among all objects .
grammar :final data type Constant names = value ; ( Constant names are generally expressed in uppercase letters )
When a constant is used as a member variable, it must be given an initial value .
JAVA 10 A new feature after : Use var
Similar to JavaScript, Allows the compiler to use its initial value to infer the type of local variable .
- var Cannot be declared as a variable , It is used by the compiler to determine the type during initialization , So you can't reallocate with different data types , Use var The initialized variable data type is inferred only according to the data type of the initializer at compile time , And can't change .
- It cannot be used with method parameters and method return types . Because the compiler cannot infer which type of var Replaced at runtime .
- It cannot be used as an instantiation variable .
- Cannot be used for class variables .
- It cannot be initialized to NULL value
2.5 Operator
2.5.1、 Arithmetic operator
Arithmetic operator
+ The addition operator ( String addition , Mathematical plus sign ) Plus sign
- Subtraction operators Minus sign
* Multiplication operators
/ Division operator ( The result will be an error due to the data type )
% Seeking remainder
++ Self increasing : The value of the operands increases 1
++1: First of all, increase by yourself , And then I'll take part in the calculation int a = 1; int b = a++; int c = ++a; a=3 b=1 c = 3
1++: Take part in the calculation first , Since the increase again
-- Self reduction : The value of the operands decreases 1
--1: First self reduction , And then I'll take part in the calculation
1--: Take part in the calculation first , And then reduce
2.5.2、 Assignment operator
Assignment operator
= Assign the value of the right operand to the left
+=、-=、*=、/= Add the values of the left and right operands 、 reduce 、 ride 、 except 、 After assignment to the left operand
%=: Modulo and assignment operators , It modulates the left and right operands and assigns them to the left
<<=: Left shift assignment operator C <<= 2 Equivalent to C = C <<2
>>=: Right shift assignment operator C >>= 2 Equivalent to C = C >>2
&=: Bitwise and assignment operators C&= 2 Equivalent to C = C&2
^=: Bitwise XOR assignment operator
|=: Bitwise or assignment operators
2.5.3、 Relational operator
Relational operator
== Determine if the values of two operands are equal , If they are equal then the condition is true .
!= Determine if the values of two operands are equal , If the values are not equal, the condition is true .
> Determines whether the value of the left operand is greater than the value of the right operand , If so, the condition is true .
>= Determines whether the value of the left operand is greater than or equal to the value of the right operand , If so, the condition is true .
< Determines whether the value of the left operand is less than the value of the right operand , If so, the condition is true .
<= Determines whether the value of the left operand is less than or equal to the value of the right operand , If so, the condition is true .
instanceof : Check whether the object is of a specific type ( Class type or interface type ).
String name = "Java"
name instanceof String result :true(name yes String Type of )
2.5.4、 Logical operators
Logical operators
&&: And 、 And A false is a false , True is true
||: or When it's true, it's true
!: Not Take the opposite
2.5.5、 An operator
An operator
&: If all the corresponding bits are 1, The result is 1, Otherwise 0
a: 0011 1100
b: 1111 1111
a&b: 0011 1100
|: If all the corresponding bits are 0, The result is 0, Otherwise 1
a: 0011 1100
b: 1111 1111
a|b: 1111 1111
^: If the corresponding bit values are the same , The result is 0, Otherwise 1
a: 0011 1100
b: 1111 1111
a^b: 1100 0011
〜: The bitwise negate operator flips every bit of the operands , namely 0 become 1,1 become 0.
a: 0011 1100
~a: 1100 0011
<<: Bitwise shift left operator . The left operand moves left by bits the number of bits specified by the right operand . amount to *2
>>: Bitwise shift right operator . Shift left operand right by bit number of bits specified by right operand . amount to /2
>>>: Shift bit right to zero operator . The value of the left operand is shifted right by the number of bits specified by the right operand , Move the resulting space to zero .
& and && The difference between :
&& Is a logical operator , And 、 And what it means . If the expression on both sides is false, it will be false , True is true . Short circuit function , For example, the expression on the left is false, be The expression on the right does not participate in the operation
& It's a bit operator , Bitwise AND , The expressions on both sides of the equation are false When doing logic and operation , But both sides of the equation are involved in the calculation , No short circuit function .
2*8 How to calculate the fastest ?
answer : The bitwise shift left operator operates fastest , hold 2 Move left 3 position 2<<3
2 convert to 2 Into the system for :0000 0010
Shift left three places :0001 0000 convert to 10 Into the system for 16
<< >> It's very efficient
2.5.6、 Ternary operator
Conditional operator ( Ternary operator )※※※※※※
( Conditions )?a:b If the condition is true execute a, Otherwise execution b
2.5.7、 priority
版权声明
本文为[Chen Yu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230332246414.html
边栏推荐
- 常用的辅助类
- 标识符、关键字、数据类型
- 2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
- Deep learning notes (II) -- principle and implementation of activation function
- Common exceptions
- 批量下載文件----壓縮後再下載
- JS takes out the same elements in two arrays
- 2022 group programming ladder game simulation L2-4 Zhezhi game (25 points)
- 变量、常量、运算符
- Problem a: face recognition
猜你喜欢
On the principle of concurrent programming and the art of notify / Park
The art of concurrent programming (3): an in-depth understanding of the principle of synchronized
Flink customizes the application of sink side sinkfunction
Idea debug debugging tutorial
PYMOL-note
L3-011 直捣黄龙 (30 分)
标识符、关键字、数据类型
2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
MySQL keyword group_ Concat, combined connection query
MySQL is completely uninstalled and MySQL service is cleaned up
随机推荐
对象和类的概念
Three types of jump statements
Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
Can you answer the questions that cannot be answered with a monthly salary of 10k-20k?
Flink customizes the application of sink side sinkfunction
常用的辅助类
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
The art of concurrent programming (5): the use of reentrantlock
Visual programming - Experiment 1
Wechat payment iframe sub page has no response
JS calculates the display date according to the time
Design and implementation of redis (5): master-slave replication strategy and optimization
Unity knowledge points (common core classes)
Visual programming -- how to customize the mouse cursor
2022 group programming ladder simulation l2-1 blind box packaging line (25 points)
QT learning summary
Download and configuration of idea
L3-011 direct attack Huanglong (30 points)
标识符、关键字、数据类型
Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)