Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Primitive Data types

java has 8 primitive data types

TypeSizeDescriptionDefault ValueExample Values
byte1 byte8-bit signed integer0-128, 0, 127
short2 bytes16-bit signed integer0-32768, 0, 32767
int4 bytes32-bit signed integer0-2^31, 42, 2^31-1
long8 bytes64-bit signed integer0L-2^63, 100L, 2^63-1
float4 bytes32-bit floating-point (IEEE 754)0.0f3.14f, -0.001f
double8 bytes64-bit floating-point (IEEE 754)0.0d3.14159, -2.71828
char2 bytes16-bit Unicode character‘\u0000’‘A’, ‘7’, ‘\n’
boolean~1 bitTrue or false valuefalsetrue, false

Variables

a named storage space that hold value of a specific data type. rules -

  • names are case sensitive
  • start with a letter, _, $ and can include digits.
  • local variables need explicit initializations, instance/static variables get default values.

Operators

operators are symbols that perform operations on variables and values.

arithmetic operators

OperatorDescriptionExample
+Addition5 + 3 = 8
-Subtraction5 - 3 = 2
*Multiplication5 * 3 = 15
/Division6 / 2 = 3
%Modulus (remainder)7 % 3 = 1

unary operators

OperatorDescriptionExample
++Increment by 1x++ (x = x+1)
--Decrement by 1x-- (x = x-1)
+Unary plus+5
-Unary minus-5
!Logical NOT!true = false

relational operators

OperatorDescriptionExample
==Equal to5 == 5 (true)
!=Not equal to5 != 3 (true)
>Greater than5 > 3 (true)
<Less than3 < 5 (true)
>=Greater than or equal5 >= 5 (true)
<=Less than or equal3 <= 5 (true)

logical operators

OperatorDescriptionExample
&&Logical ANDtrue && false = false
||Logcal ORtrue || false = true
!Logical NOT!true = false

bitwise operators

OperatorDescriptionExample
&Bitwise AND5 & 3 = 1
|Bitwise OR5 | 3 = 7
^Bitwise XOR5 ^ 3 = 6
~Bitwise NOT~5 = -6
<<Left shift5 << 1 = 10
>>Right shift5 >> 1 = 2
>>>Unsigned right shift5 >>> 1 = 2

assignment operators

OperatorDescriptionExample
=Assignx = 5
+=Add and assignx += 3 (x = x+3)
-=Subtract and assignx -= 3
*=Multiply and assignx *= 3
/=Divide and assignx /= 3
%=Modulus and assignx %= 3

ternary operator

OperatorDescriptionExample
?:Conditional expressionx > 0 ? "Positive" : "Non-positive"

Instanceof operator

  • Checks if an object is an instance of a class or interface.
  • Example: if (obj instanceof String) { ... }.

expressions

combination of variables, literals, operators and method call that evaluate to a single value. ex -3 + 5 , Math.sqrt(16).

statements

complete unit of execution in java. ends with ;

array

fixed sized order collection of elements of the same type. can be 1D, 2D and multi dimentional

int x = 10;
double y = 5.5;
boolean isPositive = x > 0;

double result = x * y + 3;
System.out.println("Result: " + result);

if (isPositive) {
    System.out.println("x is positive");
}

int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
    System.out.println("Number: " + numbers[i]);
}