Quick 115 Java Guide
We will use the Java programming langauge to do a few programming tasks.
// bare minimum Java starter (rename class as needed)
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Most important Java notes:
- Each Java class lives in a new .java file. The name of the file needs to match the name of the class.
- Structures start with { and end with }
- Most lines need to end in a semicolon
- You must provide the type when you declare a variable, but do not use the type again after that.
Java input/output example with prompt utilities
Java IDEs
Online IDE
tldr: has ads, you have to remember to save.
This online Java IDE
-- an editor that also lets you run code right in the browser -- unfortunately does have ads, which may be misleading.
One side of the page is the code editor. At the top there
is a Run button, and the other side shows the output (or errors).
You can edit the code in the editor and run it as many times as you want.
You do not need to download any extra tools to use this page, but you do need to be
careful to save your files, it will not automatically do this for you!!
- The editor page starts off with example code in it. At the top of the editor is a "+" button. Click this to create a
new file and change the name so the name of the file is the same as as the name of the class with .java at the end.
For instance, if the name of the class is MainA, the file should be MainA.java
- You can paste my example code into a blank .java file you created in the editor and then start editing it.
You can also use the open button to open an existing .java file from your computer.
- At top under the "+New" dropdown is a Save option which lets you download a copy of the file.
Save often and don't lose your work! It WILL NOT SAVE your code if you close the browser, so you need to download your work!!!
- You have to click Run again each time you want it to run your code to test changes.
- As you type, the editor tries to help by providing suggestions, but these pull from all of Java instead of
just from the tools we are using, so its suggestions may not be useful! You can turn this off by right-clicking the code,
choosing Editor Options, and and unchecking Autocomplete at the bottom of the list, if it annoys you.
- If your output has the word error in it, java is telling you that you have a syntax error you need to fix.
It will try to tell you the line number, but sometimes an error in an earlier line can cause a problem that
isn't detected til a later line.
NetBeans
tldr: no ads; autosaves on run; lots of tools. Files live in a project directory.
Netbeans is on the classroom computers. You can
install NetBeans at home.
You will need to
create a project in Netbeans for your files to live in. You can have multiple projects, but multiple
.java files for different programs can all live in the same project as long as each .java file
has a unique name. Files in your project are listed on the left,
you can double click to open them in the editor on the right and right click them or their code in the
editor to Run File; output or errors will be shown at the bottom.
- You can add new files using File->New, making sure that the name of the file is the same as the name of the class
you want to use. For instance, if the name of the class is MainA, the file should be MainA.java
- You can paste my example code into a blank .java file you created in the editor and then start editing it.
- At top the button showing two floppys is the Save All button. If you run your code, it will save automatically
- Right-click your java file at left or your code in the editor to choose Run File each time you want it to run your
code to test changes. (Just "Run" runs whichever Netbeans has set as your "default" java file.)
- As you type, the editor tries to help by providing suggestions, but these pull from all of Java instead of
just from the tools we are using, so its suggestions may not be useful! You can turn this off under
Tools->Options->Editor->Code Completion, if it annoys you.
- If your output has the word error in it, java is telling you that you have a syntax error you need to fix.
It will try to tell you the line number, but sometimes an error in an earlier line can cause a problem that
isn't detected til a later line.
- You will have to drill down into your java project directory to find the actual .java files (NOT .class files)
or you could even copy paste the finished code into a text file and turn that in.