Java Program Structure
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.'
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.
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.
Related Blueprints & Guides
View all 166 →Accessibility on the Platform
Codecademy is committed to providing a high-quality learning experience for all learners. That includes accommodating different usage forms such as auditory, motor, or visual restrictions, limited hardware, or other acce...
Web developmentAffordances, Signifiers, and Clickability
For users on the web, the mouse click is perhaps the most fundamental human-computer interaction. The webbecamethe web partially through the power ofhypertext, or text in one document that links to other documents or res...
Web developmentAuthentication and OAuth
Authenticationis the process used by applications to determine and confirm identities of users. It ensures that the correct content is shown to users. More importantly, it ensures that incorrect content is secured and u...
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.