DEV Community

Cole Rau
Cole Rau

Posted on • Edited on

Syntax Differences: JavaScript vs. Java

In this post I'll explain some syntactical differences between JavaScript and Java.

Declaring and assigning a variable

// JAVASCRIPT
let watermelon = "good";


// JAVA
String watermelon = "good";
Enter fullscreen mode Exit fullscreen mode

In JavaScript, you can declare a variable with let, const or var. You do not need to specify the variable's data type, like you do in Java. In the Java example above, we need to say the watermelon variable will point to the String, "good".

Printing/logging/outputting something

// JAVASCRIPT
console.log("tomatoes are gross");


// JAVA
System.out.println("tomatoes are gross");
Enter fullscreen mode Exit fullscreen mode

There is just a minor difference between printing in JavaScript vs. Java. Printing the contents of an array works a bit differently in Java compared to JavaScript, which I'll cover later in this post.

Variable interpolation

// JAVASCRIPT
let fruit = "blueberry";
console.log(`My favorite fruit is the ${fruit}`);


// JAVA
String fruit = "blueberry";
System.out.println(
  String.format("My favorite fruit is the %s", fruit)
);
Enter fullscreen mode Exit fullscreen mode

To put a variable in a string:

  • In JavaScript, inside the console.log, write your string with back ticks; insert ${variableName} where you want your variable to go.

  • In Java, inside the System.out.println, use String.format() to write your string; insert %s where you want your variable to go; after the ending quotation mark of the string, place a comma then write your variable name.

Declaring, invoking, and printing the return value of a function/method

// JAVASCRIPT
const isEven = (num) => {

  // do something with num and return the result
  return (num % 2 === 0) ? true : false;
}

console.log(isEven(4));




// JAVA
public class Main {
  static boolean isEven(int num) {

    // do something with num and return the result
    return (num % 2 == 0) ? true : false;
  }

  public static void main(String[] args) {
    System.out.println(isEven(4));
  }
}
Enter fullscreen mode Exit fullscreen mode

In both languages, we're passing an integer of 4 to the isEven function/method. Then, we determine whether the remainder of the integer divided by 2 is 0. If it is, it's an even number so we return true; else, the integer is not even so we return false.

In JavaScript, you can declare and invoke a function with the following general syntax:

// declare a function called functionName
const functionName = (parameter1, parameter2, ...) => {
  // return something
}

// invoke the function called functionName
functionName(argument1, argument2, ...);
Enter fullscreen mode Exit fullscreen mode

However, in Java, every method needs to be declared within a class (in the above example, the class is Main). Every parameter to a method needs to have a data type. If the method returns something, you need to specify the return value's data type (void means nothing is returned). Every method must be invoked within the main method (The Java compiler will look for a main method before it executes any other code).

The following is the general syntax for declaring and invoking a method in Java:

public class Main {

  // declare a function called functionName
  static returnValueDataType functionName(parameterOneDataType 
  parameterOne, parameterTwoDataType parameterTwo, ...) {

    // return something 

  }


  public static void main(String[] args) {

    // invoke the function called functionName
    functionName(argument1, argument2, ...);
  }
}
Enter fullscreen mode Exit fullscreen mode

Declaring and printing an array

// JAVASCRIPT: 
let array = ["strawberry", "orange", "lemon"];
console.log(array);


// JAVA:
public class Main {
  public static void main(String[] args) {
    String[] array = {"strawberry", "orange", "lemon"};
    for (int i = 0; i < array.length; i++) {
      System.out.println(array[i]);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

In JavaScript, the elements in an array go inside of square [] brackets. In Java, the elements in an array go inside curly {} braces. Also in Java, you need to specify what data type the elements in the array will be. This is why we write String[] above. The String tells Java the array elements will be Strings. The [] tells Java you're creating an array.

Since Java doesn't natively print out the contents of an array with System.out.println(arrayName), you need to loop through the array, printing each element for each iteration. This is why the above example uses a for loop to print each element in the array.

Top comments (3)

Collapse
 
bias profile image
Tobias Nickel

Sometimes I wonder how much java and js code could be written intechangably. now that Java has the var keyword and arrow functions.

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Not much, actually. While the syntax is rather similar the language design is very different and the runtimes are even more different.

Java is much more similar to C# in their goals and language design, so they might be what comes close to "interchangeable".

Collapse
 
colerau profile image
Cole Rau

I didn't know Java has those features now!