Learn Java Programming
Supplemental, free, online textbook called Introduction to Programming Using Java
Java Cheat Sheet
Classes, Objects, & Constructors
AP Quick Reference Guide
Online Java Compiler
Java Examples
Arithmetic Operators
Digital Textbook
Visualize Java code execution
Aug 12
Join our Google Classroom. Sign in with your school email account and the code that I give you.
What is Java?
Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 3 billion devices run Java.
It is used for:
- Mobile applications (specially Android apps)
- Desktop applications
- Web applications
- Web servers and application servers
- Games
- Database connection
- And much, much more!
Why Use Java?
- Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
- It is one of the most popular programming languages in the world
- It has a large demand in the current job market
- It is easy to learn and simple to use
- It is open-source and free
- It is secure, fast and powerful
- It has huge community support (tens of millions of developers)
- Java is an object oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs
Getting started with java. You will first need to create a folder inside your documents folder called java and then another folder called HelloWorld inside the java folder. Next, open the Sublime text. It will ask you if you want to download a new version. Press cancel. On the new document type in the following code:
The name of the java file must match the class name. When saving the file, save it using the class name and add .java to the end of the filename.
Save your file as HelloWorld.java in your HelloWorld folder
Next, open the Terminal. On Windows, it will be Powershell(X86).
type in cd Documents/java/HelloWorld
Now type ls
You should see your HelloWorld.java file
Every line of code that runs in Java must be inside a class. In our example, we named the class HelloWorld. A class should always start with an uppercase first letter.Note: Java is case-sensitive: MyClass and myclass have different meanings.
The main Method
The main method is required and you will see this in every Java program:
public static void main (String[] args)
Any code inside the main method will be executed.
Java files need to be compiled before they will run, so type in javac HelloWorld.java
If there are no errors, you should see this
Next, type java HelloWorld
You should see this
You've just created your first java program!
System.out.println()
The println() method, short for "print line", is used to print a value to the screen and then adds a line break. print() also prints whatever is inside the () to the screen but does not add a line break.
You should note that the contents of the () are also in " " . This means that what is inside the quotes will print exactly to the screen.
This is called String literal, and we will see that we can put other things inside the () that will do math, call functions and more.
Each code statement must end with a semicolon ;
The /* and // signify comments. This is code that does not run, but it lets others who read your code know what you had in mind when you wrote it.
The curly braces {} mark the beginning and the end of a block of code and they always come in pairs.
Now do the same with these 3 programs
You will turn in your programs and a screenshot of the output to our Google Classroom.
Click here for the course syllabus.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.