Java - tutorial - 01/13 - basics

revision:


what is Java?

Java is a popular programming language, created in 1995. It is owned by Oracle, and more than 3 billion devices run Java.

It is used for:

mobile applications (specially Android apps)
desktop applications
web applications
web servers and application servers
games
database connection
and much, much more!

Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.). It is open-source and free. It is secure, fast and powerful. It has a huge community support (tens of millions of developers).

Java is an object oriented language, which gives a clear structure to programs and allows code to be reused, lowering development costs. As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa.


get started

To check if you have Java installed on a Windows PC, search in the start bar for Java or type the following in Command Prompt (cmd.exe): C:\Users\Your Name>java -version. If Java is installed, you will see something like this (depending on version):

        C:\Users\0>java -version
        java version "1.8.0_301"
        Java(TM) SE Runtime Environment (build 1.8.0_301-b09)
        Java HotSpot(TM) Client VM (build 25.301-b09, mixed mode, sharing)
    

In Java, every application begins with a class name, and that class must match the filename.

example

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

How to run the code above?

Save the code in a text editor as "Main.java". Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac Main.java": C:\Users\Your Name>javac Main.java. This will compile your code. If there are no errors in the code, the command prompt will take you to the next line. Now, type "java Main" to run the file: C:\Users\Your Name>java Main. The output should read: Hello World


Java syntax

Every line of code that runs in Java must be inside a class. A class should always start with an uppercase first letter.

Java is case-sensitive: "MyClass" and "myclass" has different meaning.

The name of the java file must match the class name. When saving the file, save it using the class name and add ".java" to the end of the filename.

In the example above, we named the class Main.

The main() method is required and you will see it in every Java program: public static void main(String[] args). Any code inside the main() method will be executed.

Inside the main() method, we can use the println() method to print a line of text to the screen.

!!! The curly braces {} mark the beginning and the end of a block of code. Each code statement must end with a semicolon.


comments

Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code.

single-line Comments start with two forward slashes (//). Any text between // and the end of the line is ignored by Java (will not be executed).

example:

        // This is a comment
        System.out.println("Hello World");
        System.out.println("Hello World"); // This is a comment

Multi-line comments start with /* and ends with */. Any text between /* and */ will be ignored by Java.

example:

        /* The code below will print the words Hello World
        to the screen, and it is amazing */
        System.out.println("Hello World");

Don't forget

Java counts positions from zero. 0 is the first position in a string, 1 is the second, 2 is the third ...