๐1.2: Java Introduction
Table of Contents
๐ This page is a condensed version of CSAwesome Topic 1.2
- Go to GitHub and click on your picture in the TOP RIGHT corner
- Select
Your repositories
- Open
CS2-Unit1-Notes
- Now on your repository, click and select the
Codespaces
tab - Click
Create Codespace on main
(unless you already have one listed there), wait for the environment to load, then youโre ready to code! - ๐ Take notes in this Codespace during class, coding along with the instructor.
๐ When class ends, donโt forget to SAVE YOUR WORK! There are multiple steps to saving in GitHub:
- Navigate to the
Source Control
menu on the LEFT sidebar - Click the button on the LEFT menu
- Type a brief commit message at the top of the file that opens, for example:
updated Main.java
- Click the small
โ๏ธ
checkmark in the TOP RIGHT corner - Click the button on the LEFT menu
- Finally you can close your Codespace!
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. 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.
First Java Program
Every program in Java is written as a class. Java is an object-oriented language and weโll learn more about classes and objects in Unit 2. Inside the class, there can be a main method that starts the program. When you ask the Java run-time to run a class, it will always start execution in the main method.
The name of the source file must have .java
as the extension, and it must match the class name defined inside the file!
Here is the template for a simple Java program in a file named MyClass.java
:
public class MyClass
{
public static void main(String[] args)
{
System.out.println("Hi there!");
}
}
In Java every open curly brace {
must have a matched close curly brace }
. These are used to start and end class definitions and method definitions.
Print Methods
Java has two different methods to print output to the screen:
System.out.println(value)
: prints the value followed by a new line (ln)System.out.print(value)
: prints the value without advancing to the next line
String Literals
The "Hi there!"
in the print statement above is called a string literal, and it can have zero to many characters, enclosed in starting and ending double quotes.
Syntax: the โgrammarโ of Java
Special wordsโalso called keywordsโsuch as public
, class
, and if
must be in lowercase, but class names such as System
and String
are capitalized.
Lines in a Java program that express a complete action such as assigning a value to a variable must end with a semicolon (;
). Such a line is called a statement. You can think of the semicolon (;
) in Java like a period (.
) in English. The same way you use a period to end a sentence in English, you use a semicolon to end a statement in Java.
You will not be penalized on the AP 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}
.
Syntax Errors & Debugging
Computers donโt actually speak Java so we have to compile (translate) Java source files that we write into class files, which is code that a computer can understand and run.
Syntax errors are reported to you by the compiler if your Java code is not correctly written. Examples of syntax errors are a semicolon ;
missing or if the code has a open curly brace {
or open quote "
, but no close curly brace }
or close quote "
. Informally, a syntax error is called a bug, and the process of removing errors is called debugging. An early computer science pioneer Grace Hopper documented a real bug, a moth that flew into a computer in 1947!
๐ Debugging is a normal part of coding. It can be frustrating at times, but you will get better at it with practice! Sometimes another pair of eyes really helps, so ask a friend if you get stuck or try explaining your code line by line to someone, or even a rubber duck. Rubber duck debugging is a lot of fun!
Reading Error Messages
Oops! Your code has an error. Before you try to fix it, read the error message.
The example below is an example of a compile time error - an error detected by the compiler while itโs โtranslatingโ your Java source file.
FirstClass.java:5: error: unclosed string literal
System.out.println("Hi there!);
^
1 error
The first line starts with the name of the file that was being compiled. Then thereโs a colon (:
) followed by a number. That number tells you the line number in the file where the compiler detected the error, in this case line 5.
Error messages arenโt always 100% accurate about where the error actually is; sometimes you actually need to change something a bit earlier in the program and sometimes a bit later. But the line number is the best place to start looking.
After the line number and another colon, you will find the actual error message. These can be kind of cryptic but you should still read it. As you learn more Java vocabulary they will become more meaningful but they almost always contain some useful clues. For instance take this error message: โunclosed string literalโ. You may not know what a string literal is (yet) but โunclosedโ suggests something was opened and then not closed. Keep that thought in mind.
Now look at the next two lines. The very next line is just the line of code from your program. But below that is a very important line containing a single caret (^
) positioned to point at exactly where in the line the Java compiler thinks the problem is. In this case itโs pointing at the quotation mark (โ
) before โHiโ. So itโs complaining about something being unclosed and itโs pointing us at a quotation mark.
Comments
In Java and many text-based coding languages, //
is used to mark the beginning of a comment. For multi-line comments, use /*
to start the comment and */
to end the comment. The compiler will skip over comments. However, it is a good idea to use comments to make notes to yourself and other programmers working with you.
Here are some examples of good commenting:
/* MyClass.java
Programmer: My Name
Date:
*/
// Keep track of maximum score
int max = 10;
โญ๏ธ Summary
- A Java program starts with
public class ClassName { }
. 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 exampleNameOfClass.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.print()
andSystem.out.println()
methods display information given inside the parentheses on the computer monitor. System.out.println()
moves the cursor to a new line after the information has been displayed.- A string literal is enclosed in double quotes (
" "
). - 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.
- Compiler or 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" "
.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.