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

From OnnoWiki
Jump to navigation Jump to search
(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...")
 
 
(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:
+
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 Operators
+
* Operator Arithmetic
* Unary Operators
+
* Operator Unary
* Assignment Operator
+
* Operator Assignment
* Relational Operators
+
* Operator Relational
* Logical Operators
+
* Operator Logical
* Ternary Operator
+
* Operator Ternary Operator
* Bitwise Operators
+
* Operator Bitwise
* Shift Operators
+
* Operator Shift
  
==Unary Operators in Java==
+
==Operator Unary di Java==
Java unary operators are the types that need only one operand to perform any operation like increment, decrement, negation, etc. It consists of various arithmetic, logical and other operators that operate on a single operand. Let’s look at the various unary operators in detail and see how they operate.  
+
 
 +
Operator Unary di Java adalah jenis yang hanya membutuhkan satu operan untuk melakukan operasi apa pun seperti kenaikan, penurunan, negasi, dll. Ini terdiri dari berbagai operator aritmatika, logika, dan lainnya yang beroperasi pada satu operan. Mari kita lihat berbagai operator unary secara rinci dan lihat bagaimana mereka beroperasi.
  
 
==Operator 1: Unary minus(-)==
 
==Operator 1: Unary minus(-)==
  
This operator can be used to convert a positive value to a negative one.  
+
Operator ini dapat digunakan untuk mengubah nilai positif menjadi negatif.
  
Syntax:  
+
Sintaks:  
  
 
  ~(operand)
 
  ~(operand)
  
Illustration:  
+
Ilustrasi:  
  
 
  a = -10
 
  a = -10
  
Example:
+
Contoh:
  
 
  // Java Program to Illustrate Unary - Operator
 
  // Java Program to Illustrate Unary - Operator
Line 59: Line 60:
 
==Operator 2: ‘NOT’ Operator(!)==
 
==Operator 2: ‘NOT’ Operator(!)==
  
This is used to convert true to false or vice versa. Basically, it reverses the logical state of an operand.
+
Ini digunakan untuk mengubah true menjadi false atau sebaliknya. Pada dasarnya, ini membalikkan keadaan logis operan.
  
Syntax:  
+
Sintaks:  
  
 
  !(operand)
 
  !(operand)
  
Illustration:  
+
Ilustrasi:  
  
 
  cond = !true;
 
  cond = !true;
 
  // cond < false
 
  // cond < false
  
Example:
+
Contoh:
  
 
  // Java Program to Illustrate Unary NOT Operator
 
  // Java Program to Illustrate Unary NOT Operator
Line 110: Line 111:
 
==Operator 3: Increment(++)==
 
==Operator 3: Increment(++)==
  
It is used to increment the value of an integer. It can be used in two separate ways:  
+
Digunakan untuk menambah nilai integer. Ini dapat digunakan dalam dua (2) cara yang berbeda:
  
 
===3.1: Post-increment operator===
 
===3.1: Post-increment operator===
  
When placed after the variable name, the value of the operand is incremented but the previous value is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement.  
+
Ketika ditempatkan setelah nama variabel, nilai operan bertambah tetapi nilai sebelumnya dipertahankan sementara sampai eksekusi pernyataan ini dan diperbarui sebelum eksekusi pernyataan berikutnya.
  
Syntax:  
+
Sintaks:  
  
 
  num++
 
  num++
  
Illustration:  
+
Ilustrasi:  
  
 
  num = 5
 
  num = 5
Line 127: Line 128:
 
===3.2: Pre-increment operator===
 
===3.2: Pre-increment operator===
  
When placed before the variable name, the operand’s value is incremented instantly.
+
Ketika ditempatkan sebelum nama variabel, nilai operan akan bertambah secara instan.
  
Syntax:  
+
Sintaks:  
  
 
  ++num
 
  ++num
  
Illustration:  
+
Ilustrasi:  
  
 
  num = 5
 
  num = 5
Line 140: Line 141:
 
==Operator 4: Decrement(–)==
 
==Operator 4: Decrement(–)==
  
It is used to decrement the value of an integer. It can be used in two separate ways:  
+
 
 +
Digunakan untuk mengurangi nilai integer. Ini dapat digunakan dalam dua (2) cara yang berbeda:
  
 
===4.1: Post-decrement operator===
 
===4.1: Post-decrement operator===
  
When placed after the variable name, the value of the operand is decremented but the previous values is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement.  
+
Ketika ditempatkan setelah nama variabel, nilai operan dikurangi tetapi nilai sebelumnya dipertahankan sementara sampai eksekusi pernyataan ini dan diperbarui sebelum eksekusi pernyataan berikutnya.
  
Syntax:  
+
Sintaks:  
  
 
  num--
 
  num--
  
Illustration:  
+
Ilustrasi:  
  
 
  num = 5
 
  num = 5
Line 157: Line 159:
 
===4.2: Pre-decrement operator===
 
===4.2: Pre-decrement operator===
  
When placed before the variable name, the operand’s value is decremented instantly.  
+
Saat ditempatkan sebelum nama variabel, nilai operan langsung dikurangi.
  
Syntax:  
+
Sintaks:  
  
 
  --num
 
  --num
  
Illustration:
+
Ilustrasi:
  
 
  num = 5
 
  num = 5
Line 170: Line 172:
 
==Operator 5: Bitwise Complement(~)==
 
==Operator 5: Bitwise Complement(~)==
  
This unary operator returns the one’s complement representation of the input value or operand, i.e, with all bits inverted, which means it makes every 0 to 1, and every 1 to 0.  
+
Operator unary ini mengembalikan representasi komplemen satu dari nilai input atau operan, yaitu dengan semua bit dibalik, yang berarti membuat setiap 0 menjadi 1, dan setiap 1 menjadi 0.
  
Syntax:  
+
Sintaks:  
  
 
  ~(operand)
 
  ~(operand)
  
Illustration:  
+
Ilustrasi:  
  
 
  a = 5 [0101 in Binary]
 
  a = 5 [0101 in Binary]
 
  result = ~5
 
  result = ~5
  
This performs a bitwise complement of 5
+
Ini melakukan operasi complement bitwise dari 5
 +
 
  
 
  ~0101 = 1010 = 10 (in decimal)
 
  ~0101 = 1010 = 10 (in decimal)
  
Then the compiler will give 2’s complement
+
Kemudian compiler akan memberikan komplemen 2 dari bilangan tersebut.
of that number.
+
Komplemen 2 dari 10 akan menjadi -6.
2’s complement of 10 will be -6.
+
hasil = -6
result = -6
+
 
Example:
 
  
 +
Contoh:
 +
 +
// Java program to Illustrate Unary
 +
// Bitwise Complement Operator
 +
 
 +
// Importing required classes
 +
import java.io.*;
 +
 
 +
// Main class
 +
class GFG {
 +
 
 +
    // Main driver method
 +
    public static void main(String[] args)
 +
    {
 +
        // Declaring a variable
 +
        int n1 = 6, n2 = -2;
 +
 
 +
        // Printing numbers on console
 +
        System.out.println("First Number = " + n1);
 +
        System.out.println("Second Number = " + n2);
 +
 
 +
        // Printing numbers on console after
 +
        // performing bitwise complement
 +
        System.out.println(
 +
            n1 + "'s bitwise complement = " + ~n1);
 +
        System.out.println(
 +
            n2 + "'s bitwise complement = " + ~n2);
 +
    }
 +
}
  
// Java program to Illustrate Unary
 
// Bitwise Complement Operator
 
 
// Importing required classes
 
import java.io.*;
 
 
// Main class
 
class GFG {
 
 
    // Main driver method
 
    public static void main(String[] args)
 
    {
 
        // Declaring a variable
 
        int n1 = 6, n2 = -2;
 
 
        // Printing numbers on console
 
        System.out.println("First Number = " + n1);
 
        System.out.println("Second Number = " + n2);
 
 
        // Printing numbers on console after
 
        // performing bitwise complement
 
        System.out.println(
 
            n1 + "'s bitwise complement = " + ~n1);
 
        System.out.println(
 
            n2 + "'s bitwise complement = " + ~n2);
 
    }
 
}
 
 
Output:  
 
Output:  
First Number = 6
+
First Number = 6
Second Number = -2
+
Second Number = -2
6's bitwise complement = -7
+
6's bitwise complement = -7
-2's bitwise complement = 1
+
-2's bitwise complement = 1
  
  

Latest revision as of 07:11, 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. Berikut beberapa jenisnya:

  • Operator Arithmetic
  • Operator Unary
  • Operator Assignment
  • Operator Relational
  • Operator Logical
  • Operator Ternary Operator
  • Operator Bitwise
  • Operator Shift

Operator Unary di Java

Operator Unary di Java adalah jenis yang hanya membutuhkan satu operan untuk melakukan operasi apa pun seperti kenaikan, penurunan, negasi, dll. Ini terdiri dari berbagai operator aritmatika, logika, dan lainnya yang beroperasi pada satu operan. Mari kita lihat berbagai operator unary secara rinci dan lihat bagaimana mereka beroperasi.

Operator 1: Unary minus(-)

Operator ini dapat digunakan untuk mengubah nilai positif menjadi negatif.

Sintaks:

~(operand)

Ilustrasi:

a = -10

Contoh:

// Java Program to Illustrate Unary - Operator
 
// Importing required classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring a custom variable
        int n1 = 20;
 
        // Printing the above variable
        System.out.println("Number = " + n1);
 
        // Performing unary operation
        n1 = -n1;
 
        // Printing the above result number
        // after unary operation
        System.out.println("Result = " + n1);
    }
}

Output

Number = 20
Result = -20

Operator 2: ‘NOT’ Operator(!)

Ini digunakan untuk mengubah true menjadi false atau sebaliknya. Pada dasarnya, ini membalikkan keadaan logis operan.

Sintaks:

!(operand)

Ilustrasi:

cond = !true;
// cond < false

Contoh:

// Java Program to Illustrate Unary NOT Operator
 
// Importing required classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing variables
        boolean cond = true;
        int a = 10, b = 1;
 
        // Displaying values stored in above variables
        System.out.println("Cond is: " + cond);
        System.out.println("Var1 = " + a);
        System.out.println("Var2 = " + b);
 
        // Displaying values stored in above variables
        // after applying unary NOT operator
        System.out.println("Now cond is: " + !cond);
        System.out.println("!(a < b) = " + !(a < b));
        System.out.println("!(a > b) = " + !(a > b));
    }
}

Output:

Cond is: true
Var1 = 10
Var2 = 1
Now cond is: false
!(a < b) = true
!(a > b) = false

Operator 3: Increment(++)

Digunakan untuk menambah nilai integer. Ini dapat digunakan dalam dua (2) cara yang berbeda:

3.1: Post-increment operator

Ketika ditempatkan setelah nama variabel, nilai operan bertambah tetapi nilai sebelumnya dipertahankan sementara sampai eksekusi pernyataan ini dan diperbarui sebelum eksekusi pernyataan berikutnya.

Sintaks:

num++

Ilustrasi:

num = 5
num++ = 6

3.2: Pre-increment operator

Ketika ditempatkan sebelum nama variabel, nilai operan akan bertambah secara instan.

Sintaks:

++num

Ilustrasi:

num = 5
++num = 6

Operator 4: Decrement(–)

Digunakan untuk mengurangi nilai integer. Ini dapat digunakan dalam dua (2) cara yang berbeda:

4.1: Post-decrement operator

Ketika ditempatkan setelah nama variabel, nilai operan dikurangi tetapi nilai sebelumnya dipertahankan sementara sampai eksekusi pernyataan ini dan diperbarui sebelum eksekusi pernyataan berikutnya.

Sintaks:

num--

Ilustrasi:

num = 5
num-- = 4

4.2: Pre-decrement operator

Saat ditempatkan sebelum nama variabel, nilai operan langsung dikurangi.

Sintaks:

--num

Ilustrasi:

num = 5
--num = 4

Operator 5: Bitwise Complement(~)

Operator unary ini mengembalikan representasi komplemen satu dari nilai input atau operan, yaitu dengan semua bit dibalik, yang berarti membuat setiap 0 menjadi 1, dan setiap 1 menjadi 0.

Sintaks:

~(operand)

Ilustrasi:

a = 5 [0101 in Binary]
result = ~5

Ini melakukan operasi complement bitwise dari 5


~0101 = 1010 = 10 (in decimal)

Kemudian compiler akan memberikan komplemen 2 dari bilangan tersebut. Komplemen 2 dari 10 akan menjadi -6. hasil = -6


Contoh:

// Java program to Illustrate Unary
// Bitwise Complement Operator
 
// Importing required classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring a variable
        int n1 = 6, n2 = -2;
 
        // Printing numbers on console
        System.out.println("First Number = " + n1);
        System.out.println("Second Number = " + n2);
 
        // Printing numbers on console after
        // performing bitwise complement
        System.out.println(
            n1 + "'s bitwise complement = " + ~n1);
        System.out.println(
            n2 + "'s bitwise complement = " + ~n2);
    }
}

Output:

First Number = 6
Second Number = -2
6's bitwise complement = -7
-2's bitwise complement = 1


Referensi