Variabel dan Tipe Data java
88 / 100

Recognizing Java Variables and Data Types

Various Java Data Types

Recognizing Java Variables and Data Types, in java The following are the various types of data in: Java:

  • char: Character data type, example Z
  • int: numbers or whole numbers, example 29
  • float: decimal number, example 2.1
  • double: decimal numbers as well, but bigger capacity, example 2.1
  • String: a collection of characters that make up text, example Hello World!
  • boolean: value-only data type true and false

Creating Variables

The thing to know in creating variables in Java is how to write them.

The format is like this:

<tipe data> namaVariabel;


Create an empty variable of type integer:

int namaVariabel;

Create a variable of type integer and directly fill in the value:

int namaVariabel = 19;

Create a set of variables with the same data type:

int a, b, c;

Then, where is it written?

  1. Inside the function main(): variables written inside the function main() and the other functions are called local variables.
  2. In the class: These variables are called class or global variables.


Let's Try Exercise by Creating a Self-Data Program

Create a new program called DataDiri. Please add class (java class) new in package (package) pertemuan1.

Membuat Class Java di Netbeans

In the window that appears, fill in the class name with DataDiri. After that, please follow the following code.

Program DataDiri

After that, try running it by pressing the button [Shift]+[F6]. Analyze and understand the meaning of the codes above.

Program data diri dengan Java

The output seems to be wrong.

How old is the address?, why so?

It's because we call the variable alamat on the line age.

So what will appear is the contents of the variable alamat, not the contents of the variable usia.

Fix the code to be like this:

Perbaikan Program Data Diri

And, … please try to run the program again.

Hasil Output Progam DataDiri

Do! the result is correct.

Java Variable Writing Rules

It turns out that it can't be arbitrary in creating variables.

There are rules to follow, among them:

  • Variable names cannot use keywords from Java (reserved word) as if, for, switch, dll.
  • Variable names can use letters, number (0-9), underline (underscore), and the dollar symbol ($), but the use of underscores and symbols is better avoided.
  • Variable names must start with a lowercase letter, because Java uses gaya CamelCase.
  • If the variable name is greater than 1 syllables, then the 2nd word is written by beginning with a capital letter and so on, example namaVariabel.

2nd Exercise: Circle Area Program

Let's strengthen the understanding by creating a program Circle Area.

This program functions to calculate the area of ​​a circle.

We can calculate the area of ​​a circle using the formula PI x r2.

Before starting programming, we should first understand the algorithm and the flowchart:


    Double luas, PI
    int r
    - Input
        PI = 3.14
        r = 18
    - Proses
        luas = PI * r * r
    - Output
        cetak luas

Flow Chart:

Flow Chart Luas Lingkaran

Nah, it's clear right??

Now we live coding just.

Kode Java Luas Lingkaran

Please execute and keep the results.

Data Type Conversion

Conversion means changing to another type.

Why do we need data type conversion?

To answer it, I want to show you the following illustration: = 13&wp=508&

Liquid water cannot be stored in a cardboard box. Because of that, water must first be converted into solid form (is) so that it can be stored in a cardboard.

Likewise with variables.

data type string will not be able to be stored in a variable with type integer.

How to Convert Data Type

Example: Convert to integer type

Method 1:


Method 2:


Method 3:


Method 4:

(int) variabel;
(int) 2.1;

Evidently, there are so many ways.

Let's Try in the Program…

Make a class (java class) new in package meeting2.

Give it a name: With LuasSegit.

Then follow the following program code:

package pertemuan2;

import java.util.Scanner;

public class LuasSegitia {

    public static void main(String[] args) {
        // deklarasi
        Double luas;
        int alas, tinggi;

        // mebuat scanner baru
        Scanner baca = new Scanner(;

        // Input
        System.out.println("== Program hitung luas Segitiga ==");
        System.out.print("Input alas: ");
        alas = baca.nextInt();
        System.out.print("Input tinggi: ");
        tinggi = baca.nextInt();

        // proses
        luas = Double.valueOf((alas * tinggi) / 2);

        // output
        System.out.println("Luas = " + luas);


Variable luas data type Double, means the value that can be stored is Double.

While the variable alas and tinggi type Integer.

So that the results of data operations integer can be stored in a variable of type double, then it needs to be converted.

source :

 1,370 total views,  4 views today

By admin


Leave a Reply

Your email address will not be published.

Web Hosting Murah Yang Terbaik Di Indonesia