JAVA DATATYPES AND OPERATORS - SUMMARY
Introduction: Java is a strongly typed programming language where every variable must be
declared with a datatype. Datatypes define the type of data a variable can store and determine
memory allocation and operations that can be performed.
Types of Datatypes: Java datatypes are classified into two main categories: Primitive and
Non-Primitive (Reference) types.
Primitive Datatypes: These are basic built-in datatypes that store simple values.
1. byte: 1 byte, range -128 to 127
2. short: 2 bytes, range -32,768 to 32,767
3. int: 4 bytes, commonly used integer type
4. long: 8 bytes, used for large values
5. float: 4 bytes, single precision decimal
6. double: 8 bytes, double precision decimal
7. char: 2 bytes, stores single character
8. boolean: 1 bit, true or false
Non-Primitive Datatypes: These are reference types that store memory addresses.
Examples include: String, Arrays, Classes, Interfaces.
String: Used to store sequence of characters.
Arrays: Collection of similar data types.
Classes and Objects: Used in object-oriented programming.
Key Differences:
- Primitive types store actual values.
- Non-primitive types store references (addresses).
- Primitive types are faster and use less memory.
Operators in Java: Operators are symbols used to perform operations on variables and values.
Types of Operators:
1. Arithmetic Operators: +, -, *, /, %
Used for mathematical calculations.
2. Relational Operators: ==, !=, >, <, >=, <=
Used to compare values and return boolean.
3. Logical Operators: &&, ||, !
Used to combine conditions.
4. Assignment Operators: =, +=, -=, *=, /=
Used to assign values.
5. Unary Operators: ++, --
Used for increment and decrement.
6. Bitwise Operators: &, |, ^, ~, <<, >>
Used for bit-level operations.
Conclusion: Understanding datatypes and operators is essential in Java programming. Datatypes
define how data is stored, while operators define how data is manipulated. Mastery of these
concepts helps in writing efficient and error-free programs.