Java and Javascript are really two very distinct languages, but they do both share similar underlying C-style syntax. If you're using the Javascript exercises on this site but are actually trying to learn Java, here are a couple key differences to be aware of:
Java |
Javascript |
|
Variable Types |
Java has many different types of variables, e.g. int, double, and String. | Javascript only has one generic type of variable that holds numbers, strings, objects, etc. |
Declaring Variables |
Variable type is specified: |
Variables are declared using the keyword "var": |
Function Headers |
Function name is preceded by keywords like "public" and "static", as well as the variable type of the return value: |
Function name is preceded only by the word "function": |
Function Arguments |
The formal arguments in a function header are prefixed by their variable type: |
The formal argument names in a function header have no prefix: |
|
Uses libraries to print text to the console, e.g.: System.out.println("Hello world!"); |
Has a function to pop up a message box: alert("Hello world!"); (however, some of Java's input/output functions can also be used within the Javascript exercises on this site) |
Dividing Integers |
In Java, dividing two integers returns another integer, so 12/5 returns 2 (the decimal portion is cropped). | Since Javascript doesn't actually have integers (all numbers are stored as 64-bit floats), 12/5 returns 2.4. |
Array Size |
The size of a Java array is fixed at creation (new positions can't be added to the array later). | Javascript arrays are dynamic; elements can be added or deleted later on. |
Array Initialization |
int[ ] myArray = {4, 7, 16, 3, 2, 9, 4}; | myArray = [4, 7, 16, 3, 2, 9, 4]; |