operator and type conversions in Java

Source: Internet
Author: User

Type conversions:

Automatic type conversion from small to large (implicit conversion)
When a small data type is calculated with a large data type, the result is automatically converted to a large type
byte char short-->int-->long-->float-->double
Note: Byte char short does not convert each other to their participating operations first converted to int type

Format: Data type variable with large range = Range Small Data type value


Coercion type conversion (display transformation)
Can tolerate the lack of precision can be forced to convert large types of data into small types of data
Format: Range small Data type variable = (range small data type) range large data type

The concept of the operator:

Symbols that operate on constants and variables are called operators

The concept of an expression:

A formula that joins a constant with an operator that conforms to the Java syntax can be called an expression.

Common operators: Arithmetic operator assignment operator relational operator logical operator ternary operator


Arithmetic operator: +-*/% + +--
In Java, the result of the division operation of two int data is that int cuts the decimal point directly.

Code Demo:
public static void Main (string[] args) {

int a = 10;
int B = 20;

System.out.println (A + B); 30
System.out.println (A-B); -10
System.out.println (A * b);//200
System.out.println (A/b);//0
System.out.println (1/2.0);//0.5
System.out.println (1/3);//0
System.out.println (2/3);//0
System.out.println ("===========================");
When a string is used in the middle of a number, the link finally gets a new string.
int d = 10;
System.out.println ("hehe" +10);//hehe 10
System.out.println ("hehe" +10+10);//hehe 1010
System.out.println ("hehe" + (10+10));//hehe 20
System.out.println (10+10+ "hehe");//20 hehe

System.out.println ("===========================");
System.out.println (10%3);//find remainder (modulo)
System.out.println (7%2);//1
System.out.println ("======================");
' A '---> ' b '---> 98
' A '---> 65
' 0 '---> 48
System.out.println (' a ' + 10);//107
System.out.println (' A ' + 10);//75
System.out.println (' 0 ' + 10);//58
System.out.println ((char) (' a ' + 1));//b

}

+ + operator:

When the + + operator is used alone, either the + + symbol on the left side or the right side of the variable indicates that the variable is self-increment by 1
When the + + operator is used together, if the + + symbol is on the left side of the variable, the emphasis is on the first change (increment 1) to mate,
If you are on the right side of the variable, emphasize the first mate and then change (self-increment 1)

--Operator:

When--when used alone--either on the left or right side of the variable, it means that the variable is self-minus 1
When--with the use of mates, if--on the left side of the variable, the emphasis is on first change (minus 1) then cooperate,
If on the right side of the variable, emphasize the first mate and then change (self minus 1)
Code Demo:
Public static void Main (string[] args) {

int B = 10;
System.out.println (b++);//10
System.out.println (b);//11
System.out.println ("==================");
int c = 20;
System.out.println (--c);//19
System.out.println (c);//19

int d = 30;
System.out.println (d--);//30
System.out.println (d);//29
}


Assignment operators:


Basic assignment Operator: =
Extended assignment Operator: + = = *=/=%=
Assign the left and right results to the left
Note: The left cannot be a constant
implies a forced type conversion
Benefits: More efficient
Code Demo:
public static void Main (string[] args) {

int a = 10;
a+=2; A = A + (2)---> A = ten + (2)---> A = 12
System.out.println (a);//12

int B = 20;
b-=2; B = B-(2)---> b = 18
System.out.println (b);//18

Short C = 10;
c = (short) (C + 10); Short = short + int---> short = int
c+=10;
System.out.println (c);//20

}


Relational operators:

= = (equals)! = (not equal to) > (greater than) < (less than) >= (greater than or equal) <= (less than or equal)
The relational operators are either Boolean or True or False
Code Demo:
public static void Main (string[] args) {
TODO auto-generated Method Stub
int a = 10;
int B = 20;
int c = 10;

System.out.println (A = = B);//False
System.out.println (A = = c);//True
System.out.println ("======================");
System.out.println (A! = b);//True
System.out.println (A! = c);//False
System.out.println ("======================");
System.out.println (a >= b);//False
System.out.println (a >= c);//True
System.out.println ("======================");
System.out.println (a <= b);//True
System.out.println (a <= c);//True
System.out.println ("======================");
System.out.println (a > B); False
System.out.println (a > C);//False
System.out.println ("======================");
System.out.println (a < b);//True
System.out.println (a < C);//False
System.out.println ("======================");

}


Logical operators:

logical operators for connecting Boolean expressions The final result value is a Boolean type
Not written in Java 3<x<6 should be written x>3&&x<6
With: & Rules: If both sides are false, the result of the entire expression is false, and only true for both sides.
Or: | Arithmetic rule: If there is a true on both sides, the result of the whole expression is true, only the two sides are false, the result is false
Non -:! Operation rules: True change false false change true
XOR: ^ Arithmetic rule: Both sides are the same false, different is true
Code Demo:
public static void Main (string[] args) {

System.out.println (True & True); T
System.out.println (False & True);//F
System.out.println (True & false);//F
System.out.println (False & False);//F
System.out.println ("=========================");
System.out.println (True | true); T
System.out.println (False | true);//T
System.out.println (True | false);//T
System.out.println (False | false);//F
System.out.println ("=========================");
System.out.println (!true);//F
System.out.println (!false);//T
System.out.println ("=========================");
System.out.println (true ^ true); F
System.out.println (false ^ true);//T
System.out.println (true ^ false);//T
System.out.println (false ^ false);//F

}

Short circuit with: &&
Short circuit or: | |
Benefits: Higher efficiency
Short circuit with: &&
The basic arithmetic rules are similar to &, except that if the left side is false, the right side is not executed, and the result is false directly.
Short circuit or: | |
Basic Arithmetic rules with | Similar, the difference is that if the left side is true, the right side does not execute, directly returns the result true
Three-dimensional expression:
Expression 1? Result 1: Result 2

Execution process:
To calculate the result of expression 1 first
Returns result 1 if the result is true, otherwise results 2
Code Demo: (Gets a large number of two numbers.) )
(int x=3,y=4,z;z = (x>y)? The x:y;//z variable stores a large number of two numbers. )
public class Demo09operator {
public static void Main (string[] args) {
int a = 10;
int B = 20;
int c = (a > B)? A:B;
System.out.println ("C:" + C);
}
}

(Compare two data is the same)
public class Demo10operator {
public static void Main (string[] args) {
Define two variables of type int
int a = 10;
int B = 20;

Boolean flag = (A = = b)? True:false;
Boolean flag = (A = = B);
SYSTEM.OUT.PRINTLN (flag);
}
}

(Gets the maximum value in three integers)
public class Demo11operator {
public static void Main (string[] args) {
Define three variables of type int
int a = 10;
int B = 30;
int c = 20;

Compare the large values of two integers first
int temp = ((a > B)? a:b);
int max = ((temp > C)? temp:c);
System.out.println ("Max:" + max);
}

}

operator and type conversions in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.