Java Basics for Beginners
Introduction to Java
Java is a high-level, class-based, object-oriented programming language that is designed to be easy for
developers to learn and write. It is used to create applications for various platforms. Java is
platform-independent because the compiled code (bytecode) runs on any machine that has the Java Virtual Machine
(JVM).
1. Variables and Data Types
Variables in Java are used to store data. Each variable must be declared with a specific data
type.
- int: Stores integers (whole numbers), e.g., int num = 5;
- double: Stores floating-point numbers, e.g., double price = 10.99;
- char: Stores single characters, e.g., char grade = 'A';
- boolean: Stores true or false values, e.g., boolean isJavaFun = true;
- String: Stores a sequence of characters, e.g., String name = "Java";
Example:
int age = 20;
double temperature = 36.5;
2. Operators
Operators in Java are used to perform operations on variables and values.
- Arithmetic Operators: +, -, *, /, %, ++, --
Example: int sum = 5 + 3; // sum is 8
- Comparison Operators: ==, !=, >, <, >=, <=
Example: boolean isEqual = (5 == 3); // false
- Logical Operators: && (AND), || (OR), ! (NOT)
Example: boolean result = (5 > 3 && 5 < 10); // true
3. Control Flow
Control flow statements help you control the flow of execution in a program.
- if statement:
if (condition) {
// code to execute if condition is true
- else if and else:
if (condition1) {
// code for condition1
} else if (condition2) {
// code for condition2
} else {
// code if both conditions are false
- while loop:
while (condition) {
// code to be executed while the condition is true
}
- for loop:
for (initialization; condition; update) {
// code to be executed
Example:
for (int i = 0; i < 5; i++) {
System.out.println(i);
4. Arrays
An array is a collection of elements of the same type stored in a single variable. You access
elements by their index.
Syntax:
dataType[] arrayName = new dataType[arraySize];
Example:
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[0]); // Output: 1
Arrays are zero-indexed, meaning the first element is at index 0.
You can also use loops to iterate through arrays:
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
5. Methods (Functions)
A method is a block of code that performs a specific task. It is reusable and helps organize
code.
Syntax:
returnType methodName(parameters) {
// code to execute
return value; // if required
Example:
public static int addNumbers(int a, int b) {
return a + b;
To call a method, you use:
int result = addNumbers(5, 3);
6. Introduction to OOP
Object-Oriented Programming (OOP) is a programming paradigm that is based on the
concept of 'objects'.
Key concepts:
- **Class**: A blueprint for creating objects. It defines properties (variables) and methods
(functions).
Example:
class Car {
String model;
int year;
void start() {
System.out.println("Car started.");
- **Object**: An instance of a class.
Example:
Car myCar = new Car();
- **Encapsulation**: Bundling data and methods that operate on the data within a class.
- **Inheritance**: A way to create new classes from existing ones.
- **Polymorphism**: Ability of methods to do different things based on the object they are
called on.