5 minute read

featured image alt

Welcome to Java, the Carlos Way! Over the course of this series, I’m going to try to teach you Java in the easiest way possible. There’s a lot of content, so be sure to take things slowly and at your own pace. I’ll do my best to link to a lot of different tutorials for more in-depth review. Now that I’ve got your attention, let’s get started!

Why This Exists

When I was 12 years old, I decided to tackle my first programming language, Python. I did so without any prior knowledge, without any formal textbooks or teachers: completely on my own. In the same way that I was able to learn from volunteers for free and on blogs and YouTube sites from people around the world, so too I want to impart my knowledge. I hope you are able to learn a great deal from this, and if you do, please consider passing on what you learn as a programmer. Soon, we will make the world full of people who know programming!

Now, let’s get started…

Java 101 (The Boring Stuff)


I call this “The Boring Stuff” because I know you want to get dirty with code! However, there are some central things you must understand before jumping into a development environment and writing code…

What Is OOP?

OOP, or Object Oriented Programming, is a method of programming in which a programmer writes classes, and functions for those classes, and develops a hierarchical structure in order to achieve his or her programming goals. There are many different programming languages: Python, Java, C++, and more, and each has their own separate use case. Java is primarily used for developing cross-platform applications, or programs that can run on more than one system, such as Android, Windows, Mac, and iOS.

The Java Hierarchy

In Java, as well as in other programming languages, there is a general hierarchy that dictates the way we use the language.

I have created a handy diagram in order to illustrate the Java Hierarchy: The Java Hierarchy.

Levels of Hierarchy

The levels of hierarchy go as follows:

  1. Packages
    • A package is the highest level that can be imported into a Java program. Some of the most popular Java packages are:
      • Java.util
      • Java.lang
    • Every class in Java is contained into packages.
    • It is possible to create your own package when designing a project, but this is only recommended when it is a very big project and requires developing entire classes from scratch.
    • Package names are denoted by a lowercase letter.
  2. Classes
    • Classes are contained within packages, and are usually the most often written by programmers when designing a program.
    • Some popular Java classes are:
      • Java.lang.Math
      • Java.util.Random
    • Class names should begin with a capital letter.
  3. Objects
    • Objects are what we call instances of a class, in which one instantiates a class in order to use it in our program. Instantiating is like “materializing” it, and allows us to use its member functions/variables.
  4. Code (Functions and Variables)
    • Inside of every class lie functions and variables that are relevant to its name. Though there may be many instances of a single class, it is these member functions and variables that separate them from each other.
    • Member functions use camelCase, which will be explained later.

The Java API

The Java API is the number one website you’ll be referencing and bookmarking throughout your studies. It contains the list of all of the packages, classes, objects, and methods that are embedded into the base version of Java that is shipped onto millions of devices. Few languages have as good a documentation as Java, which is why it’s a favorite for many programmers.

Syntax

What distinguishes Java from any other language its syntax. Just like any written language, programming languages each have their own set of syntax, and to learn the syntax by heart and to be able to use it seamlessly is truly to know the programming language. There are two categories to programming syntax:

  1. Syntax that the language interprets.
    • This has to be exact, or else the computer will not know what telling it to do.
  2. Syntax that is used by programmers to make it readable to other programmers.
    • This is not technically necessary, but a good programmer will always use common programming conventions.

In this course, we’ll be going over both of these.

CamelCase

One of the most important building blocks of naming structures in Java is the use of camelCase. Whether working with classes or variables, the way we distinguish between words in Java is using camelCase. This practice involves capitalizing the first letter of each word that’s composed in the Class, variable or method name. Here are a couple of examples:

  1. Class Name
    • public class DogFood
    • Right off the bat, even without seeing the class declaration, we already know it is a class because it begins with a capital letter.
    • This follows the convention explained above, which is why this is so important.
  2. Variable/Method Name
    • public void getFood(){}
    • Since this method name begins with a lowercase, we already know that it is either a variable or a method.
    • This is also in accordance with the levels of hierarchy conventions.

Technically, the Java compiler doesn’t care about your naming conventions, but using them will help you and others reading your code to understand what you’re doing. This is something I started off not caring about, and I had to learn to do it the hard way, because I ended up not being able to understand my own code. Don’t be a Carlos!

Brackets, Parentheses, and Semicolons

Another important element of Java is the use of brackets and parentheses. Almost all programming languages use these in one form or another, but the ones in Java are particularly essential because that is the way that Java knows what you are writing.

Brackets

  • All content inside of a class or function is placed inside of brackets.
  • There are always an even number of brackets in any code that will correctly compiled.
  • Always count your brackets to make sure you have the correct number!

Parentheses

  • Parentheses can be used to separate arguments when using arithmetic.
  • In methods, parameters are placed inside of parentheses, whether defining or calling the function.
  • There should also always be an even number of these, so be sure to count them!

Semicolons

  • Semicolons are placed at the end of every “code” function, or when calling a method.
  • Semicolons do not have to go on lines where you are opening or closing a class or method definition, or where there is a bracket.

Example (Putting it all together)

Here is a small example of code you’ll be able to understand soon, that implements everything that you learned today! We’ll go into more detail about everything in the snippet as you learn more about Classes and the generic Java program.

import java.util.*;

public class Dog{
  public void bark(int noise){
    makeSound(5);
  }
}

Next time, we’ll go into the use of data types, programming environments, and finally start your first program!

Next Lesson