Difference between revisions of "JAVA: Operator Assignment dengan Contoh"

From OnnoWiki
Jump to navigation Jump to search
Line 25: Line 25:
 
Operator Assignment umumnya terdiri dari dua (2) tipe, yaitu:
 
Operator Assignment umumnya terdiri dari dua (2) tipe, yaitu:
  
* Simple Assignment Operator: The Simple Assignment Operator is used with the “=” sign where the left side consists of the operand and the right side consists of a value. The value of the right side must be of the same data type that has been defined on the left side.
+
* Simple Assignment Operator: Simple Assignment Operator digunakan dengan tanda “=” dimana di sebelah kiri adalah operand dan di sebelah kanan adalah variabel nilai. Nilai di sebelah kanan harus sama dengan tipe data yang digunakan di sebelah kiri.
* Compound Assignment Operator: The Compound Operator is used where +,-,*, and / is used along with the = operator.
+
* Compound Assignment Operator: Compound Operator menggunakan tambahan +,-,*, dan / bersama operator = .
  
Let’s look at each of the assignment operators and how they operate:  
+
Berikut adalah masing-masing operator assignment dan bagaimana menggunakannya:  
  
 
==1. (=) operator:==
 
==1. (=) operator:==
  
This is the most straightforward assignment operator, which is used to assign the value on the right to the variable on the left. This is the basic definition of an assignment operator and how it functions.  
+
Ini adalah operator Assignment yang paling mudah, yang digunakan untuk menetapkan nilai di sebelah kanan ke variabel di sebelah kiri. Ini adalah definisi dasar dari operator Assignment dan bagaimana fungsinya.  
  
 
Sintaks:  
 
Sintaks:  

Revision as of 13:10, 6 May 2022

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.

Tipe Operator:

  • 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 Assignment.


Assignment Operators

Operator ini digunakan untuk memberikan nilai ke variabel. Operan sisi kiri dari operator Assignment adalah variabel, dan operan sisi kanan dari operator Assignment adalah nilai. Nilai di sisi kanan harus dari tipe data yang sama dengan operan di sisi kiri. Jika tidak, kompiler akan memunculkan kesalahan. Ini berarti bahwa operator Assignment memiliki asosiatifitas kanan ke kiri, yaitu, nilai yang diberikan di sisi kanan operator di assigned ke variabel di sebelah kiri. Oleh karena itu, nilai ruas kanan harus dideklarasikan sebelum digunakan atau harus berupa konstanta. Format umum dari operator Assignment adalah,

variable operator value;

Tipe dari operator Assignment di Java

Operator Assignment umumnya terdiri dari dua (2) tipe, yaitu:

  • Simple Assignment Operator: Simple Assignment Operator digunakan dengan tanda “=” dimana di sebelah kiri adalah operand dan di sebelah kanan adalah variabel nilai. Nilai di sebelah kanan harus sama dengan tipe data yang digunakan di sebelah kiri.
  • Compound Assignment Operator: Compound Operator menggunakan tambahan +,-,*, dan / bersama operator = .

Berikut adalah masing-masing operator assignment dan bagaimana menggunakannya:

1. (=) operator:

Ini adalah operator Assignment yang paling mudah, yang digunakan untuk menetapkan nilai di sebelah kanan ke variabel di sebelah kiri. Ini adalah definisi dasar dari operator Assignment dan bagaimana fungsinya.

Sintaks:

num1 = num2;

Contoh:

a = 10;
ch = 'y';
// Java code to illustrate "=" operator
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
        // Declaring variables
        int num;
        String name;
 
        // Assigning values
        num = 10;
        name = "GeeksforGeeks";
 
        // Displaying the assigned values
        System.out.println("num is assigned: " + num);
        System.out.println("name is assigned: " + name);
    }
}

Output

num is assigned: 10
name is assigned: GeeksforGeeks

2. (+=) operator:

This operator is a compound of ‘+’ and ‘=’ operators. It operates by adding the current value of the variable on the left to the value on the right and then assigning the result to the operand on the left.

Sintaks:

num1 += num2;

Contoh:

a += 10

This means,

a = a + 10
// Java code to illustrate "+="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 10, num2 = 20;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Adding & Assigning values
        num1 += num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}

Output

num1 = 10
num2 = 20
num1 = 30

3. (-=) operator:

This operator is a compound of ‘-‘ and ‘=’ operators. It operates by subtracting the variable’s value on the right from the current value of the variable on the left and then assigning the result to the operand on the left.

Sintaks:

num1 -= num2;

Contoh:

a -= 10

This means,

a = a - 10
// Java code to illustrate "-="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 10, num2 = 20;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Subtracting & Assigning values
        num1 -= num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}

Output

num1 = 10
num2 = 20
num1 = -10

4. (*=) operator:

This operator is a compound of ‘*’ and ‘=’ operators. It operates by multiplying the current value of the variable on the left to the value on the right and then assigning the result to the operand on the left.

Sintaks:

num1 *= num2;

Contoh:

a *= 10

This means,

a = a * 10 
// Java code to illustrate "*="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 10, num2 = 20;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Multiplying & Assigning values
        num1 *= num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}

Output

num1 = 10
num2 = 20
num1 = 200

5. (/=) operator:

This operator is a compound of ‘/’ and ‘=’ operators. It operates by dividing the current value of the variable on the left by the value on the right and then assigning the quotient to the operand on the left.

Sintaks:

num1 /= num2;

Contoh:

a /= 10

This means,

a = a / 10
// Java code to illustrate "/="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 20, num2 = 10;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Dividing & Assigning values
        num1 /= num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}

Output

num1 = 20
num2 = 10
num1 = 2

6. (%=) operator:

This operator is a compound of ‘%’ and ‘=’ operators. It operates by dividing the current value of the variable on the left by the value on the right and then assigning the remainder to the operand on the left.

Sintaks:

num1 %= num2;

Contoh:

a %= 3

This means,

a = a % 3
// Java code to illustrate "%="

import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 5, num2 = 3;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Moduling & Assigning values
        num1 %= num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}

Output

num1 = 5
num2 = 3
num1 = 2



Referensi