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, exampleZ
int
: numbers or whole numbers, example29
float
: decimal number, example2.1
double
: decimal numbers as well, but bigger capacity, example2.1
String
: a collection of characters that make up text, exampleHello World!
boolean
: value-only data typetrue
andfalse
Creating Variables
The thing to know in creating variables in Java is how to write them.
The format is like this:
<tipe data> namaVariabel;
Example:
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?
- Inside the function
main()
: variables written inside the functionmain()
and the other functions are called local variables. - In the class: These variables are called class or global variables.
Confused…?
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
.

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

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

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:

And, … please try to run the program again.

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:
Algorithm
Deklarasi:
Double luas, PI
int r
Deskripsi:
- Input
PI = 3.14
r = 18
- Proses
luas = PI * r * r
- Output
cetak luas
Flow Chart:

Nah, it's clear right??
Now we live coding just.

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: https://www.instagram.com/p/BJehYJRj8x-/embed/captioned/?cr=1&v = 13&wp=508&rd=https%3A%2F%2Fwww.petanikode.com&rp=%2Fjava-variabel-dan-tipe-data%2F#%7B%22ci%22%3A0%2C%22os%22%3A1257446%2C%22ls%22%3A1254857%2C%22le%22%3A1254857%7D
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:
Integer.perseInt(variabel);
Integer.perseInt(1.2);
Method 2:
Integer.valueOf(2.1);
Method 3:
objek.toInt();
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(System.in);
// 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 : https://www.petanikode.com/java-variabel-dan-tipe-data/
1,370 total views, 4 views today