Getting Started with Java: A Beginnerβs Guide π
First-Time Java Programming: A Simple Guide
Table of contents
- 1οΈβ£ What is Java?
- 2οΈβ£ Installing Java on Your System
- 3οΈβ£ Writing Your First Java Program
- 4οΈβ£ Java Basics: Understanding Syntax & Structure
- 5οΈβ£ Java Variables & Data Types
- 6οΈβ£ Operators in Java
- 7οΈβ£ Control Flow Statements
- 8οΈβ£ Functions in Java
- 9οΈβ£ Object-Oriented Programming (OOP) in Java
- πΉ Summary & Next Steps
- π Best Resources to Learn Java
Java is one of the most popular programming languages, widely used for web development, mobile applications, enterprise software, and more. Whether you're a beginner or transitioning from another language, this guide will help you understand the fundamentals and get started with Java programming.
1οΈβ£ What is Java?
Java is a high-level, object-oriented programming language developed by James Gosling at Sun Microsystems in 1995 (now owned by Oracle). It follows the WORA (Write Once, Run Anywhere) principle, meaning Java programs can run on any system with a Java Virtual Machine (JVM).
π Key Features of Java
β
Platform Independent: Runs on any OS using JVM.
β
Object-Oriented: Uses classes and objects for structure.
β
Robust & Secure: Strong memory management, exception handling, and security features.
β
Multi-threading Support: Enables parallel execution for better performance.
β
Automatic Garbage Collection: Manages memory allocation and deallocation.
2οΈβ£ Installing Java on Your System
To start coding in Java, you need to install the Java Development Kit (JDK). The JDK includes the compiler (javac
), runtime (java
), and other tools for Java development.
π Steps to Install Java
Download the latest JDK from the Oracle website or OpenJDK.
Install the JDK and set the Environment Variables (
JAVA_HOME
andPath
) on your system.Verify installation by running:
java -version javac -version
If Java is installed correctly, it will display the version information.
3οΈβ£ Writing Your First Java Program
Now that you have Java installed, let's write and run your first program.
π Hello World in Java
Create a file named HelloWorld.java
and write the following code:
// Class Declaration
public class HelloWorld {
// Main Method (Entry Point)
public static void main(String[] args) {
System.out.println("Hello, World!"); // Print Output
}
}
π How to Compile and Run the Program
Compile the Code:
javac HelloWorld.java
This generates a
HelloWorld.class
file (bytecode).Run the Program:
java HelloWorld
Output:
Hello, World!
4οΈβ£ Java Basics: Understanding Syntax & Structure
π Java Program Structure
Let's break down each component of the Java program structure and explain the keywords used:
// Class Declaration
public class HelloWorld {
// Main Method (Entry Point)
public static void main(String[] args) {
System.out.println("Hello, World!"); // Print Output
}
}
Each Java program consists of:
β
Class: Every program is inside a class (HelloWorld
in our example).
β
Main Method (main
): The entry point for execution.
β
Statements: Java uses semicolons (;
) to end statements.
β
Curly Braces {}
: Defines blocks of code.
1. Class Declaration
public
: An access modifier. It means the class is accessible from other classes.class
: Keyword used to define a class.HelloWorld
: The name of the class. By convention, class names should start with an uppercase letter.
2. Main Method (Entry Point)
public
: An access modifier. It means the method is accessible from other classes.static
: This means the method belongs to the class rather than instances of the class. It can be called without creating an object of the class.void
: The return type of the method. It means the method does not return any value.main: The name of the method. This is the entry point for any Java program.
String[] args
: An array ofString
arguments passed to the method. It allows the program to accept command-line arguments.
3. Statements
System.out.println("Hello, World!");
: This statement prints the text "Hello, World!" to the console.System
: A class in the java.lang package.out
: A static member of theSystem
class, which is an instance ofPrintStream
.println
: A method of thePrintStream
class that prints the argument passed to it followed by a new line.
4. Curly Braces {}
{ β¦ }
: Curly braces are used to define the beginning and end of a block of code. In this case, they define the blocks for the class and the main method.
5οΈβ£ Java Variables & Data Types
Java is a statically typed language, meaning every variable must have a defined data type.
π Variable Declaration
int age = 25; // Integer variable
double price = 99.99; // Decimal number
char grade = 'A'; // Single character
boolean isJavaFun = true; // Boolean value
String name = "Deepak"; // String (text)
π Primitive Data Types
Data Type | Size | Example | Range |
boolean | 1 bit | Boolean flag = true; | true or false |
byte | 1 byte (8 bits) | byte b = 127; | -128 to 127 |
char | 2 bytes (16 bits) | char letter = 'A'; | 0 to 65,536 |
short | 2 bytes (16 bits) | short s = 32000; | -2^15 to 2^15-1 |
int | 4 bytes (32 bits) | int num = 100; | -2^31 to 2^31-1 |
long | 8 bytes (64 bits) | long bigNum = 100000L; | -2^63 to 2^63-1 |
float | 4 bytes (32 bits) | float pi = 3.14f; | 3.4e-038 to 3.4e+038 |
double | 8 bytes (64 bits) | double price = 99.99; | 1.7e-308 to 1.7e+308 |
πΉ Note: String
is not a primitive type but is widely used for text data.
6οΈβ£ Operators in Java
Java provides various operators for performing arithmetic, comparison, and logical operations.
π What are Operators?
Operators are special symbols that perform operations on variables and values.
π Types of Operators
π Arithmetic Operators
Used for mathematical operations like addition, subtraction, multiplication, etc.
int a = 10, b = 5;
System.out.println(a + b); // Addition (+)
System.out.println(a - b); // Subtraction (-)
System.out.println(a * b); // Multiplication (*)
System.out.println(a / b); // Division (/)
System.out.println(a % b); // Modulus (%)
π Relational (Comparison) Operators
Used to compare values and return true
or false
.
System.out.println(a > b); // true
System.out.println(a == b); // false
System.out.println(a != b); // true
π Logical Operators
Used to perform logical operations.
System.out.println((a > b) && (a > 0)); // true (AND)
System.out.println((a < b) || (a > 0)); // true (OR)
7οΈβ£ Control Flow Statements
π If-Else Condition
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
π Switch Case
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other Day");
}
π Loops in Java
π For Loop
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
π While Loop
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
8οΈβ£ Functions in Java
In Java, functions (or methods) are blocks of code that perform specific tasks and can be called upon to execute when needed.
π Defining and Calling Functions
A function is defined within a class. Also, a function defined within a class is known as method. So, every function is method in Java. To call a function, you simply use its name followed by parentheses containing the arguments, if any.
public class MathOperations {
// Function Definition
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(10, 20);
System.out.println("Sum: " + result);
}
}
public static int add(int a, int b)
: This is the function header.public
: Access modifier.static
: Indicates that the method belongs to the class, not an instance of it.int
: Return type of the method.add
: Name of the method.(int a, int b)
: Parameters the method accepts.
return a + b;
: The body of the method, which defines what the method does.
9οΈβ£ Object-Oriented Programming (OOP) in Java
Java is an object-oriented programming (OOP) language. It follows the principles of:
β
Encapsulation: Wrapping data and methods into a single unit (class).
β
Inheritance: Acquiring properties from another class.
β
Polymorphism: One interface, multiple implementations.
β
Abstraction: Hiding implementation details.
π Example of a Class and Object
class Car {
String brand = "Tesla";
void honk() {
System.out.println("Beep Beep!");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
System.out.println(myCar.brand);
myCar.honk();
}
}
πΉ Summary & Next Steps
β
Java is a platform-independent, object-oriented, and secure language.
β
We learned about syntax, data types, operators, control flow, functions, and OOP.
β
The next step is to practice coding and explore advanced topics like Collections, Exception Handling, and Java Frameworks.
π Best Resources to Learn Java
π Java Documentation: Oracle Docs
π Online Practice: LeetCode Java
π Video Tutorials: Java by CodeWithHarry
I will cover specific Java topics in further blogs. Do let me know if you have any suggestions! π