PRO 30% Free • Stripe ($15) & Razorpay (₹899)

Java Program Structure

Top Course Staff · 2 min read · Web development · PRO · 30% Free Preview Available

Java Program Structure

Java programs have a specific structure in how the code is written. There are key elements that all Java programs share.

The Program

We have the text of a program inside the file calledHelloWorld.java.

// This program outputs the message "Hello World!" to the monitor
public class HelloWorld {  public static void main(String[] args) {    System.out.println("Hello World!");  }}
Copy to clipboard
// This program outputs the message "Hello World!" to the monitor
public class HelloWorld {  public static void main(String[] args) {    System.out.println("Hello World!");  }}

This program writesHello World!to your terminal when run.

Case-Sensitivity

Java is a case-sensitive language. Case sensitivity means thatsyntax, the words our computer understands, must match the case. For example, the Java command for outputting text to the screen isSystem.out.println(). If you were to typesystem.out.println()orSystem.Out.println(), the compiler would not know that your intention was to useSystemorout.

Let’s go over thisHelloWorld.javaprogram line by line:

Comments

// This program outputs the message "Hello World!" to the monitor
Copy to clipboard
// This program outputs the message "Hello World!" to the monitor

This is a single-line comment that documents the code. The compiler will ignore everything after//to the end of the line. Comments provide information outside the syntax of the language.

Classes

public class HelloWorld {   // class code}
Copy to clipboard
public class HelloWorld {   // class code}

This is the class of the file. All Java programs are made of at least one class. The class name must match the file: our file isHelloWorld.javaand our class isHelloWorld. We capitalize every word, a style known aspascal case. Java variables and methods are named in a similar style calledcamel casewhere every wordafter the firstis capitalized.

The curly braces,{and}, mark thescopeof the class. Everything inside the curly braces is part of the class.

Methods

public static void main(String[] args) {   // Statements  }
Copy to clipboard
public static void main(String[] args) {   // Statements  }

Every Java program must have a method calledmain(). A method is a sequence of tasks for the computer to execute. Thismain()method holds all of the instructions for our program.

Statements

System.out.println("Hello World!");
Copy to clipboard
System.out.println("Hello World!");

This code uses the methodprintln()to send the text “Hello World!” to the terminal as output.println()comes from anobjectcalledout, which is responsible for various types of output. Objects are packages of state and behavior, and they’re often modeled on real-world things.

outis located withinSystem, which is another object responsible for representing our computer within the program! We can access parts of an object with a., which is known asdot notation.

This line of code is astatement, because it performs asingletask. Statements always conclude with a semicolon.

Whitespace

Java programs allow judicious use of whitespace (tabs, spaces, newlines) to create code that is easier to read. The compiler ignores whitespace, but humans need it! Use whitespace to indent and separate lines of code. Whitespace increases the readability of your code.

Practice

The structure of a Java program will feel familiar the more you work with this language. Continue learning at Codecademy and you’ll be a Java pro in no time!

  • Twitter Java Style Guide
  • Google Java Style Guide

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

30% Free Preview Reached · Unlock Remaining 70%

Complete Payment to Unlock Full Guide & Source Code

You are viewing the 30% preview. Join TopCourseAI Pro to unlock the remaining 70% of this article, unblur all code snippets, and gain full access to all 412 technical projects and guides.

✓ Full Access to All 160+ Projects
✓ Instant Blur Removal Everywhere
✓ Production Source Code & Blueprints
✓ All Future Deep Dives Included
Instant Unhindered Activation ·
End of Preview · Unlock Complete Blueprint & Source Code

Ready to access the complete step-by-step tutorial?

Gain full unhindered access to all 412 technical projects and guides, unblur all source code snippets, and download runnable scripts.

Production AI Blueprints

Ready to build production-grade AI systems?

Enjoyed this guide? Continue exploring our library of 166+ production blueprints, multi-agent swarms, and autonomous agent systems with 100% unblurred, executable source code.