Java is a high-level, object-oriented programming language known for its
portability, performance, and wide usage in enterprise applications, mobile
applications (via Android), and large-scale systems. This guide covers the essential
concepts from Java basics to advanced topics.
1. Introduction to Java
Java was developed by James Gosling and Mike Sheridan at Sun Microsystems in
1991 and later acquired by Oracle. It is platform-independent and follows the
Write Once, Run Anywhere (WORA) philosophy, meaning code can be written on
one platform and run on any other platform that has a Java Virtual Machine
(JVM).
Key Features of Java:
Object-Oriented: Focuses on objects and classes, promoting reusability.
Platform Independent: Java code can run on any platform with a JVM.
Multithreaded: Supports concurrent programming.
Robust and Secure: Offers strong error handling and security features.
Rich API: Extensive libraries for networking, I/O, and more.
2. Variables and Data Types
In Java, variables are used to store data, and data types define the kind of data
they can hold.
Primitive Data Types:
byte: 8-bit integer
short: 16-bit integer
int: 32-bit integer
long: 64-bit integer
, float: 32-bit floating-point
double: 64-bit floating-point
char: Single 16-bit Unicode character
boolean: True or false
Non-Primitive Data Types:
String: A sequence of characters.
Array: A collection of similar elements.
Object: A collection of related data (key-value pairs) and methods.
Example:
int age = 25;
String name = "John";
boolean isActive = true;
3. Operators
Java has several operators for performing operations on variables and values.
Types of Operators:
Arithmetic Operators: +, -, *, /, %
Relational Operators: ==, !=, <, >, <=, >=
Logical Operators: &&, ||, !
Assignment Operators: =, +=, -=, *=, /=
Bitwise Operators: &, |, ^, ~, <<, >>
Ternary Operator: condition ? expr1 : expr2
Example:
int x = 10;
int y = 5;
int sum = x + y; // 15