CodeGym /Blog Jawa /Acak /Nambah lan Ngurangi Operator Unary ing Jawa
John Squirrels
tingkat
San Francisco

# Nambah lan Ngurangi Operator Unary ing Jawa

## Operator Unary

Operator unary yaiku operator ing Jawa sing mung mbutuhake operan siji kanggo nindakake fungsi apa wae. Dheweke kerja ing prinsip sing padha karo operasi unary ing matematika. Contone, Sampeyan bisa nggunakake operator unary kanggo makili Nilai positif, Nilai negatif, nambah Nilai dening 1, decrement Nilai dening 1 utawa negate Nilai.
• +x (nilai positif)
• -x (nilai negatif)
• ++x (operasi increment)
• --x (pengurangan operasi)
• !x (negasi)

### Jinis Operator Unary

Ana 5 jinis Operator Unary

#### 1. Unary Plus

Iki nuduhake nilai positif kaya +x = x utawa +5 = 5.

#### 2. Unary Minus

Iki nuduhake nilai negatif kaya -x = -x utawa -5 = -5.

#### 3. Increment Unary Operator

Iku nambah nilai dening 1 ngendi ++x = x+1.

#### 4. Ngurangi Operator Unary

Ngurangi nilai kanthi 1 ing ngendi --x = x-1.

#### 5. Komplemen Logika

Iku logis mbalikke nilai boolean kaya yen x = bener, banjur !x bakal palsu.

## Operator Tambah (++)

Operator increment (++) (uga dikenal minangka operator unary increment) ing Jawa digunakake kanggo nambah nilai variabel kanthi 1. Wiwit iku jinis operator unary, bisa digunakake karo operan siji.

### Sintaksis

Sintaks kanggo operator increment minangka pasangan tandha tambahan yaiku;
++x; x++;
Operator bisa diterapake sadurunge utawa sawise variabel. Loro-lorone bakal duwe tambahan sing padha 1. Nanging, loro-lorone duwe panggunaan sing kapisah lan bisa dikategorikake minangka jinis ing ngisor iki.
• Operator Pra-Tambahan
• Operator Pasca-Tambahan

``````
public class IncrementOperator {

public static void main(String[] args) {

int variable = 15;
System.out.println("Original value of the variable = " + variable);

// after using increment operator
variable++; 	 //  increments 1, variable = 16
System.out.println("variable++ = " + variable);

++variable;		//  increments 1, variable = 17
System.out.println("++variable = " + variable);
}
}
``````

### Output

Nilai asli variabel = 15 variabel++ = 16 ++variabel = 17

## Operator Pra-Tambahan (++x;)

Yen operator increment (++) ditemtokake sadurunge variabel kaya prefiks (++x), banjur diarani operator pre-increment. Ing kasus iki, nilai variabel pisanan ditambah 1, banjur komputasi luwih lanjut ditindakake.

``````
public class PreIncrementOperator {

public static void main(String[] args) {

int variable = 5;
System.out.println("Original value of the variable = " + variable);

// using pre-increment operator
int preIncrement = ++variable;

System.out.println("variable = " + variable);
System.out.println("preIncrement = " + preIncrement);
System.out.println("++preIncrement = " + ++preIncrement);
}
}
``````

### Output

Nilai asli variabel = 5 variabel = 6 preIncrement = 6 ++preIncrement = 7

## Operator Pasca-Tambahan (x++;)

Yen operator increment (++) ditemtokake sawise variabel kaya postfix (x++), banjur diarani operator post-increment. Ing kasus iki, nilai asli variabel (tanpa tambahan) digunakake kanggo komputasi lan banjur ditambah 1.

``````
public class PostIncrementOperator {

public static void main(String[] args) {

int variable = 100;
System.out.println("Original value of the variable = " + variable);

// using post-increment operator
int postIncrement = variable++; // postIncrement = 100, variable = 101

System.out.println("postIncrement = " + postIncrement);
System.out.println("variable = " + variable + "\n");

// postIncrement = 101
System.out.println("postIncrement++ = " + postIncrement++);
// postIncrement = 102
System.out.println("postIncrement++ = " + postIncrement++);
// postIncrement = 103
System.out.println("postIncrement++ = " + postIncrement++);

System.out.println("\npostIncrement = " + postIncrement);
}
}
``````

### Output

Variabel asli = 100 postIncrement = 100 variabel = 101 postIncrement++ = 100 postIncrement++ = 101 postIncrement++ = 102 postIncrement = 103

## Operator Penurunan (--)

Decrement minangka jeneng sing digunakake kanggo ngurangi nilai variabel kanthi 1. Iku uga salah siji saka jinis operator unary, supaya bisa digunakake karo operan siji.

### Sintaksis

Sintaks kanggo operator decrement minangka pasangan tandha negatif yaiku;
--x; x--;
Kaya operator increment, operator decrement (--) uga bisa ditrapake sadurunge lan sawise variabel. Loro-lorone bakal nyebabake penurunan sing padha karo 1. Loro-lorone duwe panggunaan sing beda lan bisa diverged ing jinis liyane.
• Operator Pre-Decrement
• Operator Pasca Penurunan

## Operator Pre-Decrement (--x;)

Yen operator decrement (--) kasebut sadurunge variabel kaya prefiks (--x), banjur diarani operator pre-decrement. Ing kasus iki, nilai variabel pisanan dikurangi 1, banjur komputasi liyane ditindakake.

``````
public class PreDecrementOperator {

public static void main(String[] args) {

int variable = 11;
System.out.println("Original value of the variable = " + variable);

// using preDecrement operator
int preDecrement = --variable;

// variable = 10
System.out.println("variable = " + variable);
// preDecrement = 10
System.out.println("preDecrement = " + preDecrement);
// preDecrement = 9
System.out.println("--preDecrement = " + --preDecrement);  	}
}
``````

### Output

Nilai asli variabel = 11 variabel = 10 preDecrement = 10 --preDecrement = 9

## Operator Pasca Penurunan (x--;)

Yen operator decrement (--) kasebut sawise variabel kaya postfix (x--), banjur diarani operator post-decrement. Ing kasus iki, nilai asli saka variabel (tanpa decrement) digunakake kanggo komputasi lan banjur decremented dening 1.

``````
public class PostDecrementOperator {

public static void main(String[] args) {

int variable = 75;
System.out.println("Original value of the variable = " + variable);

// using postDecrement operator
// postDecrement = 75, variable = 74
int postDecrement = variable--;
System.out.println("postDecrement = " + postDecrement);
System.out.println("variable = " + variable + "\n");
// postDecrement = 74
System.out.println("postDecrement-- = " + postDecrement--);
// postDecrement = 73
System.out.println("postDecrement-- = " + postDecrement--);
// postDecrement = 72
System.out.println("postDecrement-- = " + postDecrement--);

System.out.println("\npostDecrement = " + postDecrement);
}
}
``````
Nilai asli variabel = 75 postDecrement = 75 variabel = 74 postDecrement-- = 75 postDecrement-- = 74 postDecrement-- = 73 postDecrement = 72

## Kesimpulan

Ing pungkasan kiriman iki, muga-muga sampeyan wis ngerti babagan operator unary increment lan decrement ing Jawa. Sampeyan disaranake kanggo latihan kasus wates lan masalah latihan liyane ing CodeGym supaya yakin karo katrampilan sampeyan. Kanggo nguatake apa sing sampeyan sinau, disaranake sampeyan nonton video pelajaran saka Kursus Jawa
Good luck lan seneng sinau!
Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION