Getting Started in Java

Planning
Step 1
Hello, Java!

As you advance in programming, you may want to learn a more advanced language. Java will allow you to make more robust and complicated programs, but will require you to understand a bit more about how a program is run by a computer.

Information
Step 2
Is Java the same thing as JavaScript?

Java is to JavaScript as "ham" is to "hamster"

Short answer, no. Here are some important differences:

  1. Java is compiled. To run a Java program, first the Java compiler has to translate it into machine code before it can be executed. JavaScript is interpreted, which means the computer reads it and executes it line-by-line directly

  2. Java is statically typed. Every variable, object and function in Java has a type, you will always need to state that type in your program, and you cannot change something's type. JavaScript is dynamically typed, meaning you can change variables types, and you don't have to say what kind of variable you are talking about when you declare one.

Information
Step 3
JavaScript was made for the web!

One of the reasons that we use JavaScript so much at MVCode is that you can do it from anywhere and run it without any special programs - just Chrome, or your browser of choice. This lets us program in the cloud, so to speak, so you can do your work at the club, at home, and at school.

Java is a little trickier. You need to download the Java Software Development Kit (SDK), which includes the Java Compiler to translate your program into machine code, and the Java Virtual Machine (JVM) which then runs it.

Information
Step 4
But there is a way to do Java on the web!

To save time and heartache, we use repl.it for all the AP Computer Science courses in Java. It allows us to code and run Java programs on the web. This means you can easily switch between computers when you work on code, and don't have to download anything to run it.

Step 5
Get started on Repl!
  1. Go to Repl.it and click Sign Up.
  2. Make sure you remember your username and password, so that you can access your code in the future.
  3. Once you have logged in, click + new repl in the top right corner.
  4. Select Java for the language
Check Your Work
Step 6
You've already got some code

When your new Repl project opens, you should see this code:

Step 7
Run the code

Change the

System.out.println()

line so that it says "Hello, [YOUR NAME HERE]"
Then, press the Run button to execute the code.

Check Your Work
Step 8

It will take a couple seconds to run the program. While Repl is very convenient, it is very slow

Your program should not say "Hello, Sarah", unless your name is Sarah, too. In that case, hello, Sarah!

Information
Step 9
Anatomy of Java Code

Another thing that distinguishes Java from JavaScript is that it is strictly Object-Oriented. For our purposes, this means that all code you ever write has to be part of a class. The code that executes right away from a class should always go in a method called main.

We will go into detail on all of this later. For the time being, all you need to know is that the code that you want to run should go in the main method of you Main class.

Planning
Step 10
Forking code

We use the term fork to refer to making a copy of some code so that we can make changes to it.

Imagine a fork in the road. You are about the take the code one direction, but the main code will likely go a different direction. This allows us to experiment!

Step 11
Fork SecretNumber

Check out the Java program SecretNumber here.

This program makes a random number, ask the user to give a number, and then tells you both numbers to see if you guessed correctly. To modify it, we need to fork it.

Once you have opened the repl, click the pencil icon in the top near the name of the project. This should open a menu, with the option to fork at the bottom.

Challenge
Step 12
Make the game easier!

You don't need to understand everything about how this program works right now, but see if you can change it so that it generates a number between 0 and 9 and asks the user for one between 0 and 9, too.

Planning
Step 13
That's all you need!

Now that you know a little bit about how Java works compared to JavaScript, and you have made a Repl.it account and learned how to for a Repl, you are ready to start the AP Computer Science courses.

The rest of the steps of this lesson will teach you how to set up for Java coding on a single computer. This is a bit more work, but if you will always be using the same computer to do these courses, you can save some time long term.

Step 14
Download the Java Standard Edition SDK

If you are working at home, you will probably want to download Java and an IDE. At an MVCode Club, this should already be installed, but may need an update. At school, on a Chromebook, or anywhere where you cannot install programs, you will want to stick to repl.it

Get Java SE SDK

Step 15
Test your Java installation

To test whether or not Java is installed correctly, open the terminal. On Mac, this called Terminal. On Windows, I recommend installing Git Bash. On Linux you will most likely use XTerm.

Once you have opened your terminal program, type:

java -version

and press return

Getting Started in Java Info

Account

MVCode

Created By

slinkous

Access Level

public

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach