├── Hello.java ├── README.md ├── code ├── Calc.class ├── Calc.java ├── Hello.class ├── Hello.java ├── Ops.class ├── Ops.java ├── Person.class ├── Person.java ├── Rect.class ├── Rect.java ├── Sum.class ├── Sum.java ├── calc.js └── hello.js └── hello.js /Hello.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mjhea0/java-for-javascript/a7fb525a439e9b3594a80459d58ef4847704f7b3/Hello.java -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java for JavaScript Developers 2 | 3 | ## Java? 4 | 5 | Java is an object-oriented programming language. It's compiled to byte code 6 | which can then run on any Java Virtual Machine (JVM). 7 | 8 | ### Why Java? 9 | 10 | 1. **Fast**: Java is fast! To execute a program you must first compile it down to byte code. The Java Virtual Machine (JVM) then interprets the byte code much like how a JavaScript engine (like v8) interprets JavaScript. However, byte code runs faster since it is much closer to the native language of the computer. 11 | 1. **Agnostic**: Once compiled, it can be ran on any computer that has the JVM installed. Write once, run anywhere. 12 | 1. **Strict**: Java is great for large development teams since (a) the language itself is statically-typed so it's much stricter than JavaScript and (b) errors are generally caught earlier in the compilation phase. 13 | 1. **Popular**: It's the most widely taught programming language. Plus, Java developers are typically the highest paid. 14 | 15 | ## Hello World 16 | 17 | JavaScript: 18 | 19 | ```javascript 20 | console.log('Hello World!'); 21 | ``` 22 | 23 | Java: 24 | 25 | ```java 26 | public class Hello { 27 | public static void main(String[] args) { 28 | System.out.println("Hello World!"); 29 | } 30 | } 31 | ``` 32 | 33 | Save this in a new file called *Hello.java*. The file name *must* be the same as the public class. 34 | 35 | ### How do we run a Java program? 36 | 37 | Unlike JavaScript, we can't just run the code as is. It's a two-step process: 38 | 39 | ```sh 40 | $ javac Hello.java 41 | $ java Hello 42 | Hello World! 43 | ``` 44 | 45 | The first command compiles the source code down to the byte code, while the second executes the code. 46 | 47 | ### What's happening? 48 | 49 | Let's jump back to the code... 50 | 51 | ```java 52 | public class Hello { 53 | public static void main(String[] args) { 54 | System.out.println("Hello World!"); 55 | } 56 | } 57 | ``` 58 | 59 | First, we defined a `public` (available to anyone) class called `Hello`. Then we defined a `public` method called `main`: 60 | 61 | 1. `public` - a type of method that anyone can call 62 | 1. `static` - a static method is part of class, but we do not need to create a instance to call it 63 | 1. `void` - indicates to the compiler that this method does not return a value 64 | 1. `args` - name of the parameter 65 | 1. `String[]` - value of the parameter, an array of strings 66 | 67 | The main method is executed when the file is ran. Let's pass in some arguments: 68 | 69 | ```java 70 | public class Hello { 71 | public static void main(String[] args) { 72 | System.out.println("Hello " + args[0] + "!"); 73 | } 74 | } 75 | ``` 76 | 77 | Compile then run: 78 | 79 | ```sh 80 | $ javac Hello.java 81 | $ java Hello Java 82 | Hello Java! 83 | ``` 84 | 85 | ## Data Types 86 | 87 | Since JavaScript is dynamically-typed you don't have to declare function or variable types. JavaScript engines just figure it out. Java, on the other hand, is a statically-typed language. You have to explicitly declare the types of variables, parameters, and return values. Further, you cannot change a type once it's been declared. 88 | 89 | Java has primitive and reference types. Primitives are the most basic building blocks, while reference types are based on a class made up of primitives. 90 | 91 | - Primitives: `int`, `long`, `float`, `double`, `boolean`, `char`, `byte`, `short` 92 | - Reference types: `String`, `Scanner`, `Random`, `Die`, `int[]`, `String[]` 93 | 94 | ## Variables 95 | 96 | Again, you must specify a type when declaring a variable: 97 | 98 | ```java 99 | public class Sum { 100 | public static void main(String[] args) { 101 | int num = 2; 102 | System.out.println(num); 103 | } 104 | } 105 | ``` 106 | 107 | Compile then run: 108 | 109 | ```sh 110 | $ javac Sum.java 111 | $ java Sum 112 | 2 113 | ``` 114 | 115 | Variables declared inside of a method are local to that method and are called *local variables*. *Instance variables* are available anywhere in a class and can be used in any non-static method. 116 | 117 | ## Classes 118 | 119 | JavaScript utilizes prototypes to create reusable behavior, while Java utilizes classes. 120 | 121 | ```java 122 | public class Person { 123 | 124 | public String name; 125 | 126 | public Person(String name) { 127 | this.name = name; 128 | } 129 | 130 | public String name() { 131 | return this.name; 132 | } 133 | 134 | public static void main(String[] args) { 135 | Person teacher = new Person("Michael"); 136 | System.out.println(teacher.name()); 137 | } 138 | 139 | } 140 | ``` 141 | 142 | Take note of this like - `Person teacher = new Person("Michael");`. Here, we invoked the constructor to create a new instance. The constructor has the same name, `Person`, as the class. 143 | 144 | What else is new here? The `name()` method is not `static` (so it needs to be instantiated to use) and the appearance of `this`... 145 | 146 | ### Methods (instance vs static) 147 | 148 | - Static methods: Part of the class. Do not have to be instantiated to use. 149 | - Instance methods: Must be instantiated to use. 150 | 151 | ### `this` 152 | 153 | In Java `this` is always refers back to the current instance. 154 | -------------------------------------------------------------------------------- /code/Calc.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mjhea0/java-for-javascript/a7fb525a439e9b3594a80459d58ef4847704f7b3/code/Calc.class -------------------------------------------------------------------------------- /code/Calc.java: -------------------------------------------------------------------------------- 1 | public class Calc { 2 | 3 | public int num1; 4 | public int num2; 5 | 6 | public Calc(int num1, int num2) { 7 | this.num1 = num1; 8 | this.num2 = num2; 9 | } 10 | 11 | public int add() { 12 | return this.num1 + this.num2; 13 | } 14 | public int subtract() { 15 | return this.num1 - this.num2; 16 | } 17 | public int multiply() { 18 | return this.num1 * this.num2; 19 | } 20 | public int divide() { 21 | return this.num1 / this.num2; 22 | } 23 | 24 | public static void main(String[] args) { 25 | Calc testCalc = new Calc(2, 2); 26 | System.out.println(testCalc.add()); 27 | System.out.println(testCalc.subtract()); 28 | System.out.println(testCalc.multiply()); 29 | System.out.println(testCalc.divide()); 30 | } 31 | 32 | }; 33 | -------------------------------------------------------------------------------- /code/Hello.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mjhea0/java-for-javascript/a7fb525a439e9b3594a80459d58ef4847704f7b3/code/Hello.class -------------------------------------------------------------------------------- /code/Hello.java: -------------------------------------------------------------------------------- 1 | public class Hello { 2 | public static void main(String[] args) { 3 | System.out.println("Hello, " + args[1] + "!"); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /code/Ops.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mjhea0/java-for-javascript/a7fb525a439e9b3594a80459d58ef4847704f7b3/code/Ops.class -------------------------------------------------------------------------------- /code/Ops.java: -------------------------------------------------------------------------------- 1 | public class Ops { 2 | public static int sum(int x, int y) { 3 | return x + y; 4 | } 5 | public static int multiply(int num1, int num2, int num3) { 6 | return num1 * num2 * num3; 7 | } 8 | public static int power(int x) { 9 | int y = 2; 10 | for (int i=0; i < y; i++) { 11 | x = x * x; 12 | } 13 | return x; 14 | } 15 | public static void main(String[] args) { 16 | System.out.println(sum(1, 2)); 17 | System.out.println(multiply(2, 2, 2)); 18 | System.out.println(power(2)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /code/Person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mjhea0/java-for-javascript/a7fb525a439e9b3594a80459d58ef4847704f7b3/code/Person.class -------------------------------------------------------------------------------- /code/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | 3 | public String name; 4 | public int age; 5 | 6 | public Person(String name, int age) { 7 | this.name = name; 8 | this.age = age; 9 | } 10 | 11 | public String name() { 12 | return this.name; 13 | } 14 | 15 | public int age() { 16 | return this.age; 17 | } 18 | 19 | public static void main(String[] args) { 20 | Person teacher = new Person("Michael", 28); 21 | System.out.println(teacher.name()); 22 | int results = teacher.age(); 23 | System.out.println(results); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /code/Rect.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mjhea0/java-for-javascript/a7fb525a439e9b3594a80459d58ef4847704f7b3/code/Rect.class -------------------------------------------------------------------------------- /code/Rect.java: -------------------------------------------------------------------------------- 1 | public class Rect { 2 | 3 | public int width; 4 | public int length; 5 | 6 | public Rect(int width, int length) { 7 | this.width = width; 8 | this.length = length; 9 | } 10 | 11 | public int area() { 12 | return this.width * this.length; 13 | } 14 | 15 | public static void main(String[] args) { 16 | Rect myReact = new Rect(100, 100); 17 | System.out.println(myReact.area()); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /code/Sum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mjhea0/java-for-javascript/a7fb525a439e9b3594a80459d58ef4847704f7b3/code/Sum.class -------------------------------------------------------------------------------- /code/Sum.java: -------------------------------------------------------------------------------- 1 | public class Sum { 2 | public static void main(String[] args) { 3 | int num = 2; 4 | System.out.println(num); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /code/calc.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 1. add() 4 | 1. subtract() 5 | 1. multiply() 6 | 1. divide() 7 | 8 | */ 9 | 10 | var Calculator = function(num1, num2) { 11 | this.num1 = num1; 12 | this.num2 = num2; 13 | }; 14 | 15 | Calculator.prototype.add = function () { 16 | return this.num1 + this.num2; 17 | }; 18 | 19 | Calculator.prototype.subtract = function () { 20 | return this.num1 - this.num2; 21 | }; 22 | 23 | Calculator.prototype.multiply = function () { 24 | return this.num1 * this.num2; 25 | }; 26 | 27 | Calculator.prototype.divide = function () { 28 | return this.num1 / this.num2; 29 | }; 30 | 31 | var testCalc = new Calculator(3, 2); 32 | console.log(testCalc.add()); 33 | console.log(testCalc.subtract()); 34 | console.log(testCalc.multiply()); 35 | console.log(testCalc.divide()); 36 | -------------------------------------------------------------------------------- /code/hello.js: -------------------------------------------------------------------------------- 1 | console.log('Hello, JavaScript'); 2 | -------------------------------------------------------------------------------- /hello.js: -------------------------------------------------------------------------------- 1 | console.log('Hello, World!'); 2 | --------------------------------------------------------------------------------