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.
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.
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.