My Blog

Learning Java Script from Java Point of View

June 06, 2020

Writing a program

Let’s write a simple java program

Math.java

public static class Math{
  public static int sum (int a, int b){
    return a+b;
  }
}

Writing the same program in javascript

Math.js

const sum = (a, b) => {
  return a + b
}

Executing the program

To execute the java program you need a Java Application and JVM. A java application is a class that has main method in it. Let’s write a java application to use our program.

App.java

public class App{
  public static void main(String[] args]){
    int val = Math.sum(5,7);
    System.out.println("Sum is:" + val);
  }
}

After writing the program you need to compile and run the java application App.java.

To execute the java script program you need a html which loads the script file in the Browser and its java script engine Please read the above statement with a grain of salt. There are other ways of executing javascript.

In order to load java script on the browser, let’s create an HTML.

index.html

<html>
  <head>
    <script src="Math.js"></script>
    <script>
      var val = sum(5, 7)
      console.log(val)
    </script>
  </head>
</html>

JVM and Java Script Engines

  • IBM J9
  • HotSpot
  • V8
  • SpiderMonkey

Writing Tests

  • JUnit Test
  • Test NG
  • Jest
  • Karma

Writing a more complex program

Create a Java project using maven

Create a Java script project using npm


© 2020, Arun Abraham Home