Java data types and operations

Source: Internet
Author: User
Tags arithmetic bitwise operators

First, the data types in Java are divided into two broad categories:

1. Basic data type/native data type (total eight)

Integer type: Byte (1 bytes), short (2 bytes), int (4 bytes), Long (8 bytes). "Byte,shuort,int,long The only difference is the size of the storage capacity is not the same, according to the specific needs of the data stored to choose the appropriate data type, generally choose int"

Float type: Float (4 bytes), double (8 bytes)

Character type: char (2 bytes)

Boolean: Boolean (1-bit)

2. Reference data Type/Object data type

Class, interface, array

Integer type (byte,short,int,long)

Four representations of integer constants:
① binary integers: 0B or 0b (new features of Java7), such as: int a = 0b10100;
② octal integer: Required to start with 0, such as int a = 0157;
③ decimal integer: For example: int a = 100;
④ hexadecimal integer: Requires a 0X or 0x start, such as int a = 0xfffff123;

The Java language's integer constants are type int by default, and a long variable is declared with ' l ' or ' l ', and uppercase L is recommended because the lowercase l is easily confused with the number 1.
By default, the literal of an integral type is the int type by default.

Decimal type (float,double):
Represents a decimal type, also known as a floating-point type, in which float represents a single-precision type and double represents a dual-precision type, but neither can represent an exact decimal number.
There are two representations of the floating-point type constants in Java:
A, decimal form: For example: 3.14, 168.0,. 618
b, scientific notation form: For example: 3.14e2, 3.14E2, 1000E-2
Scientific notation The result returned is a double type.

By default, the literal of a floating-point type is the double type by default. To declare a constant of type float, you need to add either D or D after the constant after the F or f,double constant to omit it.
Note: Only floating-point variables in Java can accept scientific computational results.

Character type (char):

characters, letters, and symbols.
Char type: Represents a 16-bit unsigned integer or Unicode character, and Java encodes characters with Unicode characters.
Unicode collects symbols from all the languages in the world, and is a cross-platform encoding that takes two bytes of Java characters and can represent a single character.
method represented in Char constant 2

1. Assigned the required characters directly to the variable, e.g. char a= ' a ';

2. Use decimal or hexadecimal as the assignment, such as Char a=65; the resulting value is ' a '.

Hex, such as: Char a= ' \u0041 ';

Basic Data type Conversions

In the 8 large base data types,Boolean is not a numeric type and does not participate in conversions.
In general, Byte,short,char does not participate in the conversion operation. We directly pay the Byte,short,char directly to the int type.

An automatic type conversion, also known as an implicit type conversion:
When assigning a numeric value or variable of a small data range type to another large data range type variable, the system can complete the automatic type transformation

byte b= 1;  Short s=b; // Small Turn Big int i= s; Long l=i; float f = l; double d=//1.0

Coercion type conversions, also known as Display type conversions:

When assigning a value or variable of a large range type to another small-scope type variable, the system cannot complete the conversion automatically, and a forced conversion is required, but such an operation may result in a reduction or overflow of data precision, so use caution.

double d = 1.23d; int i = (int) D; SYSTEM.OUT.PRINTLN (//result=1

automatic promotion of an expression type when an arithmetic expression contains a value of more than one base data type (except the Boolean), the data type of the entire arithmetic expression will automatically ascend when the data is operated, with the following rules:
All of the byte, short, and char types are automatically promoted to the int type;
Second, arithmetic operation

  

Here the main discussion is under + + and--

+ + and----explain: You can only manipulate variables, cannot manipulate constants.
+ +: Indicates that the current action variable is self-accumulating 1.
--: Represents the current action variable minus 1 for itself.
--------------------------------------------------------------------
Self-Increase: + +, increment operator, increase the value of the variable by 1, there are pre-and post-points, can only manipulate variables.
Decrement:---decrements the operator so that the value of the variable is reduced by 1, with pre-and post-set points, and only the variables can be manipulated.
Self-increment and decrement specific operation is the same, just one is plus 1, one is minus 1, now single Talk + +:
The code result + + and ++result will cause the value of the result variable to be added 1.
The only difference is:
Front (++result): Indicates that the result after results plus 1 is calculated,
Post (result++): Represents the operation of the value (original value) before the result variable plus 1.
If you simply perform a simple incremental operation (write result++ or ++result only), choose either one.
Comparison operators:

Used to compare the relationship between two variables or constants, the result of the comparison operator is a Boolean type that has the following format:
Boolean result = expression A comparison operator expression B;
Ternary operators:

Syntax format: X? Y:z;

where x is a Boolean expression, the value of X is computed first, and if the x result is true then the result of the entire trinocular expression is Y, otherwise it is Z. The type of the result of the trinocular operator is determined by Y and Z.
Logical operators:

&: Indicates and, if both operands A and B are true, the result is true, otherwise the result is false.
&&: Same as & results, with short-circuit effect, if the left operand A is false,result must be false, and does not run the operation of B.
|: Indicates that either A and B are false and result is false, as long as there is a or B is true, the result is true.
|| : and | The result is the same, with a short-circuit effect, if the left operand A is true,result must be true, and the operation of B is not run.
^: Judging whether A and B are different, the difference is true, and the same is false.
!: Inverse,!true The result is False,!fasle the result is true.
Bitwise operators:

<<: Moves the binary code of the operand to the left of the specified number of digits, and then adds the null after the left shift using "0".
>>: Moves the binary code of the operand to the right of the specified number of digits, and the null after the right shift uses the symbol bit to supplement:
If positive use "0" supplement;
If negative number uses "1" supplement; (Operation negative: reverse, complement, operate, reverse, seek to fill)
>>>: Moves the binary code of the operand to the right of the specified number of digits, and the empty space after the right shift is supplemented with "0".
Precedence of operator operations:

Java data types and operations

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.