Skip to main content
Logo image

Section 1.1 What is programming? What is Java?

What do Android phones, Minecraft, and Netflix have in common? They’re all programmed in Java! Many of the apps you use in an Android phone or tablet are written in Java. If you’ve used App Inventor before, those apps are translated to Java before they are run on a phone or tablet. Netflix uses Java for some of its software too. Java is a programming language that is used worldwide to create software that we all use.

Subsection 1.1.1 Telling the computer what to do

So Java is a programming language, but what does that mean? A programming language is a language that we can use to tell a computer what to do.
While computers are, in some sense, very complicated, made up of billions of tiny switches all turning on and off billions of times a second, in another sense they are pretty simple: at their most basic level they can only do a few things, mostly a bit of arithmetic and logic. Everything else has to built up from there by telling the computer what simple steps to do in what order to create the incredibly complex behaviors we see every day on our computers, tablets, and phones.
Programming languages let us describe these sequences of steps, which computer scientists call algorithms. Algorithms are used in many areas of life, not just in computer science. For example, a recipe is an algorithm for cooking a meal. A set of directions to a friend’s house is an algorithm for getting there. But unlike when we’re describing something for another person, when we’re writing algorithms to be run on a computer, we need to spell everything out very precisely. So programming languages are designed bridge the gap between how we think about problems as humans and how they need to be expressed for the computer.
Complex programs will be made up of lots of algorithms and while we ultimately have to express them all in a programming language like Java, when we are working on programs we will frequently shift between thinking like a human about what we want our program to do to then translating that into Java that tells the computer exactly how to do it.
It can be useful to plan out our code by first describing the steps in English or another natural language or by drawing diagrams. Sometimes programmers will use pseudocode which looks kind of like a programming language but lets us leave out some of the details that a real programming language would need.

Subsection 1.1.2 Our first Java program

Different programming languages have different ways of structuring code. Java is an object-oriented language in which all code is structured into classes. We’ll learn more about classes and object orientation in later units. For now we just need to understand that every Java program is written as one or more classes which have a particular structure.
In particular, within any class there can be a main method that contains the code that runs if that class is run as program. In the coding activities in this book, there is almost always one class and that is the one that is run when you hit the run button. In other programming environments that you will use for more complex programs there will be some other way to run your programs but they always start from some class’s main method.
A method is a chunk of code that performs a specific task. In other programming languages, methods are called functions or procedures. And in block programming languages like Snap! they are called blocks. For now, all you need to know is that any code you put inside the main method will be run when the class is run.
Here is the template for a simple Java program with a main method:
public class MyClass {
    public static void main(String[] args) {
        // Put your code here!
    }
}
Click on the run button below to have the computer execute the main method in the following class. Then, change the code to print your name. Be sure to keep the starting and ending quotation marks. Click on the run button to run the modified code.

Activity 1.1.1. First Java program.

Run this code to see the output below it. Then change the code to print your name, for example β€œHi Pat!”, and run again.

Subsection 1.1.3 Java syntax

Every programming language has a syntax which is a set of rules for how the text of a program has to be organized. Human languages also have syntax. For instance in English, β€œThe robot ate the cupcake”, is a syntactically correct sentence while, β€œAte robot cupcake the the”, is not. A program, like a sentence, can only actually be a program if it is syntactically correct. But computers are even more picky than humans about syntax, so one of the first things we need to learn, when learning a new programming language are the exact rules of its syntax. Luckily most programming languages are designed to have a relatively simple syntax, even if it looks a bit odd at first, so we can usually pick it up by reading and writing code and paying attention to the error messages we get when we make mistakes: no need to memorize a bunch of rules.
Puncuation plays a big role in programming syntax. As you can see in the simple programs above, Java uses curly braces {} in pairs so each open brace { must have a matched close brace }. These are used to start and end class definitions and method definitions.
Semicolons ; are also used all the time in Java. They are kind of like Java’s equivalent of a perod in English, used to end a complete statement like System.out.println("Hi there!");. For most beginning Java programmers the most common error is leaving out a necessary semicolon.

Note 1.1.2.

You will not be penalized on the exam if you forget a needed semicolon, but the Java compiler is not so lenient; your program won’t compile without it. Note also that not every line ends with a semicolon; if the line starts a construct like an if statement, there is no semicolon before the opening { nor one after the closing }.
So punctuation is critical. Whitespace, on the other hand, such as the indentation at the beginning of lines or blank lines is completely unimportant to the computer, except in a few special contexts. But it is very important to human progarammers to help show the structure of code. You should pay attention to how code in this book is indented and try to indent your code in the same way. (Or use the Reformat button on the coding exercises to automatically properly indent your code.)
Other than punctuation and whitespace, the rest of the text of a program is made up of words. Some words are special to Java and are called keywords. In the programs above the keywords are public, class, static, and void. Because keywords have a special meaning to Java we can only use them for the particular purpose each one has. All keywords in Java are lower case.
Other names are ones we get to make up. In the program above MyClass and args are names, or, more formally, identifiers. MyClass is the name of a class and args is the name of a variable (we’ll learn all about classes and variables later.) Java doesn’t care at all what names we use there other than that they be made up of just letters and numbers and start with a letter. And as you can see, when a name is made up of multiple words like MyClass the convention is to capitalize the first letter of each word.
Finally the words main and String are not keywords, but have a particular meaning to Java. The name main is used to identify the method we want Java to run when we run our program. And String is a class that is part of Java’s standard library. But since they are not keywords we could use those names in other ways in our programs. (But it would be confusing so we probably wouldn’t.)

Activity 1.1.3.

Drag or click on the blocks you need to move them from the top section into the yellow area to create the first line for the main method. There are extra blocks that you don’t need.

Activity 1.1.4.

Drag or click on the blocks you need to move them from the top section into the yellow area to create the first line for the main method. There are extra blocks that you don’t need.

Activity 1.1.5.

Drag or click on the blocks you need to move them from the top section into the yellow area to create the first line for the main method. There are extra blocks that you don’t need.

Activity 1.1.6.

Drag or click on the blocks you need to move them from the top section into the yellow area to create a Java statement that will print β€œHi!”. There are extra blocks that you don’t need.

Activity 1.1.7.

Drag or click on the blocks you need to move them from the top section into the yellow area to create a Java statement that will print Bye!”. There are extra blocks that you don’t need.

Subsection 1.1.4 Comments

Another important piece of Java syntax are comments which are bits of the program that Java ignores but which are useful for humans reading the program.
Java uses two styles of comments. Single line comments start with // and go to the end of the line. Sometimes they are placed on a line by themselves, usually to comment on the next bit of code. And sometimes short comments are added at the end of a line.
Multi-line comments, are marked with a /* to start the comment and a */ to end it.
Commenting code well is an art form: there is no need to explain things that can be understood from the code itself but it can be very valuable to explain why code is a certain way.
It can also be useful to use comments as we develop code to make notes about our plan and then delete the comments as we develop the code. And perhaps the most common use of comments is to temporarily remove a bit of code without deleting it. Many programming editors will have a keyboard shortcut for adding // at the start of a selected range of text.
Here are some examples of the different kinds of comments:
/*
 * This style of multiline comment is often used before classes and methods to
 * document their purpose.
 */
public class Foo {
}

int max = 10; // a comment about this particular line

// Some comments about whatever comes next
if (foo) {
  doStuff();
}

Subsection 1.1.5 Compiling and running Java programs

Even though Java is a programming language designed for us to communicate with the computer, computers don’t actually know what to do with Java source code. Instead we have to compile (translate) Java source files that we write into files that are meaningless to a human but which are closer to something the computer can actually run. A compiler checks your code for errors and then translates it into executable code that can be run.

Note 1.1.8.

Some languages compile their source code to something called machine code that can run directly on the computer hardware. Java uses a different strategy, compliling to an intermediate form called bytecode which is run by another program called the Java Virtual Machine. But bytecode is much closer to machine code than it is to Java source code.
If the compiler can’t understand our program because of syntax errors it will try to tell us what is wrong. That’s when we need to check for missing semicolons and unmatched curly braces; we have to fix any syntax errors before the program can be run.
We can write Java code in any text editor, including the small text editor built into the Active Code exercises in this book. For this course, you’ll write a lot of code in the Active Code exercises and you can use the scratch coding area accessible via the pencil icon at the top of this page to write simple programs.
However, for complex programs many programmers use an Integrated Development Environment (IDE) that provides tools for writing, compiling, and running code all in one place. Several are discussed in SectionΒ 1.4Β Java Development Environments. Most likely your teacher will tell you which one to use.
When working in an IDEs or text editor you must name the file the same name as the class name with β€œ.java” as the extension. All code (programs) in Java must be defined inside a class in a source file, and the name of the class must match the file name.

Subsection 1.1.6 Summary

  • Algorithms define step-by-step processes to follow when completing a task or solving a problem. These algorithms can be represented using written language or diagrams.
  • Sequencing defines an order for when steps in a process are completed. Steps in a process are completed one at a time.
  • An Integrated Development Environment (IDE) is often used to write programs because it provides tools for a programmer to write, compile, and run code.
  • A basic Java program looks like the following:
    public class MyClass {
      public static void main(String[] args) {
        System.out.println("Hi there!");
      }
    }
    
  • A Java program starts with public class NameOfClass {. If you are using your own files for your code, each class should be in a separate file that matches the class name inside it, for example NameOfClass.java.
  • Most Java classes have a main method that will be run automatically. It looks like this: public static void main(String[] args) { }.
  • The System.out.println() method displays information given inside the parentheses on the computer monitor.
  • Java statements end in ; (semicolon). { } are used to enclose blocks of code. // and /* */ are used for comments.
  • A compiler translates Java code into a class file that can be run on your computer. Syntax errors are reported to you by the compiler if the Java code is not correctly written. Some things to check for are ; at end of lines containing complete statements and matching { }, (), and "".
You have attempted of activities on this page.