当前位置:网站首页>Day07 type of mathematical operator automatic conversion relational operator bitwise operator blind date math
Day07 type of mathematical operator automatic conversion relational operator bitwise operator blind date math
2022-07-05 07:36:00 【33 year old Java enthusiast】
Operator understanding
Operator
Arithmetic operator :
- +
- -
- multiply
- /
- %( Take the remainder ) Modular arithmetic
- ++
- –
public class demo01{
public static void main(String[] args){
int a =10;
byte b =20;
System.out.println(a+b); // The result type is int
// Just add, subtract, multiply and divide integers , without long The type of , By default, it will be uniformly converted to int type
long d =1237192381273891723891289L;
byte e = 12;
System.out.println(d+e); // The result type is LONG
// As long as there is long type , The result will be converted to long type
// As long as there is double type , The result will be converted to double type
/* Be careful : If the result shows floating point numbers , If there is no conversion type, you will get 0 Result . */
//System.out.println(a/b);// The actual result is 0.5, The result is 0, Because floating point numbers are not converted
System.out.println(a/(double)b); // Correct expression
}
}
Automatic conversion rules for calculation :
- Just add, subtract, multiply and divide integers , without long The type of , By default, it will be uniformly converted to int type
- As long as there is long type , The result will be converted to long type
- As long as there is double type , The result will be converted to double type
word :cast transformation
idea shortcuts :ctrl+d Quickly copy the previous line
package operator;
public class demoday01 {
public static void main(String[] args) {
int a = 10;
byte b =20;
long c=100L;
double d =3.14;
System.out.println(a+b); // Floating point numbers and long type , The default conversion is int
System.out.println(a-c); // appear long type , Automatic conversion to long type
System.out.println(c*d); // Floating point numbers appear , Automatically convert to floating point number type , Due to discrete type , The suffix will be inaccurate
System.out.println(a/(double)b); // When floating-point numbers appear, we have to convert them to floating-point numbers to calculate , Otherwise, we will only get 0
short g1 = 5;
short g2 = 2;
System.out.println(g1/g2);//2 int type , There will be no floating point numbers
System.out.println(g1%g2);//1 Modular arithmetic Take the remainder
System.out.println(g1/(double)g2);//2.5
}
}
Operation rules of self increase and self decrease
a++ Follow ++a The difference between
b=a++ amount to ,b=a, then a=a+1
b=++a amount to a=a+1, Then it's b=a
Assign a value before the equal sign , The equal sign is followed by calculation and then assignment
package operator;
public class demo03Day06 {
public static void main(String[] args) {
int a = 2;
int b =a++; // It is equivalent to calculating first b=a, then a=a+1
int c =++a; // amount to a=a+1, then c =a
System.out.println(b); //2
System.out.println(c); //4
}
}
Assignment operator : =
Relational operator :
- >
- <
- >=
- <=
- ==
- !=
- instanceof
package operator;
public class demo02Day06 {
public static void main(String[] args) {
int a = 1;
int b = 1;
int c = 2;
byte d = 2;
double e =2.123123;
float f = 2.123123f;
short g =5;
System.out.println(a==b);//true
System.out.println(a==c);//false
System.out.println(d==c);//true
System.out.println(d!=e);//true
System.out.println(f==e);//false Accuracy error Avoid floating-point comparisons
System.out.println(c<=e);//true Avoid floating-point comparisons
System.out.println(g/c);// The type is not converted =2
// Modular arithmetic , That is, take the remainder
System.out.println(g/(double)c);// correct
System.out.println(g%c);// Modular arithmetic result =1
}
}
Logical operators
- && ( And )and
- || ( or )or
- ! ( Not )not Take the opposite
package operator;
public class demo04Day06 {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
boolean c =true;
System.out.println(a&&b);// Two for the truth
System.out.println(a||b);// One of them is true
System.out.println(!(a&&b));// Just one of them is fake , It's true . As long as both are true , It's fake .
System.out.println(!(a&&c));// As long as both are true , It's fake . Just one of them is fake , It's true .
// Short-circuit operation
int d =5;
boolean f = false;
System.out.println(f&&(d>d++));// As long as the first part is fake , Stop the following operations
System.out.println(d);// Originally d++ What is calculated first can be equal to 6, The condition is true . But if the first part is false, just quit the operation
}
}
An operator ( Binary is used )
A =0011 0101
B=1001 0010
A&B =0001 ,1000 //and, If the top and bottom are the same, it is 1, Or for 0
A|B =1011 0111 // perhaps , If the next one is 1 for 1
A^B =0101 1000 // Take the opposite , If the upper and lower are the same , Then for 1, No is 0
~A =1100 1010 // Take the opposite
2<<3 2 Move right 3 position , amount to 2 multiply 2 multiply 2 multiply 2
0000 0000 by 0
0000 0001 by 1
0000 0010 by 2, Move three places to the right 0001 0000
0000 0011 3
0000 0100 4
0000 0101 5
0000 0110 6
0000 0111 7
0000 1000 8
0001 0000 by 16
Extracurricular expansion
java Calculating power , How many powers of numbers .
Math yes java Important mathematical operation Library
quote Math Library pow Calculation .
package operator;
public class demo03Day06 {
public static void main(String[] args) {
double d =Math.pow(2,4); //2 The fourth power of , Output as floating point number
System.out.println(d);
}
}
边栏推荐
- How can Oracle SQL statements modify fields that are not allowed to be null to allow nulls?
- Unforgettable summary of 2021
- ORACLE CREATE SEQUENCE,ALTER SEQUENCE,DROP SEQUENCE
- [neo4j] common operations of neo4j cypher and py2neo
- Reading literature sorting 20220104
- Ugnx12.0 initialization crash, initialization error (-15)
- Oracle code use
- arcpy. SpatialJoin_ Analysis spatial connection analysis
- Matrix keyboard scan (keil5)
- 数字孪生实际应用案例-风机篇
猜你喜欢

UE5热更新-远端服务器自动下载和版本检测(SimpleHotUpdate)

With the help of Navicat for MySQL software, the data of a database table in different or the same database link is copied to another database table

CADD course learning (5) -- Construction of chemosynthesis structure with known target (ChemDraw)

Clickhouse database installation deployment and remote IP access

The folder directly enters CMD mode, with the same folder location

HDU1232 畅通工程(并查集)

Differences between pycharm and idle and process -- join() in vs Code

【idea】Could not autowire. No beans of xxx type found

Nombre - 1. Création de tableaux

Ugnx12.0 initialization crash, initialization error (-15)
随机推荐
Daily Practice:Codeforces Round #794 (Div. 2)(A~D)
Apple system shortcut key usage
Thunderbird tutorial \ easy to use mail client
Microservice registry Nacos introduction
The SQL implementation has multiple records with the same ID, and the latest one is taken
Install deeptools in CONDA mode
What if the DataGrid cannot see the table after connecting to the database
String alignment method, self use, synthesis, newrlcjust
Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘
SQL JOINS
Daily Practice:Codeforces Round #794 (Div. 2)(A~D)
Opendrive ramp
How to deal with excessive memory occupation of idea and Google browser
P3D gauge size problem
repo. conda. An example of COM path error
Basic series of SHEL script (II) syntax + operation + judgment
Explanation of parallel search set theory and code implementation
Machine learning Seaborn visualization
And play the little chestnut of dynamic agent
Apple modify system shortcut key