Java Syntax Essentials: Comments, Operators, Data Types
Posted on Apr 6, 2025 in Social Anthropology
Java Fundamentals: Syntax and Operators
Java Comments Explained
In Java, there are three types of comments:
// Single-line comments
/* Comments of one or more lines */
/** Documentation comment, one or more lines */
Java Identifiers: Naming Rules
Identifiers are used for naming variables, functions, classes, objects, or anything else the programmer needs to identify or use.
In Java, an identifier must begin with a letter, an underscore (_
), or a dollar sign ($
). Subsequent characters can be letters or digits. Java distinguishes between uppercase and lowercase letters (case-sensitive), and there is no maximum length for an identifier.
Valid identifiers include:
ID
username
User_Name
_variable_del_sistema
$Transaction
Example usage:
int contador_principal;
char _lista_de_ficheros;
float $cantidad_en_Ptas;
Java Arithmetic Operators
Basic Arithmetic
Operator | Use | Description |
---|
+ | op1 + op2 | Adds op1 and op2 |
- | op1 - op2 | Subtracts op2 from op1 |
* | op1 * op2 | Multiplies op1 by op2 |
/ | op1 / op2 | Divides op1 by op2 |
% | op1 % op2 | Calculates the remainder of the division of op1 by op2 |
Increment and Decrement Operators
Operator | Use | Description |
---|
++ | op++ | Increments op by 1; evaluates to the value before the increment (postfix) |
++ | ++op | Increments op by 1; evaluates to the value after the increment (prefix) |
-- | op-- | Decrements op by 1; evaluates to the value before the decrement (postfix) |
-- | --op | Decrements op by 1; evaluates to the value after the decrement (prefix) |
Java Conditional Operators
Comparison Operators
Operator | Use | Returns true if… |
---|
> | op1 > op2 | op1 is greater than op2 |
>= | op1 >= op2 | op1 is greater than or equal to op2 |
< | op1 < op2 | op1 is less than op2 |
<= | op1 <= op2 | op1 is less than or equal to op2 |
== | op1 == op2 | op1 and op2 are equal |
!= | op1 != op2 | op1 and op2 are different |
Logical Operators
Operator | Use | Returns true if… |
---|
&& | op1 && op2 | op1 and op2 are both true (conditionally evaluates op2) |
& | op1 & op2 | op1 and op2 are both true (always evaluates op1 and op2) |
|| | op1 || op2 | Either op1 or op2 is true (conditionally evaluates op2) |
| | op1 | op2 | Either op1 or op2 is true (always evaluates op1 and op2) |
! | !op | op is false |
Java Bitwise Operators
Bitwise Shift Operators
Operator | Use | Operation |
---|
>> | op1 >> op2 | Shift the bits of op1 right op2 times (signed shift) |
<< | op1 << op2 | Shift the bits of op1 left op2 times |
>>> | op1 >>> op2 | Shift the bits of op1 right op2 times (unsigned shift / zero fill) |
Bitwise Logical Operators
Operator | Use | Operation |
---|
& | op1 & op2 | Bitwise AND |
| | op1 | op2 | Bitwise OR |
^ | op1 ^ op2 | Bitwise exclusive OR (XOR) |
~ | ~op2 | Bitwise Complement |
Java Assignment Operators
Shortcut assignment operators:
Operator | Use | Equivalent |
---|
+= | op1 += op2 | op1 = op1 + op2 |
-= | op1 -= op2 | op1 = op1 - op2 |
*= | op1 *= op2 | op1 = op1 * op2 |
/= | op1 /= op2 | op1 = op1 / op2 |
%= | op1 %= op2 | op1 = op1 % op2 |
&= | op1 &= op2 | op1 = op1 & op2 |
|= | op1 |= op2 | op1 = op1 | op2 |
^= | op1 ^= op2 | op1 = op1 ^ op2 |
>>= | op1 >>= op2 | op1 = op1 >> op2 |
<<= | op1 <<= op2 | op1 = op1 << op2 |
>>>= | op1 >>>= op2 | op1 = op1 >>> op2 |
Java Primitive Data Types
Data Type | Value Range / Size | Description |
---|
Integers |
byte | 8-bit two’s complement | Byte integer |
short | 16-bit two’s complement | Short integer |
int | 32-bit two’s complement | Integer |
long | 64-bit two’s complement | Long integer |
Real Numbers |
float | 32-bit IEEE 754 | Single-precision floating point |
double | 64-bit IEEE 754 | Double-precision floating point |
Other Types |
char | 16-bit Unicode character | A single character |
boolean | true or false | A boolean value (true or false) |
Java Control Flow Statements
Selection Statements
If Statement
if (condition) {
// statement(s) if condition is true
}
If-Else Statement
if (condition) {
// statement(s) if condition is true
} else {
// statement(s) if condition is false
}
Switch Statement
switch (variable) {
case value1:
// Instructions
break;
case value2:
// Instructions
break;
// ... more cases
default:
// Instructions if no case matches
}
Loop Statements
For Loop
for (initialization; termination_condition; increment/decrement) {
// statement(s) to repeat
}
Do-While Loop
initialization;
do {
// statement(s) to repeat
increment/decrement;
} while (condition);
While Loop
initialization;
while (condition) {
// statement(s) to repeat
increment/decrement;
}
Alternative while loop structure:
while (boolean_expression) {
// instructions...
}