Some very basic Java tips I learnt from GitHub’s Learn Lab Java Course. And Hello World!

Intro to Java

I still have little knowledge on Java, but I’m willing to go further!


Table of Contents


Here I’m using vscode with the following extensions - maybe they may be automatically installed when creating a java file to work on, so don’t worry! Use this only to consult with something is going wrong:

  • Debugger for Java
  • Extension Pack for Java
  • Language Support for Java(TM) by Red Hat
  • Maven for Java
  • Project Manager for Java
  • Test Runner for Java

Verifying installation

To properly run, Java must be installed. Verify on the terminal with:

java -version
javac -version

Both must return the installed version. If not, check the installation on Java website.


Java class

public class hello_world is an example of class that you can create first in the blank java project.

    public class hello_world {

    }

And then we can create the first method as follow.


Java main method

public static void main(String[] args) is the main Java method.

  • public: Is the access modifier of the main method
  • static: In order to JVM load the class into the memory and call the main method, it has to be static
  • void: We don’t want to return nothing on this main method, so the return type is void
  • main: Is the name of java main method, and where a java program will look when executed
  • String[] args: Is the called ‘java command line arguments’, meaning that the main method accepts a single argument of type String array.

Now we have the following

    public class hello_world {
        public static void main(String[] args) {

        }
    }

Hello World

Now that we have some Java basic structure, let’s add a print function with System.out.println

    public class hello_world {
        public static void main(String[] args) {
            System.out.println("Hello, World!");
        }
    }

Compiling

To run a java program we firt need to compile with the command javac, that will create the executable java program.

So just run first in the file.java, and then run the compiled file that will be only file with java:

javac file.java
java file

Tips

Comparing strings

Comparison elements like == and != work on the indentity of objects. Having two different strings actually they will be different objects, even if are two equal words.

To do so, use

  • "string".equals(variable) (same of variable == "string" logic) or
  • !"string".equals(variable) (same of variable != "string" logic).