Beginners Guide to Java: Understanding the Basics
Nidhi Krishna P V
Data Science | Full-Stack Development | MCA | AI & Machine Learning Enthusiast
Java is one of the most popular programming languages, known for its platform independence, object-oriented features, and robustness. If you're just starting out, this guide will walk you through the fundamental concepts of Java.
1. What is Java?
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It follows the principle of Write Once, Run Anywhere (WORA), meaning Java code can run on any device with a Java Virtual Machine (JVM).
2. Setting Up Java
To start coding in Java, you need:
3. Java Basics: Syntax & Structure
A simple Java program looks like this:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Explanation:
4. Variables & Data Types
Java has different data types to store values:
int age = 24; // Integer
double price = 99.99; // Decimal number
char grade = 'A'; // Single character
boolean isJavaFun = true; // Boolean (true/false)
String name = "Nidhi"; // String (text)
5. Control Statements
Conditional Statements:
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
Loops:
For Loop:
for (int i = 1; i <= 5; i++) {
System.out.println("Number: " + i);
}
While Loop:
int count = 1;
while (count <= 5) {
System.out.println("Count: " + count);
count++;
}
6. Object-Oriented Programming (OOP) in Java
Java is an object-oriented language, meaning it uses classes and objects. Example:
class Car {
String model;
int year;
void displayInfo() {
System.out.println("Model: " + model + ", Year: " + year);
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.model = "Tesla Model S";
myCar.year = 2023;
myCar.displayInfo();
}
}
OOP Concepts:
7. Exception Handling
To handle errors in Java, we use try-catch blocks:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
}
Conclusion
Java is a powerful language with a vast ecosystem. This guide covers only the basics, but mastering Java requires continuous learning and practice. Stay tuned for more advanced topics in upcoming editions of Code Chronicles By Nidhi!