DEV Community

nadirbasalamah
nadirbasalamah

Posted on

Java Tutorial - 1 Introduction

Java is one of the most popular programming language right now. Java has been used in many application development especially backend development.

Setup

This is the guide that can be used to install the java SDK to run the java code.

To check the java installation run this command:

java -version
Enter fullscreen mode Exit fullscreen mode

Writing the First Code

To write the java code, any IDE or text editor can be used. The IDE that commonly used are NetBeans and IntelliJ IDEA. Here it is the first code.

public class MyApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
Enter fullscreen mode Exit fullscreen mode

This is the output after the code is executed:

Hello World!

Enter fullscreen mode Exit fullscreen mode

Based on that code, the System.out.println() syntax is executed inside main method that located inside MyApp class. the println() function is used to create an output with additional white space.

Another way to create an output is using printf() function. This function can be used to create an output with specific format. In this example, the number and the text or a String is printed using printf() function.

public class MyApp {
    public static void main(String[] args){
        // create a variable called text and number
        String text = "this is a text";
        int number = 99;

        //using %s syntax to print out the String data
        // \n syntax is used to add additional newline
        System.out.printf("The text data: %s\n",text);
        // using %d syntax to print out the int data
        System.out.printf("The number data: %d\n",number);
    }
}
Enter fullscreen mode Exit fullscreen mode

This is the output:

The text data: this is a text
The number data: 99

Enter fullscreen mode Exit fullscreen mode

To learn more about printf(), visit this page.

Adding Inputs

There are many ways to create an input in Java, one of those are using Scanner class that can be imported in java.util.Scanner package.

import java.util.Scanner;

public class MyApp {
    public static void main(String[] args){
        // create a scanner
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        // receive the input using nextInt() for int data
        int data = scanner.nextInt();
        // skip to the next input
        scanner.nextLine();

        System.out.print("Enter a text: ");
        // receive the input using nextLine() for String data
        // nextLine() is used to allow String input with spaces (e.g. "Hello World!")
        String textData = scanner.nextLine();

        // print out the inputted data
        System.out.println("Your entered number: " + data);
        System.out.println("Your entered text: " + textData);

    }
}

Enter fullscreen mode Exit fullscreen mode

This is the input sample with the output:

Enter a number: 12
Enter a text: Sample Text
Your entered number: 12
Your entered text: Sample Text

Enter fullscreen mode Exit fullscreen mode

Variables

To create a variable in Java, use this syntax.

<modifier> <data_type> <variable_name>;
Enter fullscreen mode Exit fullscreen mode

In this example, the variable called number is declared with the int type.

// if the value isn't assigned in a variable,
// the default value is assigned
// (in this case the default value is 0)
int number;
Enter fullscreen mode Exit fullscreen mode

This is another example of creating variable with String data type.

// assign the value with '=' operator
String name = "firstname lastname";
// change the value
name = "another name";
Enter fullscreen mode Exit fullscreen mode

There are another special variable called final and static variable.
The final variable is a variable that the value cannot be changed or known as constant. To create a constant in Java can be done by adding final keyword before variable declaration.

// to declare a constant, use uppercase letters.
final double PI = 3.14;
Enter fullscreen mode Exit fullscreen mode

The static variable is a variable that created only once in the beginning phase of program execution. When the static variable is defined inside a class, the variable can be accessed without creating the instance or an object of the class.

This is the example of declaring a static variable:

static int number = 12;
//the static and final keyword can be used together
static final float PI = 3.14;
Enter fullscreen mode Exit fullscreen mode

Data Types

There are two main types of data type in Java. There are primitive data type and reference data type.
This is the list of primitive data type in Java.

Data Type Value Minimum Value Maximum Value
byte non-decimal number -128 127
short non-decimal number -32768 32767
int non-decimal number -2147483648 2147483647
long non-decimal number -9223372036854775808 9223372036854775807
float decimal or floating number 1.4E-45 3.4028235E38
double decimal or floating number 4.9E-324 1.7976931348623157E308
boolean true or false - -
char single character (alphabetic) - -

The primitive data type that commonly used are int, float char, and boolean.

Use the data type based on the value that will be assigned and consider the minimum value or maximum value that can be stored.

There are many examples of reference data type. One of those are String that commonly used to store alphabetical characters like username. The other example of reference data type are array, list, and a class that defined in Java.

The main difference between primitive and reference data type is the primitive data type store the real value inside the variable. Otherwise, the reference data type store the reference to the exact or real value inside the variable.

This is the illustration of primitive data type and reference data type.
Data Type Illustrations

Operator and Modifier

In Java, there are many operators that available for number operation. The operation must be done with the same data type.

Operator Description
+ add operation
- substract operation
* multiply operation
/ division operation
% modulo operation (get the remainder from division operation)

This is the example of using operator in Java.

public class MyApp {
    public static void main(String[] args){
        int a = 6;
        int b = 2;

        int sumResult = a + b;
        int subsResult = a - b;
        int mulResult = a * b;
        int divResult = a / b;
        int modResult = a % b;

        System.out.println("a + b = " + sumResult);
        System.out.println("a - b = " + subsResult);
        System.out.println("a * b = " + mulResult);
        System.out.println("a / b = " + divResult);
        System.out.println("a % b = " + modResult);

    }
}
Enter fullscreen mode Exit fullscreen mode

The output:

a + b = 8
a - b = 4
a * b = 12
a / b = 3
a % b = 0

Enter fullscreen mode Exit fullscreen mode

Modifier can be used to specify the scope of variable or a function inside the code. The modifier can be used before the declaration of variable or a function.

// for variable, this variable can be accessed from any class
public int number = 12;

// for function, this function can be accessed only inside its own class
// function will be covered later
private int getSum(int a, int b) {}
Enter fullscreen mode Exit fullscreen mode

The available modifiers in Java can be checked here.

Modifier Class Sub Class Package Outside Package
public
protected
private
default

The check mark (✔) represents the visibility.

Sources

  • Useful resource for learning the basics of Java.
  • Learn more about static keyword here.

I hope this article is helpful for learning the Java programming language. If you have any thoughts or comments you can write in the discussion section below.

Top comments (0)