11 Basic Java Concepts for Beginning Programmers

iD Tech
June 27, 2019

Java is a powerful, multi-platform programming language used for a number of professional and commercial applications—including every Android app and even the Android operating system itself!

With Java, kids can learn to make a tic-tac-toe game with a win algorithm, a top down shooter, or an endless runner!

But what basic Java concepts should they know about, specifically? How can kids get started making their own game or application? 

Java Basics

The goal of this post is to get you and your student thinking—does programming with Java (and the concepts and definitions below) sound like something they'd want to explore further?

Of course, after reading through, there will still be some learning holes that need filling! What follows, though, should be more than enough to nudge aspiring coders in the right direction, should they choose to do so. 

Ready?

1. Setting Up Your Workspace with an IDE

To write Java code, you’ll need an IDE (Integrated Development Environment). This is a tool that helps you write, organize, and test your programs. The interface looks like this:

intellij.png

Any kid interested in Java coding should get familiar with IDEs and their interfaces. For instance, IntelliJ has handy shortcuts! Typing "psvm" and pressing Tab automatically creates a "main method," which is the starting point of every Java program. (More on main methods below.)

2. Understanding Class Files

All Java code is contained within class files—think of a class as a container that organizes your program.

Importantly, class names should be capitalized, and each new word in the title should also be capitalized. The names cannot contain spaces. Example of a correctly-named class: “AdventureGame”.

3. Creating the Main Method

Before one can begin coding, they’ll need to add a method inside of the class, which is the first bit of code that’s called when you run your program. Think of it like a starting point for the rest of the code!

So again, with the example above, underneath “public class [ClassName]” typing “psvm” and then pressing the tab button will create a main method in IntelliJ. 

4. Using Variables to Store Data

A variable stores information about a program while it's running. 

For instance, in an RPG (a type of video game), a character's name, level, and hit points are all stored in their own variable. Any time your child is playing a game and they level up (or take damage), a variable changes to reflect what happened.

To use a variable, a coder must declare the proper variable type:

Integers: An integer is a type of variable and represents a whole number. For example, -17, 4, 0, and 42 are all integers. You declare an integer in Java using the “int”  keyword.

Doubles: A double is a number that can have a decimal. For example, -12.5, 4.0, 0.001, and 4.2 are all doubles. You declare a double in Java using the “double”  keyword.

Strings: You use a String to store text. For example, "carrot," "apple pie," "pear," and "orange juice" are all Strings. You declare a String in Java by typing  “String” followed by the variable name. 

It's considered good practice to declare all variables at the beginning of the code. When everyone codes this way, they know exactly where to look to find them!

5. Printing Statements

A print statement is a line of code that shows text in the output area of IntelliJ, also known as the console. The image below shows the code structure of printing statements in Java.

print statements.png

You might not need to know this just yet, but here is the breakdown of the above:

  • System is a Java=defined class. In this case, System is used to output statements.
  • out is part of the System class which takes a stream of data that will be printed.
  • println is a method that takes a statement and prints it to a new line in the console.
  • ; the semicolon is used in Java coding to close a line of code. If the semicolon is missing, the program will output an error.

The result is something like:

System.out.println("Welcome to Java!");
System.out.println("Your score is: " + 10);

6. Adding Comments

In most coding languages, there is a way to leave comments and text that the computer will ignore. For example, in Java, you can add a comment by putting two forward slashes // in front of it.

A single-line comment:

"// This is a comment"

A multi-line comment:

/* 
  This is a multi-line comment.
  It can span multiple lines.
*/

Comments are used to leave notes when you have to take a break or need clarification about something, or to let someone else, who's looking at your code, understand what's going on. Comments organize and keep your code tidy so you can find what you're looking for.

7. Doing Math with Operators

Operators perform mathematical calculations while a program is running.

Say, for instance, if a game character earns experience, the action needs to be added to the total experience count. If an arrow hits an enemy, the damage it takes needs to be subtracted

So, operators are used for whenever a value needs to be changed. 

Multiple operators can be performed at once, and Java arithmetic operators follow the mathematical order of operations

8. Make Decisions with Conditional Statements

Conditional statements are used to determine the flow of a program, and checks to see if a condition is true or false. Based on the condition, a specific block of code will run. The three types of conditional statements are if, else if, and else. 

conditional statements example.png

9. Repeating Actions with loops

For loops run a section of code repeatedly until a condition is met. They are useful when you know how many times to repeat. 

For example, in an RPG, a player's inventory could be stored as an array of strings. While exploring a dungeon, they encounter a locked door—a for loop can be used to determine if the player is carrying a key to the door.

A while loop is code that repeats itself based on a condition. For instance, a while loop can continue to run until monster health is equal to 0. Now, players will continue to descend into the depths of the dungeon until they run out of health!

10. Storing Multiple Values with Arrays

Arrays are collections of values stored under the same variable name, and are useful for organizing your data and storing similar values together. The image below demonstrates the code structure of an array.

array example.png

In Java, an array is defined by brackets [ ] and can be of any variable type, such as a String, integer, or double. All the array values are stored inside curly braces and separated by commas.

int[] numbers = {1, 2, 3, 4};
System.out.println("First number: " + numbers[0]);

11. Multidimensional Arrays

While arrays are ideal for storing a finite list of elements with the same data type, sometimes you need a table of elements. To accomplish this, you can use a two-dimensional array, which holds two one-dimensional arrays representing rows and columns on a grid.

Two-dimensional arrays are declared like any other variable—they have a data type and a name. The image below demonstrates the code structure of a two-dimensional array.

two-dimensional array.png

To explain:

  • The array_type is the data type of the two-dimensional array—for example, strings, integers, decimals, etc.
  • The two sets of brackets, [][], represent the two sets of arrays stored in the multidimensional array.
  • The array_name is the name of the two-dimensional array.
  • The new keyword creates an object for the array_type such as a String object.
  • In this example, the [rows][columns] will represent a value's row number and column number.

Ready to put it all together?

So, next steps? 

One, here is a list of ways to practice Java online

After that, online coding courses and summer coding camps are great options, allowing students to dive into the details en route to building a lifelong passion for tech, like Java, alongside new friends in a pressure-free, creative environment. 

And kids who might thrive in a more personal atmosphere? Java tutoring is available for a 1-on-1 learning experience with curriculum customized to their unique interests and skill level. 

So, again—know of someone who enjoys something like playing Pokémon? Maybe learning how to make a text adventure RPG is in their future. Or, if they are interested in social media, perhaps they can try to recreate an algorithm for different user search functions. 

With hands-on exposure to Java fundamentals and the opportunity to dig deep into programming logic, instruction in any form can help students build the confidence needed to keep learning during summer and long after summer ends.

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! Online camps, Roblox coding classes, coding summer courses, and more. 

By signing up you agree to our Privacy Policy
Subscribe & Save!

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! Online camps, Roblox coding classes, coding summer courses, and more. 

By signing up you agree to our Privacy Policy

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! Online camps, Roblox coding classes, coding summer courses, and more. 

By signing up you agree to our Privacy Policy
Subscribe & Save!

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! Online camps, Roblox coding classes, coding summer courses, and more. 

By signing up you agree to our Privacy Policy