Java Syntax Essentials: Comments, Operators, Data Types

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

OperatorUseDescription
+op1 + op2Adds op1 and op2
-op1 - op2Subtracts op2 from op1
*op1 * op2Multiplies op1 by op2
/op1 / op2Divides op1 by op2
%op1 % op2Calculates the remainder of the division of op1 by op2

Increment and Decrement Operators

OperatorUseDescription
++op++Increments op by 1; evaluates to the value before the increment (postfix)
++++opIncrements op by 1; evaluates to the value after the increment (prefix)
--op--Decrements op by 1; evaluates to the value before the decrement (postfix)
----opDecrements op by 1; evaluates to the value after the decrement (prefix)

Java Conditional Operators

Comparison Operators

OperatorUseReturns true if…
>op1 > op2op1 is greater than op2
>=op1 >= op2op1 is greater than or equal to op2
<op1 < op2op1 is less than op2
<=op1 <= op2op1 is less than or equal to op2
==op1 == op2op1 and op2 are equal
!=op1 != op2op1 and op2 are different

Logical Operators

OperatorUseReturns true if…
&&op1 && op2op1 and op2 are both true (conditionally evaluates op2)
&op1 & op2op1 and op2 are both true (always evaluates op1 and op2)
||op1 || op2Either op1 or op2 is true (conditionally evaluates op2)
|op1 | op2Either op1 or op2 is true (always evaluates op1 and op2)
!!opop is false

Java Bitwise Operators

Bitwise Shift Operators

OperatorUseOperation
>>op1 >> op2Shift the bits of op1 right op2 times (signed shift)
<<op1 << op2Shift the bits of op1 left op2 times
>>>op1 >>> op2Shift the bits of op1 right op2 times (unsigned shift / zero fill)

Bitwise Logical Operators

OperatorUseOperation
&op1 & op2Bitwise AND
|op1 | op2Bitwise OR
^op1 ^ op2Bitwise exclusive OR (XOR)
~~op2Bitwise Complement

Java Assignment Operators

Shortcut assignment operators:

OperatorUseEquivalent
+=op1 += op2op1 = op1 + op2
-=op1 -= op2op1 = op1 - op2
*=op1 *= op2op1 = op1 * op2
/=op1 /= op2op1 = op1 / op2
%=op1 %= op2op1 = op1 % op2
&=op1 &= op2op1 = op1 & op2
|=op1 |= op2op1 = op1 | op2
^=op1 ^= op2op1 = op1 ^ op2
>>=op1 >>= op2op1 = op1 >> op2
<<=op1 <<= op2op1 = op1 << op2
>>>=op1 >>>= op2op1 = op1 >>> op2

Java Primitive Data Types

Data TypeValue Range / SizeDescription
Integers
byte8-bit two’s complementByte integer
short16-bit two’s complementShort integer
int32-bit two’s complementInteger
long64-bit two’s complementLong integer
Real Numbers
float32-bit IEEE 754Single-precision floating point
double64-bit IEEE 754Double-precision floating point
Other Types
char16-bit Unicode characterA single character
booleantrue or falseA 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...
}