Welcome, welcome! Moving from Python, we’ll be looking at Java - AWW YEA!Now Java is by far one of the most important languages a CS major or really anyone involved in the software industry is sort of expected to understand (at least a bit!).Python and Ruby, while they are quite exquisite in their own right, with their easy to understand English-like syntax, nothing really beats the big “Daddies” CS majors recognize as C and Java.Personally though, I feel Python rules over all. More on this in another post.Anywhoozies, as a final note, it’s a general rule of thumb that once you really get the gist of one programming language, it’s not as difficult to become involved with others.It’s kind of like when a man, let’s call him Brad, really understands how one woman works. After Brad gets tired of the first woman because he’s completely figured her out and wants to move on to bigger and better, it’s easier for him to grasp the inside workings of other women (unless that woman is an alien, than tough luck Brad, you’re gonna have to try much harder).Sure, there might be other features that define the other women differently to make them unique, but given that he was able to make the first woman work correctly for him (or compile - what have you), he’s pretty much done with the rocky waters, and on to Fiji. Unless of course, his first woman was easy, than it’s gonna be harder for him.An example of an easy women in terms of programming languages would be Python, a harder to approach woman would be C, and an alien in that same context would be Lisp. Take from all this what you will.Okay, THAT was kind of a silly example but I think it has some merit, so boo hoo.We’ll be performing the following operations using the appropriate Java program structure and syntax:
EASY!I’m going to provide the complete program that compiles correctly on Github, so that if you get stuck or anything, you can look at that. Cheers!Let’s discuss each section of this program then… starting with…
With Java, when we save the file, we want to name it the same name the class has.You see, the basic structure of a Java program looks like this:public class First_Program {
public static void main(String[] args) {
}
}If you decide to rebel and not save the file with the class name (let’s say you saved this file instead as Second_Program), then it’s going to give you an error. DUN DUN DUN! Save yourself the trauma and just save it CORRECTLY.For this program, we will be naming the file and class name after something fun like our favorite TV show.So my file name would be Hawaii_5_0. This also means I would name my class file Hawaii_5_0.That’s right, you stupid time lord alien humanoid lovers!!!I LAUGH AT YOUR MULTIPLE FACES DOCTOR WHO.
With any well written program, there are comments scattered in areas where the user might want to explain more complex method he used. There’s also a good amount of spacing between multi-line statements so that the program is easier to read and a poor little programmer won’t be pulling her/his hairs out just to figure out what the heck you were doing. Or if the programmer is bald/doesn’t have a lot of hairs, he’ll just be SHH (shaking his/her head). Either way, we wouldn’t want THAT to happen, so make your program presentable. IT’S ART PEOPLE!There are also comments usually at the top stating who the program is for, the program’s purpose, maybe the date of the creation, the creator’s name, small things like that.There are 3 formats for a comment:// Single line comments
/* Multi-line comments */
/** Documentation comments */^doc comments aren’t really used too often for beginner courses, they’re just the de facto industry standard to document Java classesFor this program, we will insert a comment that contains your name, program number and name, date, and location where it was created (in a stupid time machine, in Dr. Farnsworth’s house, your smelly dorm room, you get the idea).
Now there are primitive data types in Java and then for beginners, there’s that one reference variable - strings.Strings are just strings of text basically and are declared using the keyword String, likeString movie = "AVATAR";To print this String statement out we would use the System.out.println() function.System.out.println(movie);We can use this function to print out quotation marks too and multiple segments of code, using the + operator like this:System.out.println("I'd have to say that my favorite movie is " + movie + ".");That statement would output:I’d have to say that my favorite movie is AVATAR.A fancy way of saying what I just did through combining variables and quotations is concatenation. Big whoop.Any how, a special trick I learned with the System.out function is that you can use this statement at the beginning of your programimport static java.lang.System.out;
And now you can just say