Introduction to Java
Java is a high-level, object-oriented programming language developed by Sun Microsystems.
It is platform-independent because of the Java Virtual Machine (JVM).
Why Java is popular:
- Write Once, Run Anywhere
- Strongly typed language
- Secure and robust
- Large ecosystem and community
Basic Java Example:
public class Main {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Java programs are compiled into bytecode and run on the JVM.
Java Basics
Variables in Java:
int x = 10;
String name = "John";
Data Types:
- int
- double
- char
- boolean
- String
Conditional Statements:
if (x > 5) {
[Link]("Greater than 5");
} else {
[Link]("Less or equal to 5");
}
Loops:
for (int i = 0; i < 5; i++) {
[Link](i);
}
OOP Concepts in Java
Object-Oriented Programming Concepts:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Example Class:
class Person {
String name;
Person(String name) {
[Link] = name;
}
void greet() {
[Link]("Hello " + name);
}
}
Packages:
Java uses packages to organize classes.
Example:
import [Link];
Java is widely used in web applications, Android development, and enterprise software.