Java Beginner (ii)

Source: Internet
Author: User

First, the data type

When defining a long or float type variable, add L or f (case insensitive, only easy to identify, no lowercase l recommended)

integers are int by default, and floating-point numbers are double by default

Second, Java characters

The Java language uses Unicode encoding, where one character in Java is two bytes, so one character in Java can represent a Chinese character

Third, operator

Variables can use + + or--to perform operations, constants can not, for example: 10++ will report unexpected type error;

+ + and--operator use: When used alone, before and after the operand

When participating in the operation, put in front of the operand, increment or decrement, and then participate in the operation, after the operand, participate in the operation, then increment or decrement

Interview question: Short s=1;s=s+1;

Short s=1;s+=1;

The above code has no problem, such as please point out and explain.

First there is a problem, the compiler will report "Precision lost" error, S is the short type, s+1 after the int type, when the value of the int type is assigned to the short value of the second no problem, because the S+=1 assignment operation implies coercion type conversion, equivalent to s= (the data type of s) (s+1 )

Four, logical operators

& and && difference: single &, the left is true or false, the right side of the operation; double & If the left is true, the right side of the operation, if the left is false, the right does not operate, double & short-circuit effect int x=2;int y=3; (++x==2&&++y==4) is false, now x becomes 3, and Y is still 3, because double & short, the right side does not perform

| and | | The difference: roughly ibid.

Xor operator ^: Same as true on both sides, false on both sides

Five, bitwise operators

&, |, ^, ~ (&, |, ^ when both sides are numeric, do a bitwise operation, when both sides are bool values do the logical operation)

& Bit and operation, there are 0 0;| bit or operation, there are 1 1;^ bit XOR operation, the same 0, different 1 (characteristics: A data is another data bit XOR or two times, the data is unchanged); ~ Bitwise negation operator

Interview questions: Int a=10;int b=20; interchange A, B value

Method one: With third-party variables

Method Two: Use bitwise XOR operator (left a, B, a, right a^b)

A=a^b;
B=a^b;//b=a^b^b =>b=a
A=a^b;//a=a^a^b =>a=b

Method Three: Using variable addition to implement

A=a+b;//a=30
b=a-b;//b=10
A=a-b;//a=20

Method Four: One sentence to take care of

b= (a+b)-(A=B);

VI. Displacement operators

<<: Left, left highest bit discarded, 0 on the right; Calculation method: The data on the left is the power of movement. Example: 3<<2 = 3*2^2=12

>>: Shift right, the highest bit is 0 left 0, the highest is 1 to the left 1; Calculation method: The left side of the data/2 of the movement of the power. Example: 24<<2 = 24/(2^2) =6

>>>: Unsigned Right SHIFT, whether the highest bit is 0 or 1, left 0

Interview question: Calculate the result of 2 times 8 in the most efficient way

2<<3 = 2*2^3=2*8

Java Beginner (ii)

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.