|
| 1 | +# Getting Started With Java |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +In the [previous article](/03_java/01_installation_and_quick_start.md), you saw how you can set your development environment for Java. The quickstart guide highlighted a simple Java program that prints "Hello, world!" in the terminal: |
| 6 | + |
| 7 | +```java |
| 8 | +public class App { |
| 9 | + public static void main(String[] args) { |
| 10 | + System.out.println("Hello, world!"); |
| 11 | + } |
| 12 | +} |
| 13 | +``` |
| 14 | + |
| 15 | + |
| 16 | +In this article, we are going to break down the parts of such a program in an effort to know what each name does. We can refer to this as the Java syntax. More specifically, we are going to look at [public](#public), [static](#static), [void](#void), [String[ ] args](#string--args), and [main](#main). |
| 17 | + |
| 18 | + |
| 19 | +### Table of Contents |
| 20 | + |
| 21 | +- [Create A Java Project](#create-a-java-project) |
| 22 | +- [Understanding Java Main Method](#understanding-java-main-method) |
| 23 | + |
| 24 | + |
| 25 | +## Create A Java Project |
| 26 | + |
| 27 | +Now, to create a Java program, we need to do the following: |
| 28 | + |
| 29 | + |
| 30 | +### Add A Project Folder |
| 31 | + |
| 32 | +This folder can fold multiple Java programs. Below, I will show you how you can add one program to an empty folder. On your terminal, run: |
| 33 | + |
| 34 | +```python |
| 35 | +$ mkdir getting_started && code getting_started |
| 36 | +``` |
| 37 | + |
| 38 | +This creates a folder called `getting_started` and opens it using VS Code. Choose a location where you want to create the folder, your Desktop, Documents etc. |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +### Start A Java Project On VS Code |
| 44 | + |
| 45 | +Click on View > Command Pallet as shown in the image below. Alternatively, press Ctrl + Shift + P. |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +In the interactive popup, type "create java project" and then click on the dropdown command seen: |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +Select "No build tools" as seen below: |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +Select your project location by clicking on the green button. Notice that our directory created earlier has been selected by default. |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +You will see another interactive popup. In it, type in a name of your choice. In my case, I have chosen "App" Then, press **Enter** on your keyboard. |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +As soon as I have pressed **Enter** on my keyboard, a new Java project called `App` has been created on my `getting_started` directory. |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +You can choose to continue with the popped up VS Code window or work with your project directory directly. Below, I will close the popup window and continue with my main project directory. Clicking on the `App` project, I am able to access my program file called `App.java`. |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +To run it, I can either click the **Play** icon at the top-right of the window or right-click inside the `App.java` file. |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +Either way, you should see the text "Hello, world!" displayed on your terminal. More from VS Code's [Java Tutorial](https://code.visualstudio.com/docs/java/java-tutorial). |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +## Understanding Java Main Method |
| 84 | + |
| 85 | +The Java [`main`](#main) method is the entry point for executing a Java program. It can contain code to execute or call other methods. Additionally, the `main` method can be used in another part of a program. This method is found in a class, `App` for example, as seen above. The class name does not really matter (you can call it anything), but it should contain the method `main` to become an entry point to the program. |
| 86 | + |
| 87 | +### `public` |
| 88 | + |
| 89 | +This is an access modifier. It gives intructions on the type of access available in the `main` method. `public` allows the JRE (Java Runtime Environment) access to execute the `main` method. To see it in action, let us try to remove it from the `main` method: |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +Notice VS Code throws the error `Error: Main method not found. Please define the main me...` |
| 94 | + |
| 95 | + |
| 96 | +### `static` |
| 97 | + |
| 98 | +When we run a Java program for the first time, there is no object of the class that is present. We, therefore, need to use `static` to instruct JVM to lod the class into memory and then call `main` without creating an instance of that class. |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +### `void` |
| 103 | + |
| 104 | +It is intended that every Java method should have a return type, and return something. However, the `main` method doesn't return anything. As a result, we use `void` to handle this. |
| 105 | + |
| 106 | +```java |
| 107 | +// App.java |
| 108 | + |
| 109 | +public class App { |
| 110 | + public static void main(String[] args) { |
| 111 | + System.out.println("Hello, World!"); |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +This method has no return type, hence the use of `void`. However, if we try to return something, then an error is raised. |
| 117 | + |
| 118 | +```java |
| 119 | +public class App { |
| 120 | + public static void main(String[] args) { |
| 121 | + return ("Hello, World!"); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// Error: |
| 126 | + |
| 127 | +Void methods cannot return a value |
| 128 | +``` |
| 129 | + |
| 130 | +To return something, we need to exclude `void` from the `main` method. |
| 131 | + |
| 132 | +```java |
| 133 | +// App.java |
| 134 | + |
| 135 | +public class App { |
| 136 | + public static String main(String[] args) { |
| 137 | + return "Hello, World!"; |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +Notice that we have to specify the return type as `String` since we are returning the text "Hello, World!" |
| 143 | + |
| 144 | + |
| 145 | +### `main` |
| 146 | + |
| 147 | +This method has to be named as "main". When a Java program starts, it will ALWAYS look for the `main` method. If this method does not exist, then we get an error. |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | +### `String [] args` |
| 153 | + |
| 154 | +In the terminal, you can run a Java file by using the command `java` followed by the filename, which becomes the second argument: |
| 155 | + |
| 156 | +```java |
| 157 | +$ java App.java |
| 158 | +``` |
| 159 | + |
| 160 | +The `main` method accepts a single argument of type `String` array. Each string in the array is a commandline argument that we can use to either affect a program or pass in data at runtime. |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +Notice that in the terminal, I have passed in additional arguments. These arguments are space-separated, and get printed. |
0 commit comments