Basics of Java

This is a really time-consuming language to learn plus it's not easy. I recently started to self-learn Java and thought why not post a blog about it?

This is a very useful language. Java is used for,

  • Mobile Applications

  • Desktop Applications

  • Web Applications

  • Games

  • Database Connections

  • And a ton more..!

Java is a bit close to C/C++. To set up your PC to run Java you need to download and install Java Development Kit(JDK). Click here to download the Java JDK. After installation, you can run the java --version command on your console to verify the installation.

Then you need a code editor or an IDE to write Java Code. I am using Intellij idea. Installing it is really simple, just download and install it on your PC.

Okay now in Java, every application begins with a class name, and the class name must match the filename. Let's assume that we have a file called main.java, Then the class name should be main. Given below is a simple "Hello World" script to showcase that.

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

The main() method is required and you will see it in every Java program. There is also a print() method, which is similar to println(). The print method does not have a line break after the end that is the difference.

Java Comments

We can write comments in Java starting with two forward slashes "//". Multi-line comments start with /* and end with */. We can place comments anywhere in our code and the JDK will not compile them.

Variables

Variables are used to store data in a program temporarily. There are five main variable types in Java. They are,

String a = "Dineth";
//stores text. surrounded by double quotes

char b = "H";
//stores single characters, surrounded by double quotes

int c = 20;
//stores integers 

float d = 24.5;
//stores decimal numbers

boolean e = false;
//stores values with two states: true or false