Introduction: Programming and Java
This article/series of videos will be aimed for beginning programmers or anyone who wants to learn the Java programming language. Except for the introduction video, each subsequent video is meant to be very short and will be used to simply speak about the programming concept as well as demonstrate the code in Java.
Here are the main points that you need to know:
The Java Series
Instead of article form, this series will be in youtube video series so I can speak clearly and we can learn about programming and Java quickly.
The github link for this series can be found here.
Video 0: Introduction and Goals
Video 1: Setup and Hello World, Java
Install Java using the following commands:
If you're using Windows and really want to use a build tool, see one of the appendix sections near the end of the article.
Create a file named MainStartup.java and enter the following code in it:
Compile the code, and then run the executable that's created by using the following two commands:
Video 2: Basic Data Types (Variables)
Video 3: For Loops, If Statements
Video 4: Functions
Video 5: Arrays, ArrayLists, and Import
Video 6: Classes
Create a new file for our new class. Name the file "srcMathLibrary.java" and enter the following code into it:
Finally, our MainStartup.java file will test our class out.
Video 7: Object Oriented Programming, Inheritance, Abstract
Some classes act as base classes, that other classes are derived from. For example, Dogs are a type of Mammal, and Mammals are a type of Animal. Since we may want to make classes for Fishes, Amphibeans, Frogs, etc. in the future, a good object oriented approach is to plan classes by a hierchary.
We start with Animal.java, where we define an abstract Animal class (which means that it must be filled in by the derived class before it can be used):
Next, we extend the Animal class by created a new file named Mammal.java for all of our mammals.
Next, we can make a Dog.java file with some Dog class code.
Finally, our MainStartup.java file will create an instance of the Dog class and use its methods.
Video 8: Complex Data Structures
Video 9: Generic Classes/Methods
Generics allow us to code so that one set of code will work no matter which data type the user's data structure comes in. For example, sorting an Array of Integers isn't that different from sorting an Array of Doubles or Longs. The only real difference is data type. To keep our sorting code concise, and to make it easier for the user, we use generics so that our classes (and methods) can handle different data types.
First, we create a helper class in a file named "ArrayUtility.java", to show a generic method that can print any array, regardless of the type.
Next, we create a "GenericClass.java" file for a class that declares a type for the class, so that certain methods and variables can be of that type. (This is what all the data structure classes do.
Finally, our MainStartup.java file will test the generic method and class.
Video 10: Interfaces
Interfaces do the same thing that abstract classes do: they specify certain methods and variables that a derived class must define.
The reason to use interfaces instead of just abstract classes is that a derived class may inherit from multiple interfaces, but only from one class. Variables in interfaces are final (meaning unchangeable).
Create a file named "Dog.java" and add the following code to it.
Test the Dog class in "MainStartup.java".
As mentioned, all variables in interfaces are final. Add the following to line 14 of Dog.java and try to make the program, again.
The code won't compile because you're trying to change a constant variable.
Video 11: Annotations
Video A: Building with Maven
Maven is a Java build tool. Here's a reference article. The reason you would use it is because it manages your java dependencies for you. Not necessary for our tiny programs, but in real world huge applications, there are many dependencies with different versions and compatibilities, and so Maven manages all of that for you.
Install Maven with the following command in your terminal. (The next line ensures that it's working.)
Okay, before we use Maven, we need to take care of some issues that may come up. The first thing is to make sure that your JAVA_HOME environment is set. (If not, Maven know what what Java compiler to use.) Find your java download location and make sure it's set. On Ubuntu in the command line:
Find the name of the folder for your Java installation. In my case, it's "openjdk-8-jdk". Next, set your JAVA_HOME environment variable to point to this path. In the command line:
And that should set our environment variable.
Now, make sure that your java is actually up-to-date. This may not be necessary, but for me it was because openjdk-8 didn't install everything the first time I installed it. In the terminal...
FINALLY, we can use Maven itself to build a project. Create a new project folder, and cd inside of it. Then type:
This will start creating a new Maven project. Press enter through everything, but for the following, you need to set some project IDs up.
For groupID, say com.srcmake.builder. "com" is standard, "srcmake" is the company/website name, and "builder" is the project name. ArtifactID is also the project name. Sort of...The version will be 1.0.
Again, for everything else, just press enter.
The project will be made, and there are three important files right now.
1. pom.xml - This file manages our project, from dependencies, libraries, names, versions, etc. This is defines our project.
2. test - Has a bunch of folders for our project's unit tests.
3. src - Holds our project's Java source code.
Let's build our project. In the root directory, in the command line:
If you see a failure...google it. If you see a success, then great. That just compiled our code from the src folder. It also created a bunch of stuff in the newly made "target" folder. To actually run our Java code, cd into the classes folder and java run our groupID and app entry point.
And you should see Hello World.
That was a lot of work, compared to our old method, right?Our command line way of using javac and java to compile is very good for quick programs, but any real project should be built using Maven or Gradle.
For a more in-depth look, see this resource.
Video B: Building with Gradle
Gradle is another build tool to manage our Java projects.
To install Gradle, in the command line:
Next, we create a project using Gradle. Then we build and run it.
And that's it.
Video C: API Project (Spring)
The spring tutorial can be found here.
Video D: Deploy an API onto a Heroku Instance (Cloud API)
Follow the instructions on heroku's website for beginning a Spring Java project.
To use the Spring commands, download the tool, navigate to bin, and use the spring file as a script to run the command's you need.
Then copy the myapp folder with our starter project back into our actual project directory.