Hello World!
My name is Andre da Silva, and I just graduated from the University of Illinois Springfieldsumma cum laude with a bachelor’s in computer science! I’ve learned so much
throughout my degree, and I know there’s still so much more to learn in the
years to come.
I’ve always felt both that teaching is the best way to learn and that I wanted
to give back to computer science education. (Richard Feynman pioneered this, and I took the technique from him.) So many people have helped me get
to the point where I am today, so it only feels right, now, having graduated, to
find a forum to go back to the basics and help people who are just getting
started. My parents and instructors have been so supportive of my journey through computer science, it only feels fitting to go back to the basics to help the next generation who are just now getting started.
The most natural place to start is where every Java
programmer ever has started: with the classic “Hello World” program.
This is a program that can be written in any language—I think it was first
written in the ever-important C, about 50 years ago, sometime in the early 1970s.
You need what’s called an “integrated development environment” to write and
test good Java code. You can technically write Java without one, but that’s like
using Notepad for everything when Google Docs or Microsoft Word are available.
Some people do literally write their code in Notepad—and sometimes you have to,
during interviews, for example—but unless absolutely required, I always
recommend using an IDE because it’s so helpful.
My favorite is IntelliJ IDEA. For years, while I was in
school, I got access to the Ultimate version. If you’re not in school—yet or
anymore—or you don’t have a job at a company willing to give you the Ultimate
version, the Community Edition is just fine!
To get the Community Edition, go to Download
IntelliJ IDEA, and select the right download for your operating system. I
run Windows 10 on my laptop, so I selected Windows. The setup wizard will prompt
you for a few things. You can leave them as defaults. IntelliJ is a big
program, so even the Community Edition takes a few minutes to download.
While you wait, feel free to read ahead.
Today, working on Hello World, our goal is to simply write a program that puts “Hello
World” on the screen. This is such a simple operation that programmers in
almost any language will write their language’s version of this program just to
make sure everything is installed and configured correctly. For novice
programmers, writing your first Hello World is a real rite of passage.
Before we look at any code, let me caution you that Java has
a reputation—at least at this level—for being quite verbose. In Python (another
language), all you need to do is write the following in a Python file:
print(“Hello, World”)
Java’s Hello World includes a lot more extra code just to get things running. We
call that “boilerplate.” Some people—particularly those Python developers (look
how clean print(“Hello world!”) is!)—poke fun at Java and its developers for
how wordy the language is. But after you write in it enough, you’ll get used to
it, and you’ll come to understand what it all means.
For now, it’s OK to take everything for granted. Explanations
will come in later posts, and you’ll surely learn what everything means.
Once you have IntelliJ launched, click “File,” then “New,” then “Project”. For
now, you can then select “Java” and leave everything as defaults. Give the
project a name like “MyFirstProject.” Avoid spaces. You need to define a JDK—the
Java Development Kit—to be able to write and run Java. Use version 23
(as of May 2025 when this was written).
Java is written in .java files. Some magic happens—more on
that later—that translates Java statements like “int x = 5;” (which in human
language means “give me a box somewhere in memory, let it store integers from
about minus-2-billion to plus-2-billion, give that box the name x, and put
inside that box the value 5”) into instructions the computer can understand.
The actual code being run by the computer exists in files that end in .class;
these aren’t meant to be read or written by humans. We use the .java files, and
the .class files are just for the computer. The process for converting .java to
.class is “machine-independent” thanks to the Java Virtual Machine—so you can
run Java code on any computer, with any operating system, as long as you have
access to the JVM.
Once your project is all set up, again, click File, New, but
this time select “Java Class.” This will prompt you to name your file. For now,
remember this: The name of the file and the name of the class must always
match. The PepperoniPizza class is defined in PepperoniPizza.java, the
FluffyPuppy class is defined in FluffyPuppy.java, and so on. “Fluffy Puppy” or “Pepperoni
Pizza” are not allowed (because of the spaces), and neither is “1FluffyPuppy”
(because Java classes can’t start with a number).
We are going to write the HelloWorld class, so that needs to go into a file
named HelloWorld.java. The HelloWorld file contains the HelloWorld class.
IntelliJ is nice because when you name the HelloWorld class, it will
automatically give you the following:
public class HelloWorld{
}
Everything we write to get “Hello World” on the screen will go inside those two
initial curly braces. Notice that for each opening one, there’s a closing one,
and vice versa. Braces—and eventually other paired symbols, like square brackets
[] and parentheses ()—must always be paired, or an error will occur.
Inside those curly braces, write the following, and then
pause again:
public static void main (String[] args){}
several parts of this must be exact: public, followed by static, followed by
void, followed by main, followed by an open parenthesis, followed by String[]
followed by a name (by convention, args, but it could be anything), followed by
a closing parenthesis.
Note the capitalization: everything except String is deliberately lowercase, and String is deliberately uppercase. More on why in an upcoming article.
Your file should now have the following:
public class HelloWorld {
public static void
main (String[] args) {
}
}
Later on, you can get away with just typing the letters psvm and then hitting Tab and IntelliJ will know what you want. But, for now, as you're just getting started, stick to the whole thing. Each part of this matters, and you will shortly learn why.
Now, inside main’s curly braces (so also inside HelloWorld’s),
write the following—again, copying capitalization exactly:
System.out.println(“Hello World”);
Your code should then look like
public class HelloWorld {
public static void
main (String[] args){
System.out.println(“Hello World”);
}
}
And now, finally, you can run it!
You have 3 choices for how to do this:
- Click the green play button next to the public class line (in “the gutter”)
- Click the green play button next to the public static void main line (also in “the gutter”)
- Click the green play button next to the green outline of a ladybug in the upper right corner.
IntelliJ will now convert your human-readable code into
something machine-readable, and the result will be that a window will open
underneath where your code is (we call that window “the console”), and in that
window, you will see 2 things: “Hello World” on the screen, and something like “Process
terminated: exit condition 0”. The “Hello World” on the screen is what we
wanted, and “Process terminated: exit condition 0” means everything worked, and
your program didn’t crash! (By convention, 0 is the status code for success,
and any other number means some type of failure. Be careful typing exactly what
I have above; if you make a typo, you won’t see “Hello World,” and instead of
exit code 0, you’ll probably get exit code 130, indicating a fatal error).
If you’ve made it this far, take a deep breath, give yourself a pat on the
back, and take a minute to celebrate this milestone: you just wrote your first Java
program!
No comments:
Post a Comment