Java Basics-Operator

Source: Internet
Author: User

1, the integer by 0 will produce an exception, and the floating-point number will be 0 in addition to the result of infinity or Nan

2, for example: double w = x * y/z Java is a portable language, but when it comes to calculations, because 64 bits store a double value and 80-bit floating-point registers store a double value is not the same, Many Intel processors are calculated one step at a time (so that the results are more accurate), and the final result is truncated to 64 bits, which is different from the results of the 64-bit machine calculations (direct settlement, no intermediate process), a single operation of a storage, there may be overflow or slow calculation of the situation, The Java programming language acknowledges the conflicts between the optimal performance and the ideal result, and makes improvements. By default, the virtual machine designer allows the intermediate calculation results to be extended precision, but for methods marked with the STRICTFP keyword, strict floating-point calculations must be used to produce the desired result. For example: public static strictfp void Main (string[] args), so all floating-point computations in the main method are strictly computed.

3, self-increment operator, self-decrement operator

The prefix method is first +1 and then the suffix is +1 after the first operation.

4. Relational operators and Boolean operators

&& Express Logical "and", | | | Represents the logical "or",! Representations of logical "not" they operate in a "short circuit" manner (that is, once the first operand is able to determine the value of the expression, the second is not necessarily calculated)

Ternary operator: A?B:C

5, bitwise operator:& ("and"), | ("or"), ^ ("xor"), ~ ("non")

Bitwise operators are primarily for binary

When working with integer values, you can directly manipulate the individual bits that make up an integer value, which means that each bit in the integer can be obtained using the masking technique

The & and | operators are applied to Boolean values, and the resulting result is also a Boolean value, and the method of operation and &&, | | Different, they do not calculate by short-circuit, but must calculate the value of the number of two operations

(1) & ("and"): Two operands are 1, the result is 1, otherwise 0

For example: int a = 129;

int b = 128;

SYSTEM.OUT.PRINTLN ("Output result is" + (a&b)); The result is the value of "a" is 129, the conversion to binary is 10000001, and the value of "B" is 128, converted to binary is 10000000. So know the result is 10000000, that is, 128.

(2) | ("or"): two operands only one is 1, the result is 1, otherwise 0

For example: int a=129;
int b=128;

SYSTEM.OUT.PRINTLN ("Output result is" + (a|b)); Result is 129

(3) ^ ("xor"): Two of the operands are the same 0, the difference is 1

For example: int a = 15;

int b = 2;

SYSTEM.OUT.PRINTLN ("The result of the output is" + (a^b)); The result is that the value of "a" is 15, converted to binary 1111, and the value of "B" is 2, converted to binary 0010, so it can be concluded that the result is 1101 or 13.

(4) ~ (non-operator) if the bit is 1, the result is 0, and if the bit is 0, the result is 1

For example: int a = 2;

SYSTEM.OUT.PRINTLN ("A non-result is:" + (~a));

6. Shift operator: << shift left, >> right shift >>> unsigned right shift

(bitwise operators are computationally faster than multiplication operations in arithmetic calculations)

Shift left N is equal to the N power (low with 0), the right shift n is equal to/2 of the N power (if the value is positive, then the high is 0, if the value is negative, then the high position of 1.), unsigned right shift, ignore the sign bit, all the slots are filled with 0

7. Mathematical Functions and constants:

The header file for the Math method (static method) is: import static java.lang.math.*;

Common trigonometric functions provided by the Math class: Math.sin math.cos math.tan Math.atan math.atan2

The Math class provides an exponential function and its inverse function-the natural logarithm and the base 10 logarithm: Math.exp Math.log math.log10

There is no power operation in Java, and the Pow method in the math class is required. For example: double y = Math.pow (x,a);

Java also provides two approximate values to represent π (PAI) and E constants: Math.PI MATH.E

8. Conversions between numeric types:

Low accuracy--high accuracy no information loss

High accuracy--high accuracy there may be information loss

Methods for forcing type conversions: Math.Round (Follow rounding)

9. Enumeration type:

Enum Size{small, Medium,large,extra_large};

Size s = size.medium;

A variable of type size can only store an enumeration value given in the type declaration, or a null value, and NULL means that the variable has no value set.

  

    

Java Basics-Operator

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.