Java Note - Operators

本文详细介绍了Java中的各类运算符,包括算术运算符、赋值运算符、比较运算符、逻辑运算符、位运算符及移位运算符等,并解释了它们的功能与使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

An operator takes one or more arguments and produces a new value.

Precedence

Operator precedence defines how an expression evaluates when several operators are present.

Assignment

Assignment is performed with the operator “=”, it means “Take the value of the right-hand side and copy it into the left-hand side.”

The primitive holds the actual value and not a reference to an object.
When you assign “from one object to another”, you are actually copying a reference from one place to another.

Mathematical Operator

The basic mathematical operators

addition (+)
subtraction (-)
multiplication (*)
division (/) Integer division truncates, rather than rounds.
modulus (%)

Java uses the shorthand notation from C/C++ that performs an operation and an assignment at the same time.

+=
-=
*=
/=
%=

Unary minus and plus operators

minus (-) inverts the sign on the data
plus (+)

Auto increment and decrement

increment (++) increase by one unit
decrement (--) decrease by one unit

String operator

+ if an expression begins with a String, then all operands that follow must be Strings.
+=

Relational operators

Relational operators generate a boolean result.

less than (<)
greater than (>)
less than or equal to (<=)
greater than or equal to (>=)
equivalent (==)
not equivalent (!=)

Logical operators

Logical operators produces a boolean result

AND (&&)
OR (||)
NOT (!)

Short-circuiting
The expression will be evaluated only until the truth or falsehood of the entire expression can be unambiguously determined. As a result, the latter parts of a logical expression migth not be evaluated.

A boolean value is automatically converted to an appropriate text form if it is used where a String is expected.

Bitwise operators

The bitwise operators allow you to manipulate individual bits in an integral primitive date type.
Bitwise operatos perform Boolean algebra on the corresponding bits in the two arguments to produce the result.

bitwise AND (&)
bitwise OR (|)
bitwise NOT (~)
bitwise XOR (^)

Shift operators

The shift operators also manipulate bits.

left-shift operator (<<)
signed right-shift operator (>>) if the value is positive, zeroes are inserted at the higher-order bits; if negative, ones are inserted.
unsigned right-shift operator (>>>) regardless of the sign, zeroes are inserted at the higher-order bits.

Shifts can be combined with the equal sign (shorthand notation)

<<=
>>=
>>>=

Ternary if-else operator

The ternary operator, also called the conditional operator, is unusual because it has three operands.

boolean-exp ? true-value : false-value

The ternary operator is different from if-else because it produces a value.

Literals

When you insert a literal value into a program, the complier knows exactly what type to make it.
A trailing character after a literal value establishes its type.

L (long)
F (float)
D (double)
0 (octal)
0x (hexadecimal)

Casting

Casting from float to integer value always truncates the number.

The largest data type in an expression is the one that determines the size of the result of that expression.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值