Java Basic Tutorial



Introduction to Java

Java is a general-purpose, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is widely used for building various types of applications, including desktop, web, and mobile applications.

Hello World Example


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

Data Types

Java provides several built-in data types, including:

  • int: used to store whole numbers
  • double: used to store floating-point numbers
  • String: used to store text
  • boolean: used to store boolean values (true or false)

Control Structures

Java includes various control structures for decision-making and looping:

  • if statement: executes a block of code if a certain condition is true
  • for loop: repeats a block of code a specified number of times
  • while loop: repeats a block of code while a certain condition is true

Functions

In Java, you can define functions (methods) to encapsulate reusable blocks of code:


public class Calculator {
    public static void main(String[] args) {
        int result = add(3, 5);
        System.out.println("The result is: " + result);
    }

    public static int add(int a, int b) {
        return a + b;
    }
}
  

Conclusion

This was a brief introduction to Java and some of its basic concepts. Java is a versatile programming language that offers a wide range of features for developing robust applications. Continue exploring to expand your Java knowledge!