WikiGalaxy

Personalize

Introduction to Java

What is Java?

Java is a high-level, class-based, object-oriented programming language designed to have as few implementation dependencies as possible. It is widely used for building enterprise-scale applications.

Setting Up Java Development Environment

Installing JDK

To start developing in Java, you need to install the Java Development Kit (JDK). The JDK contains the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), and other tools needed in Java development.

Writing Your First Java Program

Hello World Example

The first step in learning any programming language is writing a simple program that prints "Hello, World!" to the console. This helps you understand the basic syntax of the language.


public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
      

Understanding Java Syntax

Basic Syntax

Java syntax is the set of rules defining how a Java program is written and interpreted. It includes the use of classes, methods, variables, data types, and operators.

Variables and Data Types

Declaring Variables

Variables are containers for storing data values. Java has different types of variables, such as int, float, char, and String. Each variable has a specific data type that determines the size and layout of the variable's memory.


int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
String myText = "Hello";
      

Java Operators

Arithmetic Operators

Java provides various operators for performing arithmetic operations such as addition, subtraction, multiplication, and division.


int sum = 100 + 50;
int difference = 100 - 50;
int product = 100 * 50;
int quotient = 100 / 50;
      

Control Flow Statements

If-Else Statements

Control flow statements are used to control the order of execution of statements in a program. The if-else statement allows conditional execution of code blocks.


int number = 10;
if (number > 0) {
    System.out.println("Positive number");
} else {
    System.out.println("Negative number");
}
      

Loops in Java

For Loop

Loops are used to execute a block of code repeatedly. The for loop is commonly used when the number of iterations is known.


for (int i = 0; i < 5; i++) {
    System.out.println(i);
}
      

Java Methods

Defining Methods

Methods are blocks of code that perform a specific task and are executed when called. They help in code reusability and organization.


public class MyClass {
    public static void myMethod() {
        System.out.println("Hello from a method");
    }
    public static void main(String[] args) {
        myMethod();
    }
}
      

Object-Oriented Programming

Classes and Objects

Java is an object-oriented programming language, which means it uses objects and classes to structure the code. A class is a blueprint for objects, and an object is an instance of a class.


public class Car {
    String color;
    int year;

    public Car(String color, int year) {
        this.color = color;
        this.year = year;
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Red", 2020);
        System.out.println("Car color: " + myCar.color);
    }
}
      
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025