Hello everybody,
This is Michael, and first of all, Happy New Year! Can’t believe it’s already 2019! I hope you all had a wonderful holiday season. I also wanted to say thank you for reading my posts in 2018, and I hope you learned some new skills along the way. Get ready for some exciting programming and analytical content in 2019!
Now, since is the first post of 2019, I thought I’d try discussing something new-Java. Some of you might be wondering “What is Java?” Java is basically a general-purpose computing language that is used for Android apps (whether on the phone or tablet), server apps for financial services companies (think large hedge funds like Goldman Sachs), software tools (like NetBeans-more on that later), and games like Minecraft.
One thing to keep in mind with Java is that is more for building applications and programs. Java isn’t really useful for statistical analyses like R is, or database building/querying like MySQL.
Now to understand Java, I will discuss a concept called IDEs, or Integrated Development Environments. IDEs are basically the software systems you will use to write, test, and debug your programs. My personal favorite IDE is NetBeans, which is what I will use for all of my Java posts (there are other IDEs to choose from like JCreator, which I’ve used but didn’t like as much as NetBeans). NetBeans, and most other Java IDEs, are free to download; another perk is that they will point out and explain errors in your code (whether syntax or otherwise) by displaying a red line underneath the line(s) of code with the error(s). You might also see yellow lines underneath certain portions of code, but that is usually displayed to suggest how to make your code neater rather than to point out errors in your code.
So let’s get started and open up NetBeans:

This screen is what you will see every time you open up NetBeans. The other tabs contain other Java projects I’ve done in my spare time.
How do we create a new Java project? Here’s a video showing how to create a new Java project-Java Lesson 1.
As you can see, you must first create a project, then create a class. You can have several classes in your project, but for now let’s stick with a single main class.
Once you finish creating your project and class, you should see your interface, which looks like this:
The interface is where the magic of Java programming happens; here, you write, run, and debug the code you will use for your program.
Now let’s demonstrate the programming process using the most famous example for beginner coders-the “Hello World” program. Here’s a video demonstration for anyone interested-Hello World demo.
Some of you are probably wondering “What does all this code mean?” Let me explain.
- A package is a mechanism for grouping classes. The package
javalessonsgets its name from our main class-JavaLessons. If we had several packages in our project, then they would all belong to thejavalessonspackage.- There are two types of packages-user defined (like the
javalessonspackage) and built-in (which come with Java). Some built-in packages include:java.util– contains utility classes which implement data structures such as Dictionary; commonly used for Date/Time operationsjava.awt– commonly used for graphical interfaces purposes (such as creating buttons or drop-down menus)
- There are two types of packages-user defined (like the
- Two concepts that you should know are objects and classes, which are the two most fundamental concepts in Java.
- Java is an object-oriented programming language, which means that Java programs consist of objects that can either act alone or interact with one another.
- Think of it this way-the world is full of objects (people, trees, dogs, food, electronics, houses, etc.). Each of these objects can perform several actions, and each action affects some of the other objects in the world. For instance, people buy and eat food, while dogs pee on trees.
- Simple enough, right? Let’s say we had a program that analyzes the inflow and outflow of animals at an animal shelter. The program would have an object to represent each dog or cat that enters or exits the shelter as well as objects for cages, people that surrender/adopt animals, and so on.
- Every object has characteristics, or attributes. Using the aforementioned shelter example, animals have names, ages, genders, breeds, medical conditions, and so on.
- The values of an object’s attributes give the object a state. For instance, a dog can be named Spots, be 10 years old, male, and a Chocolate Labrador. The things an object can do are its behaviors; using the example I just mentioned, dogs can bark, growl, roll over, eat, sleep, poop , and so on.
- In Java, each object’s behavior is defined by a method (more on that later)
- Similar objects of similar data types belong to the same class, which is essentially a blueprint for creating objects. Using the dog example, all Chocolate Labradors that enter or exit the shelter would belong to the class
ChocolateLabrador.
- Java is an object-oriented programming language, which means that Java programs consist of objects that can either act alone or interact with one another.
- You’ll notice that the words
package,public class, andpublic static voidare all in blue. That’s because they are reserved words, which means you can’t use them as the names of objects or variables because these words are already reserved for the syntax of Java programming. Here’s a handy glossary of reserved words-Reserved Words in Java. - One of the first methods programmers learn is the
public static void main (String [] args)method; this method is essentially the entry point for Java programs. This method contains the block of code that will perform a certain action-in this case, display our output. Let’s break it down word by word:- public-meaning any object (whether in the same class or different classes) can use the main method
- static-means the method belongs to its class and not to any particular object
- void-meaning the method doesn’t return any value
- main-this is just the name of the method
- String [] args-meaning the necessary parameters for this method are arrays of Strings; arrays are just linear arrangements of things. I’ll cover arrays more in a future lesson.
public classmeans the class can be called from anywhere. For instance, if you were trying to create a game with several different character classes, then provided that your game class is public, the class (and any public methods) can be called by any of the character classes.System.out.println(string)is one of the first lines of code beginner Java programmers learn. This line of code prints out whatever is in the parentheses (which is usually a string). In this case, we’ll be printing out “Hello World” (I know it’s cliche, but it’s a great first Java lesson). Now let’s break down this line of code word-by-word:- System-a built-in class that contains useful methods and varaibles
- out-a static variable in the System class that represents the output of your program
- println-the method that prints a line of code
Now, before I go, I want to mention a little caveat when it comes to the println method-the difference between it and the print method. See, since println and print are so similarly named, you might think you can use the two methods interchangeably and get the same result. Not true-check out the pictures below for proof.
Granted, I’m trying to do the same thing in both examples (print “Hello World” twice). But in the first picture, since I used println both times, the two instances of “Hello World” are displayed one on top of the other. In the second picture, where I used print both times, the two instances of “Hello World” are displayed right next to each other without a space.
Thanks for reading, and here’s to expanding our analytical and programming knowledge in 2019!
Michael