Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Java Operators

Operators are special characters used to manipulate operands. Operators that perform an operation on one operand are called unary. Operators that perform an operation on two operands are called binary. Operators that perform an operation on three operands are called ternary.

Arithmetic Operators

If variables a and b are integer types and a holds 8 and b holds 4:

OperatorNameDescriptionExample
+additionAdds valuesa + b result is 12
-subtractionSubtracts valuea - b result is 4
*multiplicationMultiplies valuesa * b result is 32
/divisionDivides valuea / b result is 2
%modulusDivides value returns remaindera % b result is 0
--decrementDecreases value by 1a-- result is 7
++incrementIncreases value by 1a++ result is 9

Relational Operators

If variables a and b are integer types and a holds 8 and b holds 4:

OperatorNameDescriptionExample
==equal toif values are equal result is true, if not equal resut is falsea == b is false
!=not equal toif values are not equal result is true, if values are equal result is falsea != b is true
>greater thanif value of left operand is greater than value of right operand, result is true, if not resut is falsea > b is true
<less than if value of left operand is less than value of right operand, result is true, if not resut is falsea < b is false
>=greater than or equal toif value of left operand is greater than or equal to value of right operand, result is true, if not resut is falsea >= b is true
<=less than or equal toif value of left operand is less than or equal to the value of right operand, result is true, if not resut is falsea <= b is false

Bitwise Operators

Bitwise operators work on bits and performs bit-by-bit operation. Bitwise operators can be applied to the integer, long, int, short, char, and byte variable types.

If variable a = 0011 1100 binary and variable b = 0000 1101 binary:

OperatorNameDescriptionExample
&bitwise ANDbinary ANDa & b result is 0000 1100
|bitwise ORbinary ORa | b result is 0011 1101
^bitwise XORbinary XORa ^ b result is 0011 0001
~bitwise complimentbinary ones complement~a result is 1100 0011
<<left shiftbinary shift lefta << 2 result is 1111 0000
>>right shiftbinary shift righta >> 2 result is 1111 1111
>>>shift right zero fillshift right zero filla >>> 2 result is 0000 1111

When you shift right (>>), the leftmost bits exposed by the shift are filled in with the previous contents of the leftmost bit This serves to preserve the sign of negative numbers when you shift right and is called sign extension. That's why we need shift right zero fill, in case you whast the the leftmost bits filled with zeros.

Logical Operators

If variable a holds true and variable b holds false:

OperatorNameDescriptionExample
&&logical ANDif both operands are non-zero, the condition becomes truea && b is false
||logical ORif any of the two operands are non-zero, the condition becomes truea || b is true
!logical NOTif condition is true, result is false. If condition is false result is true!(a && b) is true

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro


Java

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2024 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268