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...")
 
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 Operators
 
* Arithmetic Operators
Line 11: Line 14:
  
 
==Unary Operators in Java==
 
==Unary Operators in 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.  
 
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 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.  
 
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:  
 
Syntax:  
Line 60: Line 68:
  
 
This is used to convert true to false or vice versa. Basically, it reverses the logical state of an operand.
 
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:  
 
Syntax:  
Line 111: Line 121:
  
 
It is used to increment the value of an integer. It can be used in two separate ways:  
 
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 cara terpisah:
  
 
===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.  
 
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:  
 
Syntax:  
Line 128: Line 142:
  
 
When placed before the variable name, the operand’s value is incremented instantly.
 
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:  
 
Syntax:  
Line 141: Line 157:
  
 
It is used to decrement the value of an integer. It can be used in two separate ways:  
 
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 cara terpisah:
  
 
===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.  
 
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:  
 
Syntax:  
Line 158: Line 178:
  
 
When placed before the variable name, the operand’s value is decremented instantly.  
 
When placed before the variable name, the operand’s value is decremented instantly.  
 +
 +
Saat ditempatkan sebelum nama variabel, nilai operan langsung dikurangi.
  
 
Syntax:  
 
Syntax:  
Line 171: Line 193:
  
 
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.  
 
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:  
 
Syntax:  
Line 182: Line 206:
  
 
This performs a bitwise complement of 5
 
This performs a bitwise complement of 5
 +
 +
Ini melakukan pelengkap bitwise 5
 +
  
 
  ~0101 = 1010 = 10 (in decimal)
 
  ~0101 = 1010 = 10 (in decimal)
  
Then the compiler will give 2’s complement
+
Then the compiler will give 2’s complement of that number.
of that number.
 
 
2’s complement of 10 will be -6.
 
2’s complement of 10 will be -6.
 
result = -6
 
result = -6
 +
 +
 +
Kemudian compiler akan memberikan komplemen 2 dari bilangan tersebut.
 +
Komplemen 2 dari 10 akan menjadi -6.
 +
hasil = -6
 +
 +
 
Example:
 
Example:
  
 +
// 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
  
  

Revision as of 06:59, 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:


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

Unary Operators in 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 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(-)

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:

~(operand)

Illustration:

a = -10

Example:

// 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(!)

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:

!(operand)

Illustration:

cond = !true;
// cond < false

Example:

// 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(++)

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 cara terpisah:

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:

num++

Illustration:

num = 5
num++ = 6

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:

++num

Illustration:

num = 5
++num = 6

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 cara terpisah:

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:

num--

Illustration:

num = 5
num-- = 4

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:

--num

Illustration:

num = 5
--num = 4

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:

~(operand)

Illustration:

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

This performs a bitwise complement of 5

Ini melakukan pelengkap bitwise 5


~0101 = 1010 = 10 (in decimal)

Then the compiler will give 2’s complement of that number. 2’s complement of 10 will be -6. result = -6


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


Example:

// 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