java has 8 primitive data types
Type Size Description Default Value Example Values
byte1 byte 8-bit signed integer 0 -128, 0, 127
short2 bytes 16-bit signed integer 0 -32768, 0, 32767
int4 bytes 32-bit signed integer 0 -2^31, 42, 2^31-1
long8 bytes 64-bit signed integer 0L -2^63, 100L, 2^63-1
float4 bytes 32-bit floating-point (IEEE 754) 0.0f 3.14f, -0.001f
double8 bytes 64-bit floating-point (IEEE 754) 0.0d 3.14159, -2.71828
char2 bytes 16-bit Unicode character ‘\u0000’ ‘A’, ‘7’, ‘\n’
boolean~1 bit True or false value false true, false
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 are symbols that perform operations on variables and values.
Operator Description Example
+Addition 5 + 3 = 8
-Subtraction 5 - 3 = 2
*Multiplication 5 * 3 = 15
/Division 6 / 2 = 3
%Modulus (remainder) 7 % 3 = 1
Operator Description Example
++Increment by 1 x++ (x = x+1)
--Decrement by 1 x-- (x = x-1)
+Unary plus +5
-Unary minus -5
!Logical NOT !true = false
Operator Description Example
==Equal to 5 == 5 (true)
!=Not equal to 5 != 3 (true)
>Greater than 5 > 3 (true)
<Less than 3 < 5 (true)
>=Greater than or equal 5 >= 5 (true)
<=Less than or equal 3 <= 5 (true)
Operator Description Example
&&Logical AND true && false = false
||Logcal OR true || false = true
!Logical NOT !true = false
Operator Description Example
&Bitwise AND 5 & 3 = 1
|Bitwise OR 5 | 3 = 7
^Bitwise XOR 5 ^ 3 = 6
~Bitwise NOT ~5 = -6
<<Left shift 5 << 1 = 10
>>Right shift 5 >> 1 = 2
>>>Unsigned right shift 5 >>> 1 = 2
Operator Description Example
=Assign x = 5
+=Add and assign x += 3 (x = x+3)
-=Subtract and assign x -= 3
*=Multiply and assign x *= 3
/=Divide and assign x /= 3
%=Modulus and assign x %= 3
Operator Description Example
?:Conditional expression x > 0 ? "Positive" : "Non-positive"
Checks if an object is an instance of a class or interface.
Example: if (obj instanceof String) { ... }.
combination of variables, literals, operators and method call that evaluate to a single value. ex -3 + 5 , Math.sqrt(16).
complete unit of execution in java. ends with ;
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]);
}