Java Basics Java basic data types and bit operations

Source: Internet
Author: User
Tags bitwise bitwise operators logical operators

1. Assignment Operators

The assignment uses the operator "=". It means "take the right value (i.e. the right value) and copy it to the left (that is, the left value)". The right value can be any constant, variable, or expression (as long as it can generate a value on the line). But the left value must be a definite, named variable. That is, you must have a physical space to store the value to the right of the equals sign.
Classification base data type differs from class data type
1, the basic data type assignment is very simple. The basic data stores the actual numeric value, not the reference to an object, so when it is assigned, it is copied directly to the other place.
2, but when the object "assignment", the situation has been released changes. To manipulate an object, what we really do is reference to the object. ----This particular phenomenon is often referred to as "aliasing", which is a basic way of working with Java objects.
----excerpt from "Thinking in Java" P39

2. Relationship of Byte, Word, bit, byte

1 Byte = 8 Bits
1 KB = 1024x768 Bytes
1 MB = 1024x768
1 GB = 1024x768
BPS is the abbreviation for bits per second. The transmission rate of general data machine and network communication is in "bps" unit. such as 56Kbps, 100.0Mbps and so on.
BPS is the abbreviation for byte per second. Computers generally display the speed in bps, such as 1Mbps approximately equal to KBps.

3. The amount of space the base data type occupies

Byte occupies 8 bits, or a byte. Because it is signed, the hour is-128 and the maximum is +127.
Short occupies 16 bits, two bytes. Minimum value 2^15, maximum value 2^15-1.
int occupies 32 bits, 4 bytes. Minimum value 2^31, maximum value 2^31-1
Long occupies 64 bits, 8 bytes. Minimum value 2^63, maximum value 2^63-1
Float occupies 32 bits, 4 bytes. Minimum positive non 0 value 2^-149, maximum positive non 0 value (2-2^-23) 2^127.
Double is 64 bits, 8 bytes. The minimum positive non-0 value is 2^-1074, and the maximum positive non-0 value is (2-2^-52) 2^1023
Char occupies 16 bits, 2 bytes. The main purpose is to support Unicode, so char in Java is a double byte, not a byte in C + +
Boolean online is more, but according to the contents of the bytecode, if it is a single Boolean variable, then use an int to store, if it is a Boolean array, it is stored in byte.

4. Note the difference between C + +

int occupies 2 bytes in C and C + + and 4 bytes in Java.
In C + +, char is the underlying data type, 8 bits, 1 bytes. Byte is not the underlying data type, generally a typedef unsigned char byte; in other words, byte is actually a unsigned char type, then it is also 8 bits, 1 bytes. The difference is that char can represent a range of-128-127, and a byte can represent a range of 0-255.
In Java, char and byte are the underlying data types, where the char type in byte and C + + is the same, 8-bit, 1-byte, 128-127. However, the char type, which is 16 bits, 2 bytes, ' \u0000 '-' \uffff ', can represent the unsigned number of 0~65535.
Why is a char in Java 2 bytes?
Because the inside of Java is Unicode, so Java is actually supporting the Chinese variable name, such as the string world = "My World", such statements can be passed.

5. Logical Operators

Logical operators are used to concatenate Boolean expressions. 、
The logical operators include "&", "&&", "|", "| |".
&: mean and, all the judging conditions should be executed in turn;
&&: A number of conditions, if the previous condition returns false, then no longer judge, is false;
|: Normal or, all judgment conditions should be executed in turn;
|| : A number of conditions, if the previous condition returns True, then no longer judged, is true;

6. Bitwise operators

Bitwise operators are primarily for binary,
It includes: Bitwise AND (&), non (~), bitwise OR (|), bitwise XOR (^).
On the surface it seems a bit like a logical operator, but the logical operator is a logical operation on two relational operators, while the bitwise operator is mainly for the bits of two binary numbers.
&: When the bits corresponding to both operands are 1 o'clock, the result is 1, otherwise 0. such as 1100&1010=1000
|: When both sides of the operand correspond to a bit as long as one is 1, the result is 1, otherwise 0. such as 1100|1010=1110
~: bits of operand, 0 change to 0, this is monocular operator
^: When both operands correspond to a bit value that is not the same, the result is 1, otherwise 0. such as 1100^1010=0110

7. Bitwise MOVE Operator

There are three main types of bit movement operators:
<< (shift left):
In the absence of overflow, for positive and negative numbers, the left one is multiplied by 2 of the 1, left n is the equivalent of multiplying by 2 of the n-th square
>> (right Shift with symbols): Move right to high fill sign bit
Move right one is equivalent to 2, and the right shift n is the equivalent of dividing by 2 of the n-th side.
>>> (unsigned Right shift): Move right to High 0,
The unsigned right shift operator only has meaning for 32-bit and 64-bit values

8. Example Validation

public static void Main (string[] args) {//1, Shift Left (<<)//0000 0000 0000 0000 0000 0000 0000 0101 Then move left After 2 bits, the low 0:////0000 0000 0000 0000 0000 0000 0001 0100 are converted to 10 System.out.println (5 << 2);//Run result is 2 0//2, right Shift (>>) high fill sign bit//0000 0000 0000 0000 0000 0000 0000 0101 Then right shift 2 bit, high fill 0://0000 0000 00        0000 0000 0000 0000 0001 System.out.println (5 >> 2);//Run result is 1//3, unsigned Right shift (>>>) high fill 0 For example, 5 is converted to binary after: 0101 take the inverse plus 1 for 1011//1111 1111 1111 1111 1111 1111 1111 1011///We move right 5 bits to 3, 5 for right 3 bits and unsigned right Move 3 bits: System.out.println (5 >> 3);//result is 0 System.out.println ( -5 >> 3);//result is -1 SYSTEM.OUT.P Rintln ( -5 >>> 3);//The result is 536870911//4, Bit with (&)//bit with: the first operand of the nth position in the second operand if all is 1, then the result of Nth is also 1, otherwise        is 0 System.out.println (5 & 3);//result is 1 System.out.println (4 & 1);//result is 0//5, bit or (|) The nth position of the first operand is the nth of the second operand As long as there is a 1, then the result of Nth is also 1, otherwise 0 System.out.println (5 | 3);//The result is 7//6, Bit XOR (^)///The nth position of the first operand in the nth bit of the second operand        Conversely, the result of Nth is also 1, otherwise 0 System.out.println (5 ^ 3);//The result is 6//7, bit non (~)//operand of the nth bit is 1, then the result of the nth bit is 0, and vice versa.  SYSTEM.OUT.PRINTLN (+);//result is-6}

Java Basics Java basic data types and bit 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.