It feels like you can't go anywhere in the coding world without hearing about Java! Maybe I should stop doing all my coding in coffee shops, but I 'm not sure that would help as much as you'd think. I decided to get to the bottom of this programming language, but to do that, we've got to start at the top! If you've got a few minutes, come take a shot of Java. Yes, of course you can bring your own coffee, we're saying Hello to the World!
What is Java?
Java is a computing platform and a "high-level, general-purpose, memory-safe, object-oriented programming language. It was designed by James Gosling and first released by Sun Microsystems in May 1995. It's a very popular (third most popular programming language on Earth in 2022, according to GitHub) programming powerhouse, run under the mantra Write Once, Run Anywhere(we'll get to that in a bit). Java is used by platforms like LinkedIn, Amazon, and Netflix, coded in games like Minecraft, Runescape, and Angry Birds, and used by over 3 billion devices around the world. Run Anywhere, indeed. Okay, that's enough about where Java is run, now it's time to talk about how.
Syntax
Java syntax is largely influenced by C and C++, but any JavaScript programmer is bound to see a few similarities (note: though they sound like it, Java and JavaScript actually have no direct relation to each other. Further, Java is a compiled language, as JavaScript is interpreted). The comment/pseudocode structure is the same in both languages, and the basics seem pretty intuitive when coming from a JavaScript background, with a few caveats. Let's take a look at how Java makes a standard Hello World statement.
Hello World!
I know I said JavaScript programmers would feel somewhat at home, but there are big differences. Take logging "Hello World" into a console for instance.
Doing this in JavaScript is pretty straight forward:
console.log("Hello World");
See? Nice and succinct, straight forward (and built in). With Java, we have a few more steps to work through.
-Have some class
First off, as was said earlier, Java is an object-oriented language, meaning, especially in the case of Java, that every data item is an object. Still, in the Java structure, an object item will live inside of a class, and all code is written inside of a class. With that in mind, before we do anything, we'll need to specify a class. Java does this like so:
public class SayHello{}
To specify a class, Java must designate what sort of class it is. Public is the most permissive sort, and any other class can access a public class. Other classes include, but are not limited to: final, static, abstract and concrete.
- Choose an object -ive
Now that we have and understand our class, we need to add an object to it.
Remember, every data item is an object, so we must specify what kind of object it is:
public static main(){}
there are a couple of things to mention here about this specification, and specifications of this in the future:
- Public: This is the same as the class designation above, and signifies public access
- Static: Static signifies that this method belongs to this class itself, and not a particular instance of it.
- main: The name of the method. The runtime sees main as the starting point of the program.
With these three things, we have our object, and we should add it to the class. There's still a couple of things missing here before we can finally move on to the actual command..
- opening arguments
We should allow the potential for other things to be passed into this method. To do that, we must specify the datatype of the arguments, or args. The reason for this is that, in Java, we need to tell the runtime what datatype to expect on its way to read the methods, or really anything, for that matter:
public class SayHello{ /*did I mention variables capitalize every word, instead of leaving the first word lower-cased*/
public static main(String[] args){}
}
- what's your type Here, we've designated the type of arguments to be an array of strings, so now we should be good to... wait a minute. Didn't I say we needed to tell the runtime what datatype to expect for "really anything"? What should we tell it about what to expect to come out of this function? Well, like the console log in our javaScript example, we don't really expect anything to come out of the other side of this method. So do we really need to tell the runtime to expect nothing?
public class SayHello{
public static void main(String[] args) {}
Yes, in fact we do. and the void keyword does just that. Now that the runtime knows what to expect, and the class, object and arguments are well typed and defined, we can move on to the actual method.
- Take a byte
In the JavaScript example, we simply invoked a console function to get our iconic phrase, why is that? Because JavaScript runs on web applications, and thus, can expect there to be a console. Java doesn't make these sorts of expectations. As Java goes with its WORA mantra, it can't really expect what system it will be run in. No matter the code here, when we're done and compiled, Java will convert it all into a form called Java bytecode. This preps it for its eventual journey through its VM (virtual machine) and into whatever kind of web environment, game system, or anything in the vast internet of things we've created in the world. So how do we print to the console with no console... well, maybe no console.. Where are we anyway? What's a "code"??.. Sorry, got lost in the vastness. The answer is we don't need to call a console to tell our program to print, we can just do it directly:
System.out.print("Hello, World!");
- All together now
See what we did here? We simply called the system, whatever system it may be (System) to use its output(out) equivalent of a console to print our key phrase. As complicated as it has to be, given the staggering amount of environments Java can potentially find itself in, but pretty simple when it's broken down. Let's put it all together and see what we get:
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");
}
}
And there we have it! A good 'ol "Hello World" from the wonderful land of Java.
Conclusion:
Java is an amazingly versatile programming language that deftly uses specifics and expectations to work itself into any environment in which it may find itself. Thanks for coming by to say hello, and I hope you come away with more understanding than you got here with. Till we code again!!
PS: Funny coincidence, it turns out that the release-candidate for the absolute newest build of Java (24) just came out... TODAY! If you're up to take a stroll on the cutting edge, I've left a link to the Build download below. Happy Coding!
Resources:
- https://www.java.com/en/download/help/whatis_java.html
- https://en.wikipedia.org/wiki/Java_(programming_language)
- https://www.geeksforgeeks.org/types-of-classes-in-java/
- https://www.geeksforgeeks.org/java/ ####Get the latest Release-Candidate Build for Java 24!
- https://jdk.java.net/24/