DEV Community

Donny
Donny

Posted on

Java or Bust: A New Coder's Journey Into the Unknown

I was just fresh out of code school, with nothing but bright prospects in front of me ( so I hoped) and then---Coronavirus hits. Hiring comes to all but a halt and, as my Career Coach advised me, “Yes, some companies are still hiring, but they can afford to be very picky. It’s going to be harder now than before CoronaVirus to get a job.”

With those somewhat dimmer prospects in view as I sat at home in “shelter in place” with my husband here in San Francisco, I began, first out of boredom, to look at other coding school’s websites. I’m not sure why I did that. After all, I had just spent 3 months at a coding school. Nevertheless, as I was about ready to click onwards from one school’s site, it noticed they were offering a “COVID-19 Mentoring package.” Long story short, I decided to take them up on the offer. I was assigned a mentor and began the study of Java. Up to that point, my coding education had been centered around Ruby and JavaScript. What was this Java thing about? How different would it be from JavaScript? I would soon find out as I began my Java studies in earnest using the learning platform this coding school had generously made available to help people sitting at home during the pandemic.

Let’s Start With the Dessert First

So let’s start the meal with the dessert first--a more sensible way to dine, in my opinion. I’ll go right to the core of the matter. When you make the switch from JavaScript to Java it’s like making the shift from an automatic shift car to a manual shift. With automatic, lots of stuff is done for you under the hood. You can just assume lots of the details are taken care of and off you go. With stick shift, you have to do a lot of the work yourself.

Let’s have a look:

Declaring Variables

In JavaScript when you declare a variable, you don’t have to specify what kind of variable it is. It’s just :

const myString = “some string”

or

const myInteger = 5

or

const myBoolean = true

However, in Java the above statements won’t cut it. You have to explicitly state what kind of data type your variable represents. So if we rewrite the above JavaScript variable declarations into Java, it would look like this:

String myString = “some string”;
int myInteger = 5;
boolean myBoolean = true;

Oh yes, note that in Java, semicolons at the end of a statement are mandatory. This is in contrast to JavaScript where those semicolons are optional--and becoming less and less common in my observation. In fact, my “favorite” mistake in writing Java code is to forget to put the semicolons at the end of the statement.

In addition, Java breaks down its integers types even further. In addition to int (integer) you can have a “long” data type. You use “int” as a data type for integers between -231 to +231-1. You use the “long” primitive type for any integers outside of that range. (Think super big or super small numbers).

Lastly, let me point out how Java makes it all “go”

In JavaScript, we can make things run in the browser. Below, we make an alert box displaying ‘Hello, World!’ appear:

<html>
    <body>
        <p>Beginning text</p>
        <script>
            alert('Hello, World!')
        </script>
        <p>End text</p>
    </body>
</html

However, Java is an object-oriented language which means you always gotta have an object to do anything. So every Java program has got to have one Main object to run the program. This main object is called the main method:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Notice how we have the class “HelloWorldApp." And then nested inside HelloWorldApp is the “main” method. In our example, the only thing “main” does is print out a statement `a la console.log written like so:

System.out.println(“Hello World”).

I’ll have to admit, at the beginning all this Java syntax looks pretty strange. In addition, there are a lot more moving parts to learn than in JavaScript. For example, in the main method’s “signature”--
“public static void main(String[] args) -- all those words represent different “speeds” you can “shift” to (going back to my manual car shift analogy). I’ll teach you just one of those now: “Void” tells us we don’t have any returned output. If we expect an output, we would then have to substitute the keyword “void” with the keyword of the data type we expect to return. So if our method were adding 1 + 1 and we expected the integer “2” to be returned, then we’d have to substitute the keyword “void” for the keyword “int” in the method signature.

Whew! Even that little bit seems like a lot. It’s hard when you start studying Java from a JavaScript background that so much is NOT done for you in Java. However, after a short while, you begin to feel how much more power and control you have under your computer keys. Now I know why some people prefer driving a stick shift over a manual transmission.

Keep on coding out your dreams.

Until we meet again,

Donny

My gratitude to Ryan Desmond and Martin Breuss of Coding Nomads who gave me this opportunity to start my journey coding in Java. Special thanks to my always encouraging and enthusiastic mentor, Kelly.

Namaste

Top comments (0)