Last time, we took in a little hit of Java, just to see if we liked the taste. We checked out the basics and got a sniff of what the Hello World under Java had to offer. I don't know about you, but I could go for another taste! So, if you're up for it, let's grind a bit deeper, get under the grounds, and get into more of what makes Java, Java.
Recap
In the last entry, we went through the basics and came out with a simple "Hello World" in the Java language:
public class SayHello {
public static void main(String[] args) {
//I'm using println instead of print so we move to the next line after
System.out.println("Hello World!");
}
}
This solidified the mainstays of Java for us: The making of classes, object specification, class types, datatypes, one special function-return type, and the world Java navigates to log (or, in this case, print out) a simple phrase. All of this is very important. It's the java of Java, but having a cup of Joe is more than just the Joe. In true Java fashion, we need to take all parts into consideration. In coffee terms, the first question could be, "Now that I've made the coffee, how can I bring it to the table?" It's a simple and often overlooked question for most, and, if we're used to interpretive languages like JavaScript, it's the kind of question we'd tend to ignore altogether. It's a very important quandary, though, and one that Java keeps in mind with each creation. The answer, of course, is a cup. As Java goes, this "cup" is referred to as a compiler
Compiling
We've said previously that Java converts its code into a form called java bytecode so that any computer with a JVM can interpret and run it. To do this, we must take the natural java form and pour, or, in this case compile it for transport. Here with Java, we do the compiling manually from the command shell. To start, we, from the shell, call the Java compiler, give it the path to our class, or the classpath so the compiler can find it, and finish it all with a .java
javac SayHello.java
That simple (so far)! the javac command calls the java compiler, then we steer it where it needs to go. We should remember though, that the Java compiler needs a door to walk through. That door is a door we've already built in our previous Java code: the main function. The compiler will always begin its work in the main function of the class it is pointed towards (in our case, the SayHello class). The flow of code compiling in Java will flow like so:
note: The Java compiler is checking your code while it transforms. If your code doesn't pass the series of checks, the cup will refuse you.
When the in-built compiler finishes a successful compilation, it creates a .class file. In our above case, the file would be called "SayHello.class", but you'll hardly ever refer to it this way when running it. The program makes it a bit easier for us instead, assuming we mean class and allowing us to simply type:
java SayHello
In order to get to the class file in question. If we follow these steps correctly, it would go something like this:
- We create a class holding an object holding a function that prints to the equivalent of a console.
public class SayHello {
public static void main(String[] args) {
//I'm using println instead of print so we move to the next line after
System.out.println("Hello World!");
}
}
- We then move into the compiler in the shell, calling the compiler to come byte up this class.
javac SayHello.java
At this point, there are two SayHello files. One file class is SayHello.java, the other, SayHello.class. This is something you can check on your own with the shell's list command:
ls
SayHello.class SayHello.java
The .class file represents the compiled class, while .java represents the original. To implement the compiled class file in the compiler we don't need to use .class, though.
java SayHello
Hello World!
And there you have it! One complete cup of "SayHello" styled Java to take wherever we need it to be.
Conclusion
Today, we've taken the juicy Java we created previously and given it a cozy compiled cup to rest in before it finds its new home wherever it goes. The versatility of Java takes attention to detail and flexibility that is optimized in its compilation program. Next, we'll move even further into the Java process and maybe even following it all the way to this new home and beyond. Stay tuned, and happy coding.