Starting Java

Our Basic Java Program Boilerplate

// Inside Main.java
class Main {
    public static void main(String[] args) {
        // Your java code goes here.
    }
}

Boilerplate

Boilerplate code, also known as boilerplate, is the part of code that has to be repeatedly in use with no or a little modification.

How To Use Our Boilerplate

  1. For every replit program we create, we will be coding inside our Main.java file.

  2. You can copy the boilerplate to help you start your code.

  3. Do not modify any code given in the boilerplate, only code in the section under "your java code goes here."

  4. After you have programmed, you can run your code by clicking the run button at top.

Our First Java Program

// Inside Main.java
class Main {
    public static void main(String[] args) {
        // Your java code goes here.
        System.out.println("Hello, World!");
    }
}

This program is written in Java to output the message Hello, World! into the console.

System.out.println("Hello, World!");

The code above is a way to output a text message to the program's console.

Key Points for Programming Java.

  1. Capitalization Matters: System != system

  2. Indentations Matter: It should be 4 spaces per indentation and it should be consistent for all indentations in your code.

// GOOD:
public static void main(String[] args) {
    // Your java code goes here.
    System.out.println("Hello, World!");
}
// BAD:
public static void main(String[] args) {
  // Your java code goes here.
system.out.printn("Hello, World!");
}
  1. Most lines in Java ends with a semicolon ;.

  2. Spelling matters: println vs. printn

  3. The label after class should be the same as the file's name

// I am assuming your file is called Main.Java

import java.util.Scanner; // This will allow us to do user inputs

class Main {
    public static void main(String[] args) {
        // Create your scanner object
        Scanner sc = new Scanner(System.in);
        
        // Write your code here.
        
        sc.close(); // Always close your scanner object.
    }
}

What is Java?

Java is a compiled, versatile, high-level, and object-oriented programming language known for its platform independence, robustness, and wide range of applications.

Developers write Java code and then compile it into platform-neutral bytecode using a Java compiler.

This bytecode can be executed on various platforms and devices by the Java Virtual Machine (JVM). Java is renowned for its strong support for multi-threading, extensive library ecosystem, and the "write once, run anywhere" philosophy, making it a popular choice for web development, mobile app development, enterprise software, and more.

Last updated