Data Types In Java (13 Data Types)
In Java, data types specify the type of data that a variable can hold. They are mainly divided into
two categories:
🔹 1. Primitive Data Types
These are the most basic data types in Java.
Type Size Description Example
byte 1 byte Smallest integer (-128 to 127) byte a = 100;
short 2 bytes Small integer (-32,768 to 32,767) short s = 2000;
int 4 bytes Standard integer (-2^31 to 2^31-1) int x = 100000;
long 8 bytes Large integer (-2^63 to 2^63-1) long l = 123456789L;
float 4 bytes Decimal with 6-7 digits precision float f = 5.75f;
double 8 bytes Decimal with 15 digits precision double d = 19.99;
char 2 bytes Single character (Unicode) char c = 'A';
boolean ~1 bit True or false boolean b = true;
🔹 2. Non-Primitive (Reference) Data Types
These refer to objects and are used to store complex types.
Type Description Example
String A sequence of characters (text) String name = "Java";
Arrays Collection of elements int[] arr = {1, 2, 3};
Class Blueprint for creating objects class Student { ... }
Interface Contract for classes to implement interface Drawable {}
Object Base type of all classes in Java Object obj = new Object();
🔸 Notes:
Primitive types store actual values.
Non-primitive types store references (addresses) to objects in memory.
All classes, including String, are reference types.
Imp Note
Java char is 2 bytes (16 bits) → This allows it to represent Unicode characters (not
limited to ASCII).
Unicode is a universal character encoding standard that covers all characters from all
languages (e.g., English, Hindi, Chinese, emojis, etc.).
ASCII is a subset of Unicode (only supports 128 characters: 0–127).
Operators in java.
In Java, operators are special symbols or keywords used to perform operations on variables and
values. They are grouped into several categories based on their function.
🔹 1. Arithmetic Operators
Used for basic math operations:
Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b
🔹 2. Relational (Comparison) Operators
Used to compare values:
Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
Operator Description Example
>= Greater than or equal to a >= b
<= Less than or equal to a <= b
🔹 3. Logical Operators
Used to combine boolean expressions:
Operator Description Example
&& Logical AND a > 5 && b < 10
` `
! Logical NOT !(a > 5)
🔹 4. Assignment Operators
Used to assign values to variables:
Operator Example Equivalent To
= a = 5 Assign 5
+= a += 3 a = a + 3
-= a -= 2 a = a - 2
*= a *= 4 a = a * 4
/= a /= 2 a = a / 2
%= a %= 3 a = a % 3
🔹 5. Unary Operators
Work with a single operand:
Operator Description Example
+ Unary plus +a
- Unary minus -a
++ Increment (pre/post) ++a, a++
-- Decrement (pre/post) --a, a--
! Logical NOT !true
🔹 6. Bitwise Operators
Operate on bits (used in low-level programming):
Operator Description Example
& Bitwise AND a & b
` ` Bitwise OR
^ Bitwise XOR a ^ b
~ Bitwise Complement ~a
<< Left Shift a << 2
>> Right Shift a >> 2
>>> Unsigned Right Shift a >>> 2
🔹 7. Ternary Operator
A shorthand for if-else:
condition ? value_if_true : value_if_false