Skip to content

HelloWorld

Chris Ward edited this page Oct 7, 2022 · 5 revisions

Here we see why we started with Python. The typical first program is to print "Hello World" to the console. How would that have happened in python?

print("Hello World")

That's it. If we loaded that onto the Circuit Playground it would print that to the console. Now here is your first Java program. It will take a bit of explaining.

package examples.a_helloworld;

public class HelloWorld {
    public static void main(String[] args) throws Exception {
        System.out.println("Hello, World!");
    }
}
  • Java needs to understand where each file is. The package statement is like a path. This file is in examples/a_helloworld.
  • Java requires all code to be in a class. For now we can just think of a Class as a file. Later we will learn more about Classes. For now know that the name of the file MUST match the name of the Class.
  • These next bits are not super important to understand at the moment, but we will bring them up now.
  • The public part means that it is visible to all other programs that might want to see or use it - we will learn more about this later.
  • If you want to "run" a java program, it needs to have a 'main' method just like this. If this code was a part of a library it would not be necessary.
    • public - everyone can see it
    • static - only one main method
    • main - the name of the method, have to use this name here
    • String[] args - an array of strings
    • throws exceptions - how java deals with error handling

Whew! That's a lot just to get to the one line of code we cared about: System.out.println("Hello, World!"); Most things in Java are classes, and System is a "built in" class, like how print is built into Python. But print in Java is accessed from the System class, that has another class for handling output to the console, called 'out', which has the function println (for print line) on it.

Nothing seems simple in Java, does it? Don't worry too much about all that yet. We will slip in explanations of all of these things as we go along.

Did you notice the ; at the end of the println statement? Java lines end with one. Python didn't have anything. Python needed very precise spacing for code blocks. Java uses { } for code blocks.

If you want to move along go ahead, and check out variables

Why the heck do we use Java?

But if you are asking "Why the heck do we use Java then?" I can give a short explanation. Lets compare 3 languages, C++, Java, and Python. You may not have known when you used it, but Python is called an interpreted language. What that means is that when you run a Python program, it runs each line as it sees it. It's probably what you assumed happened. But C++ (see plus plus) code requires you to compile it. It takes the pretty code you wrote and turns it into the bare instructions the computer actually understands. The computer doesn't actually understand Python. For every line of Python it "compiles" as it sees it. (this is a simplification) If you guessed that this might be slower, you would be right. To get the most out of hardware, you probably want to use a compiled language. There are issues with C++ though. You have to be very careful with it, or mistakes can crash computers, or cause you to run out of memory. Java was created to be in-between these two. Java code is compiled before it is run, but it is compiled to an intermediate step called byte code. This is done so that a Java interpreter can run the same program on any hardware. That C++ program has to be compiled differently for every hardware it runs on. Because of the Java interpreter it can manage memory - as can Python - so that the programs don't crash the whole computer or run out of memory. This running of byte code makes Java faster than Python, but not as fast as C++. Lots of people find that the speed tradeoff of using Java and the easier programming environment (controversial statement maybe) make using Java the better choice. This is why we use Java on the Robot.

Clone this wiki locally