![Learning Java Programming: Understanding Variables and Data Types](https://1.bp.blogspot.com/-Wh3-vfndnRg/WOHC2viBv7I/AAAAAAAAEhE/pEdfryyz6x472o8b2uysQxG-xcVsbTdtwCPcB/s1600/variabel-dan-tipe-data.jpg)
A variable is a place to store temporary values.
That is all.
Serious?
Yeah seriously, if you want a longer one ...
... let's see the meaning from Wikipedia:
Variable: (Lat) 1. changeable, not fixed; 2. declaration of something that has a variety of values 3. different in programming languages also called symbols that represent certain values, variables known in sub-programs are called local variables. while what is generally known in one program is called a global variable.
Well, do you understand?
If not, understand enough: Variables as a place to store temporary values.
Then, what is the data type?
Data type is a type of data stored in a variable.
Types of Data Types
The following are various types of data in Java:
char
: Character data type, exampleZ
int
: numbers or integers, for example29
float
: decimal number, for example2.1
double
: decimal number too, but more capacity, for example2.1
String
: a collection of characters that make up text, for exampleHello World!
boolean
: data types that are only valuabletrue
andfalse
Making Variables
The thing you need to know in making variables in java is how to write them.
Format it like this:
<tipe data> namaVariabel;
Example :
Create an empty variable of type integer:
int namaVariabel;
Creates an integer type variable and is immediately filled with a value:
int namaVariabel = 19;
Create a set of variables whose data types are the same:
int a, b, c;
Then, where was it written?
- Inside functions
main()
: variables written in other functionsmain()
and functions are called local variables. - In the class: This variable is called a class or global variable.
Confused…?
Let's Try Exercises by Creating a DataDiri Program
Create a new program called DataDiri. Please add a class (java class) just package (package)
pertemuan1
.![Creating Java Classes in Netbeans](https://1.bp.blogspot.com/-auMt7ptDILY/VhUXJ3fj_FI/AAAAAAAAB5c/phVlb95qgig/s1600/Membuat%2Bjava%2Bclass%2Bdi%2Bnetbeans.png)
In the window that appears, fill in the name of the class with DataDiri . After that, please follow the following code.
![DataDiri Program](https://2.bp.blogspot.com/-8e30RYXIhlY/VhnvGo1bPYI/AAAAAAAAB-Q/A63y3RrrzKQ/s1600/Program%2BDataDiri%2B-%2BVariabel%2Bdan%2Btipe%2Bdata%2Bjava.png)
After that, try running by pressing the [Shift]+ button [F6]. Analyze and understand the intent of the codes above.
![Self data program with Java](https://2.bp.blogspot.com/-bL-KEN2HXCI/VhnzLOzXvvI/AAAAAAAAB-c/6sUMVdFsPSM/s1600/Program%2BDataDiri%2Bpetani%2Bkode%2Bdengan%2Bjava.png)
The output seems wrong.
How old is the address, why is that?
That is because we call the variable
alamat
at the age line .
So what will appear is the contents of the variable
alamat
, not the contents of the variable usia
.
Repair the code to be like this:
![Improvement of the Self Data Program](https://1.bp.blogspot.com/-JgBuIhQp5qk/Vhn0m_dG0wI/AAAAAAAAB-o/LmtZaZPGUMs/s1600/Perbaikan%2Bprogram%2BDataDiri%2B-%2Bpetani%2Bkode%2Bdan%2Bjava.png)
And, ... please try running the program again.
![DataDiri Program Output Results](https://2.bp.blogspot.com/-Mlq6BX9Kn1k/Vhn2H52rl4I/AAAAAAAAB-0/QCD-sZtuwx0/s1600/hasil%2Boutput%2BProgram%2BDataDiri%2Bpetani%2Bkode%2Bdengan%2Bjava.png)
Yep! the result is correct.
Variable Writing Rules
Apparently not allowed to create variables.
There are rules that must be followed, including:
- The variable name may not use the keywords of the Java (reserved word) such as
if
,for
,switch
, etc. - Variable names may use letters, numbers (0-9), underscore , and dollar symbols ($), but the use of underscores and symbols is better avoided.
- The variable name must start with a lowercase letter, because Java uses the CamelCase style .
- If the variable name is more than 1 syllable, then the second word is written by starting with a capital letter and so on, for example
namaVariabel
.
2nd Exercise: Broad Circle Program
Let us establish understanding by making a Broad Circularprogram .
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 to program, we should first understand the algorithm and 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:
![Flow Chart Area of Circles](https://4.bp.blogspot.com/-topvJ0egkaI/VhaAEc1i0rI/AAAAAAAAB7c/TLjkOJ1o7jI/s1600/Flow%2BChart%2Bluas%2Blingkaran%2B-%2Bjava.png)
Well, it's clear right?
Now we are just coding .
![Java Code Area of Circles](https://3.bp.blogspot.com/-GKiBCcH0k7Q/VhZ-UqlS0DI/AAAAAAAAB7Q/SJ8Cu-lksvo/s1600/Kode%2Bjava%2Bluas%2Blingkaran.png)
Please execute and refine the results.
Data Type Conversion
Conversion means changing to another type.
Why do we need to convert data types?
To answer this, I want to show the following illustration:
Water that is in liquid form cannot be stored in cardboard. Therefore, water must be converted into a solid form (ice) so that it can be stored in cardboard.
Likewise with variables.
Data string types can not be stored in variables with integertypes .
How to Convert Data Types
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;
Apparently, there are many ways.
Let's Try in the Program ...
Make a new class (java class) in the meeting package2 .
Give a name: Extensive Triangle .
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);
}
}
Variables of
luas
type Double data , meaning the value that can be stored is Double .
While the variables
alas
and tinggi
types are Integer .
In order for the results of integer data operations to be stored in a variable of type double, it needs to be converted.
If executed, it will produce:
![The output of the program area is triangle with java](https://4.bp.blogspot.com/-D-ewjhAcySs/ViDl9iOzQiI/AAAAAAAACAU/_y-3m7vEULw/s1600/hasil%2Boutput%2Bprogram%2Bluas%2Bsegitiga%2Bdengan%2Bjava%2B-%2Blab%2Bjava%2Bpetani%2Bkode.png)
0 Komentar untuk "Learning Java Programming: Variables and Data Types"
Silahkan berkomentar sesuai artikel