Going from JavaScript to Java.
Tags: javascript, java
Published: 2020-06-01 13:40:41

Welcome! How are you doing on this beautiful day? Or maybe it's night time, I'm not entirely sure, either way I hope you're doing well. As you can tell by the name of this blog title, I will be trying to give a little insight into the basics of Java. Let's not waste any more time and get straight into it.

Let's start with a little history. Java is a programming language that was first developed by James Gosling in 1995.Java is an object oriented class programming language that is typically used for server side development. Unlike JavaScript, Java organizes software design around data and objects rather than functions and logic.Java is an extremely popular language in the software industry. A few fun facts to show how popular it Java is, there are upwards of 9 million Java developers worldwide, around three billion phones that run Java, around 130 millions TV sets, and every single blue-ray player. With that being the case, there are many different reasons why you should use Java.

For one, Java is platform independent. Meaning once a Java program is compiled you can execute the program on any platform or operating system. This lends to the flexibility in how many different devices you can program with Java. Another reason you should use Java is that you can benefit from the object oriented programming style that the language offers. You can benefit a lot from using the object oriented style of coding from reuse-ability, error handling, organization, and maintenance. To give a little more context, let's take a look at some key differences between Java and JavaScript. 

One difference between Java and JavaScript is that Java is a statically typed language, while JavaScript is a dynamically typed language. Meaning declaring variables in Java is much more strict than in JavaScript. You have to explicitly declare what type of variable you are creating in Java, however that is not necessary in JavaScript. This may seem like a hassle at first, but it can reduce the time you may take finding bugs because of it.  
 
In order to do anything in Java, you must use utilize classes. Classes are reusable blocks of code that produce objects for real world examples such as a user signing up on your website. While in recent years JavaScript has updated to include classed, in no way is it required or necessary to run a program. It takes a lot more boiler plate code to run Java programs vs JavaScript programs. Again, this may seem like a hassle, however the fact that Java is so structured can greatly help find and identify bugs. 

Java applications and programs are run in a JVM, which is known as a Java Virtual machine. While JavaScript is mainly run in web browsers, but can be run in server side development with a runtime environment such as Node.js. This lends to the fact that Java is platform independent and it's so flexible. Much more flexible than JavaScript in my opinion. 

Now that we have some context on a few differences between Java and JavaScript let's look at some code.

```Java

public class Main {
    public static void main(String[] args) {
        // declare variable to store age
        int age = 30;

        // declare variable to store name
        String name = ("Michael");

        // declare variable to store if tacos are great
        boolean tacosAreGreat = true;

        // log in terminal
        System.out.println(age);
        System.out.println(name);
        System.out.println(tacosAreGreat);

    }
}
```

In the code example above is a basic Java program that will log the value of each variable to the console. Notice how the _public class Main_ is the first thing being created in this program. Like mentioned before, in order to do anything in Java you must utilize classes, you will get an error if otherwise. Another thing to take note of is how before every variable is the type of variable that will be created. This goes back to the fact that Java is a statically typed language.  Finally, notice how we actually print information in the terminal in Java via the _System.out.println_. This is how Java programs are able to print information to the terminal. Let's now do the same thing, but in JavaScript. 

```JavaScript
// declare variable to store age
const age = 30;
// declare variable to store name
const name = 'Michael';
// declare variable to store if tacos are great
const tacosAreGreat = true;



//log in terminal using a function
 const log = (() => {
     console.log(age);
     console.log(name);
     console.log(tacosAreGreat);
 })();


// log in terminal using a class constructor function
 class Log {
   constructor(){
     this.age = 30;
     this.name = 'Michael';
     this.tacosAreGreat = true;
  };

 logValues(){
     console.log(this.age);
     console.log(this.name);
     console.log(this.tacosAreGreat);
     };
 };

 const log = new Log;
 // call .log method
 log.logValues();

// log in terminal using a console.log
console.log(age, name, tacosAreGreat);
```

In the code example above is a basic JavaScript program that will log the value of each variable to the console. Unlike Java, JavaScript does not constrict you to any one way to set up a program. Another thing to take note of is how before every variable there isn't the type of variable that will be created unlike Java. This goes back to the fact that JavaScript is a dynamically typed language and does not have too many rules. Finally, the many different ways we can log information to the terminal in JavaScript. The code example above uses an immediately invoked function, a JavaScript class method, and just a console.log that all will do the same thing. The thing to take away from this is, unlike Java where you must set up your program in a specific way, JavaScript allows for much more freedom in how you wish to preform different tasks. 

In conclusion, even though Java needs a lot of boilerplate to start, it can save time debugging and adding onto code in the long run. The fact that Java is platform independent makes for a plethora of different use cases while learning the language. Also it can be very wise to learn and add to your tool belt along with JavaScript early on to help deepen your understanding of programming and add more value to you as a developer. Until next time!