Java Primitive Data Types and Keywords
Java's Primitive Data Types:
Java has 8 primitive data types:
1. byte - 8-bit signed integer
2. short - 16-bit signed integer
3. int - 32-bit signed integer
4. long - 64-bit signed integer
5. float - 32-bit floating point
6. double - 64-bit floating point
7. char - 16-bit Unicode character
8. boolean - true or false
Difference between int, float, and double:
1. int:
- Used to store whole numbers (integers)
- Example: int num = 25;
2. float:
- Used to store decimal numbers with single precision (less accurate)
- Must add 'f' or 'F' at the end
- Example: float price = 10.5f;
3. double:
- Used to store decimal numbers with double precision (more accurate)
- Default for decimal values
- Example: double pi = 3.14159;
Use of Keywords in Java:
Keywords are reserved words that have a predefined meaning in Java. They cannot be used as
variable names.
Examples:
1. class - Declares a class. Example: class MyClass { }
2. public - Specifies access level. Example: public void show() { }
3. static - Indicates that a method or variable belongs to the class, not instances. Example: static
int count;
4. void - Specifies that a method does not return a value. Example: public void main() { }
5. return - Used to return a value from a method. Example: return x + y;