Brandon Briones
Posted on August 26, 2020
Hello fellow Devs, in this blog I'm going to take a quick look at the high-level programming language of Java. New developers might think that if they know JavaScript then Java should be pretty similar. To my surprise that was a big nope, I saw there version of console.log('hello world') and let's just say it wasn't as straight forward as that.
First let's talk about the development of Java. When this programming language was being developed at Sun's MicroSystem in 1991 by James Gosling and company. James designed Java with C++ style syntax. So if you are coming from C++, then adapting to this language might be a smoother ride. Java is centered around the concept of object oriented programming or OOP for short.
To keep it short OOP revolves around four key concepts or pillars shown above. Then end goal of OOP is to manipulate objects and their contents to get results, big emphasis on Objects.
When learning a new programming language we have to start from square one. For me when first learning JavaScript one of the first functions I wrote was to print out "hello world" to the console. So let's take a look at some code and see how Java would do just that.
Just to compare below is the code that would do the same thing in Javascript.
Now you can see why I was a little shook when looking at Java's version. Let's dive right into it, since Java is OOP oriented we are going to be working with class to be able to create our Object. When looking at this code there's a couple of key words that strike my interest and I want to know what they are for.
Keywords
public
This is known as an access modifier. When taking a look at the second public, this is attached to a method. By having this method public, this allows other classes to have access and use this method. If you want it to be strictly for that certain class then by switching public
to private
that will make that method exclusively for that class.
Static
The next keyword of static
basically keeps that method from being instantiated. Which basically means this method does not need to become an object to be used, which is great for saving up memory space. When this method is used, it does not need to reference an object. So by simply taking out the keyword static the method will then become an instance of the class.
Void
Lastly void as you can guess means nothing, this just says that this method returns nothing. In the instance that it did return something this is where you would put the datatype to what is being returned.
At this very moment you might be thinking, there's a lot going on just to print out a simple hello world to the console. If you think we are done with just those 3 keywords think again sir or mam.
Another point of interest is the word main, this isn't a keyword but this is necessary to have in a class. Why? The answer is basically the Java launcher by default looks for a method called main which will automatically be called so it can pass control to the program. In the instance that you have another class with a main you have to tell your Java virtual machine which class to start from. Main also takes in an array of String Objects also referenced by args.
After you have a main when creating another method you can name the method whatever you want.
Now for the juicy part that does the actual printing to the console.
System is a class object that has a member of out, then out has a method of println that will make 'hello world' convert to a string object then print out to the console.
Now we can say that we are done, for now. Learning a new language might be a challenge but it is essential in becoming a well rounded developer. Especially in the job field the more languages you know the more opportunities you can come across!
Posted on August 26, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024