Difference between revisions of "JAVA: Operator Aritmatik dengan Contoh"
Onnowpurbo (talk | contribs) (Created page with "Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various...") |
Onnowpurbo (talk | contribs) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types: | Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types: | ||
− | + | Operator merupakan blok bangunan dasar untuk bahasa pemrograman apa pun. Java juga menyediakan banyak jenis operator yang dapat digunakan sesuai dengan kebutuhan untuk melakukan berbagai perhitungan dan fungsi, baik itu logika, aritmatika, relasional, dll. Diklasifikasikan berdasarkan fungsionalitas yang disediakan. Berikut beberapa jenisnya: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | Arithmetic | + | * Operator Arithmetic |
+ | * Operator Unary | ||
+ | * Operator Assignment | ||
+ | * Operator Relational | ||
+ | * Operator Logical | ||
+ | * Operator Ternary | ||
+ | * Operator Bitwise | ||
+ | * Operator Shift | ||
− | + | Artikel ini menjelaskan semua yang perlu diketahui tentang Operator Aritmatika. | |
− | |||
− | + | ==Operator Arithmetic== | |
− | + | Operator ini melibatkan operator matematika yang dapat digunakan untuk melakukan berbagai operasi aritmatika sederhana atau lanjutan pada tipe data primitif yang disebut sebagai operan. Operator ini terdiri dari berbagai operator unary dan biner yang dapat diterapkan pada satu atau dua operan. Mari kita lihat berbagai operator yang disediakan Java di bawah operator aritmatika. | |
− | |||
− | num1 = 10, num2 = 20 | + | [[File:Arithmeticoperators.png|center|300px|thumb]] |
− | sum = num1 + num2 = 30 | + | |
+ | Sekarang mari kita lihat masing-masing operator aritmatika di Java: | ||
+ | |||
+ | |||
+ | ==1. Addition(+):== | ||
+ | |||
+ | Operator ini adalah operator biner dan digunakan untuk menambahkan dua operan. | ||
+ | |||
+ | |||
+ | Sintaks: | ||
+ | |||
+ | num1 + num2 | ||
+ | |||
+ | Contoh: | ||
+ | |||
+ | num1 = 10, num2 = 20 | ||
+ | sum = num1 + num2 = 30 | ||
+ | |||
+ | // Java code to illustrate Addition operator | ||
+ | |||
+ | import java.io.*; | ||
+ | |||
+ | class Addition { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | // initializing variables | ||
+ | int num1 = 10, num2 = 20, sum = 0; | ||
+ | |||
+ | // Displaying num1 and num2 | ||
+ | System.out.println("num1 = " + num1); | ||
+ | System.out.println("num2 = " + num2); | ||
+ | |||
+ | // adding num1 and num2 | ||
+ | sum = num1 + num2; | ||
+ | System.out.println("The sum = " + sum); | ||
+ | } | ||
+ | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Output | Output | ||
− | num1 = 10 | + | num1 = 10 |
− | num2 = 20 | + | num2 = 20 |
− | The sum = 30 | + | The sum = 30 |
− | 2. Subtraction(-): | + | |
+ | ==2. Subtraction(-):== | ||
+ | |||
+ | Operator ini adalah operator biner dan digunakan untuk mengurangi dua operan. | ||
+ | |||
+ | |||
+ | Sintaks: | ||
+ | |||
+ | num1 - num2 | ||
− | + | Contoh: | |
− | num1 - num2 | + | num1 = 20, num2 = 10 |
− | + | sub = num1 - num2 = 10 | |
− | num1 = 20, num2 = 10 | + | // Java code to illustrate Subtraction operator |
− | sub = num1 - num2 = | + | |
+ | import java.io.*; | ||
+ | |||
+ | class Subtraction { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | // initializing variables | ||
+ | int num1 = 20, num2 = 10, sub = 0; | ||
+ | |||
+ | // Displaying num1 and num2 | ||
+ | System.out.println("num1 = " + num1); | ||
+ | System.out.println("num2 = " + num2); | ||
+ | |||
+ | // subtracting num1 and num2 | ||
+ | sub = num1 - num2; | ||
+ | System.out.println("Subtraction = " + sub); | ||
+ | } | ||
+ | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Output | Output | ||
− | num1 = 20 | + | num1 = 20 |
− | num2 = 10 | + | num2 = 10 |
− | Subtraction = 10 | + | Subtraction = 10 |
− | 3. Multiplication(*): | + | |
+ | ==3. Multiplication(*):== | ||
+ | |||
+ | Operator ini adalah operator biner dan digunakan untuk mengalikan dua operan. | ||
− | + | Sintaks: | |
− | num1 * num2 | + | num1 * num2 |
− | |||
− | num1 = 20, num2 = 10 | + | Contoh: |
− | mult = num1 * num2 = 200 | + | |
+ | num1 = 20, num2 = 10 | ||
+ | mult = num1 * num2 = 200 | ||
+ | |||
+ | // Java code to illustrate Multiplication operator | ||
+ | |||
+ | import java.io.*; | ||
+ | |||
+ | class Multiplication { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | // initializing variables | ||
+ | int num1 = 20, num2 = 10, mult = 0; | ||
+ | |||
+ | // Displaying num1 and num2 | ||
+ | System.out.println("num1 = " + num1); | ||
+ | System.out.println("num2 = " + num2); | ||
+ | |||
+ | // Multiplying num1 and num2 | ||
+ | mult = num1 * num2; | ||
+ | System.out.println("Multiplication = " + mult); | ||
+ | } | ||
+ | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Output | Output | ||
− | num1 = 20 | + | num1 = 20 |
− | num2 = 10 | + | num2 = 10 |
− | Multiplication = 200 | + | Multiplication = 200 |
− | 4. Division(/): | + | |
+ | ==4. Division(/):== | ||
+ | |||
+ | Ini adalah operator biner yang digunakan untuk membagi operan pertama (dividen) dengan operan kedua (pembagi) dan memberikan hasil bagi sebagai hasilnya. | ||
+ | |||
+ | Sintaks: | ||
+ | |||
+ | num1 / num2 | ||
− | + | Contoh: | |
− | num1 / num2 | + | num1 = 20, num2 = 10 |
− | + | div = num1 / num2 = 2 | |
− | num1 = 20, num2 = 10 | + | // Java code to illustrate Division operator |
− | div = num1 / num2 = | + | |
+ | import java.io.*; | ||
+ | |||
+ | class Division { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | // initializing variables | ||
+ | int num1 = 20, num2 = 10, div = 0; | ||
+ | |||
+ | // Displaying num1 and num2 | ||
+ | System.out.println("num1 = " + num1); | ||
+ | System.out.println("num2 = " + num2); | ||
+ | |||
+ | // Dividing num1 and num2 | ||
+ | div = num1 / num2; | ||
+ | System.out.println("Division = " + div); | ||
+ | } | ||
+ | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Output | Output | ||
− | num1 = 20 | + | num1 = 20 |
− | num2 = 10 | + | num2 = 10 |
− | Division = 2 | + | Division = 2 |
− | 5. Modulus(%): | + | |
+ | ==5. Modulus(%):== | ||
− | + | Ini adalah operator biner yang digunakan untuk mengembalikan sisa ketika operan pertama (dividen) dibagi dengan operan kedua (pembagi). | |
− | + | Sintaks: | |
− | |||
− | num1 = 5, num2 = 2 | + | num1 % num2 |
− | mod = num1 % num2 = 1 | + | |
+ | Contoh: | ||
+ | |||
+ | num1 = 5, num2 = 2 | ||
+ | mod = num1 % num2 = 1 | ||
+ | |||
+ | // Java code to illustrate Modulus operator | ||
+ | |||
+ | import java.io.*; | ||
+ | |||
+ | class Modulus { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | // initializing variables | ||
+ | int num1 = 5, num2 = 2, mod = 0; | ||
+ | |||
+ | // Displaying num1 and num2 | ||
+ | System.out.println("num1 = " + num1); | ||
+ | System.out.println("num2 = " + num2); | ||
+ | |||
+ | // Remaindering num1 and num2 | ||
+ | mod = num1 % num2; | ||
+ | System.out.println("Remainder = " + mod); | ||
+ | } | ||
+ | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Output | Output | ||
− | num1 = 5 | + | num1 = 5 |
− | num2 = 2 | + | num2 = 2 |
− | Remainder = 1 | + | Remainder = 1 |
− | |||
Latest revision as of 06:16, 6 May 2022
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types:
Operator merupakan blok bangunan dasar untuk bahasa pemrograman apa pun. Java juga menyediakan banyak jenis operator yang dapat digunakan sesuai dengan kebutuhan untuk melakukan berbagai perhitungan dan fungsi, baik itu logika, aritmatika, relasional, dll. Diklasifikasikan berdasarkan fungsionalitas yang disediakan. Berikut beberapa jenisnya:
- Operator Arithmetic
- Operator Unary
- Operator Assignment
- Operator Relational
- Operator Logical
- Operator Ternary
- Operator Bitwise
- Operator Shift
Artikel ini menjelaskan semua yang perlu diketahui tentang Operator Aritmatika.
Operator Arithmetic
Operator ini melibatkan operator matematika yang dapat digunakan untuk melakukan berbagai operasi aritmatika sederhana atau lanjutan pada tipe data primitif yang disebut sebagai operan. Operator ini terdiri dari berbagai operator unary dan biner yang dapat diterapkan pada satu atau dua operan. Mari kita lihat berbagai operator yang disediakan Java di bawah operator aritmatika.
Sekarang mari kita lihat masing-masing operator aritmatika di Java:
1. Addition(+):
Operator ini adalah operator biner dan digunakan untuk menambahkan dua operan.
Sintaks:
num1 + num2
Contoh:
num1 = 10, num2 = 20 sum = num1 + num2 = 30
// Java code to illustrate Addition operator import java.io.*; class Addition { public static void main(String[] args) { // initializing variables int num1 = 10, num2 = 20, sum = 0; // Displaying num1 and num2 System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); // adding num1 and num2 sum = num1 + num2; System.out.println("The sum = " + sum); } }
Output
num1 = 10 num2 = 20 The sum = 30
2. Subtraction(-):
Operator ini adalah operator biner dan digunakan untuk mengurangi dua operan.
Sintaks:
num1 - num2
Contoh:
num1 = 20, num2 = 10 sub = num1 - num2 = 10
// Java code to illustrate Subtraction operator import java.io.*; class Subtraction { public static void main(String[] args) { // initializing variables int num1 = 20, num2 = 10, sub = 0; // Displaying num1 and num2 System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); // subtracting num1 and num2 sub = num1 - num2; System.out.println("Subtraction = " + sub); } }
Output
num1 = 20 num2 = 10 Subtraction = 10
3. Multiplication(*):
Operator ini adalah operator biner dan digunakan untuk mengalikan dua operan.
Sintaks:
num1 * num2
Contoh:
num1 = 20, num2 = 10 mult = num1 * num2 = 200
// Java code to illustrate Multiplication operator import java.io.*; class Multiplication { public static void main(String[] args) { // initializing variables int num1 = 20, num2 = 10, mult = 0; // Displaying num1 and num2 System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); // Multiplying num1 and num2 mult = num1 * num2; System.out.println("Multiplication = " + mult); } }
Output
num1 = 20 num2 = 10 Multiplication = 200
4. Division(/):
Ini adalah operator biner yang digunakan untuk membagi operan pertama (dividen) dengan operan kedua (pembagi) dan memberikan hasil bagi sebagai hasilnya.
Sintaks:
num1 / num2
Contoh:
num1 = 20, num2 = 10 div = num1 / num2 = 2
// Java code to illustrate Division operator import java.io.*; class Division { public static void main(String[] args) { // initializing variables int num1 = 20, num2 = 10, div = 0; // Displaying num1 and num2 System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); // Dividing num1 and num2 div = num1 / num2; System.out.println("Division = " + div); } }
Output
num1 = 20 num2 = 10 Division = 2
5. Modulus(%):
Ini adalah operator biner yang digunakan untuk mengembalikan sisa ketika operan pertama (dividen) dibagi dengan operan kedua (pembagi).
Sintaks:
num1 % num2
Contoh:
num1 = 5, num2 = 2 mod = num1 % num2 = 1
// Java code to illustrate Modulus operator import java.io.*; class Modulus { public static void main(String[] args) { // initializing variables int num1 = 5, num2 = 2, mod = 0; // Displaying num1 and num2 System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); // Remaindering num1 and num2 mod = num1 % num2; System.out.println("Remainder = " + mod); } }
Output
num1 = 5 num2 = 2 Remainder = 1