Java Bits - Getting Started

Java Bits - Getting Started

The first bit in a mini-series for beginners in Java.

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:

  1. Saving the file appropriately
  2. Commenting nicely
  3. Creating variables
  4. Reading said variables from keyboard
  5. Calculating results with variables
  6. Printing said results

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…

1. Saving the file appropriately

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.

2. Commenting nicely

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).

3. Creating and printing variables

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

out.println("Haha, no more System.");

instead of

System.out.println("Aww, System.");

because the System part has already been imported with that statement. Just a small trick.Also in Java, there are semi-colons after each statement, if you didn’t catch that already. -_-So back to the other kinds of variables, there’s this nifty mnemonic device to remember the 6 primitive data types.BUY - byte STUFF - short IF - int LOOKING - long FOR - float DRUGS - doubleThere’s also this chart I got from my coursework in Java LearnStreet course…

The course was nice by the way (great for beginners), it’s basically like Codecademy for Java except it costs $40, but I think I got my money’s worth, so there.For this program, we’ll be declaring 3 int variables and call them a, b, and r.We won’t print these out yet, that’ll be discussed later on in the lesson.

4. Reading said variables from keyboard

But instead of declaring them or hardcoding them to certain values, we’ll ask what the user wants to assign them by using the Scanner class.A Scanner class in Java can be created by an import statement in the beginning of the program.import java.util.Scanner;Then we create a Scanner object.Scanner keyboard = new Scanner(System.in);This object that I call keyboard although you can call it whatever you want lets us get input from the user throughout the program.Then we can use this Scanner key word to get our input.String tv_show = keyboard.nextLine();Now whatever the person types in, Java will get the input and store it into the String variable tv_show.nextLine() is used for Strings nextInt() is used for int nextDouble() is used for doubles and so forth…So the object doesn’t have to be keyboard, I could have called the Scanner “user”, and it still would have worked out the same way, like this:String tv_show = user.nextLine();

For this program, instead of defining the int variables a, b, and r with certain values, we’ll ask the user to give us numbers. So import the Scanner class, create a Scanner object, and get values from the user using the object.

5. Calculating results with variables

Okay here’ s the more tricky stuff. Making equations that the compiler can understand and compute. Then again, if you’ve ever taken a math class and had to use the calculator a lot, this might come off as easy for you. Basic rules with these equations are to use parenthesis and exponents in the right places.Java has the built-in function pow. It’s formatted as pow(x, y) where x is raised to the y.So let’s say we wanted to compute 3^2 or 3 to the power of 2, which is 9 for the ape in all of us.So in this case we could write that pow(3,2) and that too would give us 9. Huzzah!For this program, we will declare result1, result2, result3, and result4 and initialize them to the following four equations shown here:

For the equation of result4, I realize it is 4r5. Yes, it could be written as 20r, but just write it as it is. Deal bro. Come on now.

6. Printing said results

Now for easy stuff. Using the System.out.println() function and the + operator we learned already, we’ll print all this shiz out.For this program, print out the values of a, b, and r, with a little message like this:“a is equal to: "Also print out the values of result1 through result4 using an appropriate message like this:“Result 1 is equal to: "

Subscribe to my newsletter Today

In my newsletter, I share personal and career learnings, advice, collaboration opportunities, and more.