What are unary operator in java

Unary Operators in Java with Examples

The word Unary means an operation that involves a single element. As the name suggests, The Unary operators in Java involve single operand. Java supports following unary operators:

1. Unary minus(-) Operator Example

The unary minus operator changes the sign of the operand. This operator is used on numbers, it changes the positive number to negative number and vice-versa.

int num = 100; int num2 = -num; //value of num2 is -100

Difference between – arithmetic operator and – unary operator:

The – arithmetic operator involves two operands while the – unary operator involves a single operand.

int x = 10, y = 5, z = 100, sub; //this is minus(-) arithmetic operation sub = x - y; //this is minus (-) unary operation int num = -z; //value of num is -100

Example of unary minus:

output unary minus(-) operator

2. Increment(++) Operator Example

The increment(++) operator is used to increase the value of a variable. It can be used in following two ways:

Читайте также:  Php объектно ориентированное примеры

Difference between pre increment and post increment in java:

In pre-increment the value is incremented instantly, however in post-increment the value is incremented at the end of this statement and before executing next statement. This is shown in the following example:

Here the first if statement didn’t execute as the value of num1 is still 5 during comparison and only got increment after the condition is already evaluated. The second if statement executed because pre-increment increased the value instantly before reading the remaining statement, this made the value of num2 to 6 before comparison.

class JavaExample < public static void main(String[] args) < int num1 = 5, num2 = 5; //post-increment if(num1++ == 6)< System.out.println("First if block"); System.out.println("Value of num1: "+num1); >//pre-increment if(++num2 == 6) < System.out.println("Second if block"); System.out.println("Value of num2: "+num2); >> >

Unary operator example output

3. Decrement(- -) Operator Example

The decrement(- -) operator is used to decrease the value of an operand. Similar to ++ operator, it can also be used in following two ways:

Difference between pre decrement and post decrement in java:

In pre-decrement the value is decremented instantly, however in post-decrement the value is decremented at the end of this statement and before executing next statement. For example:

class JavaExample < public static void main(String[] args) < int num1 = 5, num2 = 5; //post-decrement if(num1-- == 4)< System.out.println("Post-decrement Operator"); System.out.println("Value of num1: "+num1); >//pre-decrement if(--num2 == 4) < System.out.println("Pre-decrement Operator"); System.out.println("Value of num2: "+num2); >> >

Post and pre-decrement operator java

4. NOT(!) Operator Example

The NOT(!) Operator reverses the logical state (true or false) of an operand. If an operand or condition is true, then the Logical NOT operator will make it false and vice-versa.

If value of a boolean variable 'b' is true, then !b is false If the value of 'b' is false, then !b is true

Let’s take an example to understand this concept:

class JavaExample < public static void main(String[] args) < int totalMarks = 32; //if total marks are not greater than 33 then //the student is fail else pass if(!(totalMarks >33)) < System.out.println("Fail"); >else < System.out.println("Pass"); >> >

5. Bitwise Complement(~) Operator Example

Bitwise complement is represented by ~ (Tilde) symbol. The bitwise complement is a 1’s complement representation of number. 1’s complement changes the 1’s to 0’s and 0’s to 1’s.
For example: In the following java program, we got -8 as a bitwise complement of 7, let’s see how we got this value.

Binary representation of number 7 = 111
1’s complement of this: 000 (reversed 1’s to 0’s and 0’s to 1’s)
decimal equivalent of this: 0

The compiler returns a 2’s complement of an input number, this means that compiler will print the 2’s complement of 0.

2’s complement= 1’s complement + add 1 to Least Significant Bit (LSB).
= 111 + 1
= 1000
Decimal equivalent of this 2’s complement is -8.

Источник

What are Unary Operators in Java?

Java Course - Mastering the Fundamentals

Operators in Java are an essential part of any programming language. They allow developers to perform various calculations and operations, such as arithmetic, logical, and relational. Java, like many other programming languages, offers a wide range of operators that can be used to execute various tasks and make programming more efficient.

Unary operators are a type of operator that only require one operand to perform an operation. These operations can include incrementing or decrementing a value, reversing a boolean value, or other types of calculations. Examples of unary operators include the increment operator (++), decrement operator (—), and negation operator (!). It is important to note that Unary operators can be applied to different types like arithmetic, logic, and others.

Types of Unary operators are:

Types of Unary operator

Types of Unary Operators in Java

Let’s see different types of unary operators in Java :

Symbol Operator Name Description
Unary Minus It is used to denote a negative value.
+ Unary Plus It is used to denote a positive value.
— — Decrement Operator Decrements value by 1
++ Increment Operator Increments value by 1
! Logical Complement Operator Reverse the value of boolean variable

Unary Plus

It is used to represent a positive operand. In this, the use of the operator is optional that is we do not have to write the operator before operand.

Unary Minus

It converts a positive value into a negative value and vice versa.

Explanation:

  • In the code above, the value of x1 was initially 12 before the unary minus( — ) operator was applied to it as a result, it changed to -12 since the unary operator negated its value.

Increment Operator

The increment operator in java is used to increase the value of the variable(operand) by one( 1 ). The symbol for the operator is two plus signs ( ++ ). The operator can be applied before or after the operand.

In java increment operator is used in two forms:

Pre-increment Operator:

Pre-increment operator is defind as when the increment operator( ++ ) is written before a variable then it is known as Pre-increment operator or Prefix. The Pre-increment operator works in such a way that, first the value is incremented by one( 1 ), then returns the new incremented value.

Explanation:

  • In the very first statement, a value 12 has been assigned to a.
  • Then, in the next statement, it performs two actions.
  • It increments the value of a to 13 .
  • Then assign the new incremented value to b.

Post-increment Operator:

Post-increment operator is defined as when the increment operator( ++ ) is written after a variable then it is known as Post-increment operator or Postfix. The Post-increment operator works in such a way that, initially, the operator returns the value of the operand, and finally, the value is incremented by one( 1 ).

Explanation:

  • In the above code, we can see that, first, the post-increment operator assigns the value in the left-hand side variable b.
  • Then the operator increments the value of a by 1 .

Note: Both i += 1 and i++ evaluate the same result but the only difference between them is that i++ uses the increment operator(unary operator) while i+=1 uses the assignment operator.

Decrement Operator

Decrement operator in java is used to decrement the value of operand by one( 1 ). The symbol for the operator is two minus signs ( — ). Same as of increment operator it can also be applied before and after an operand.

In java decrement operator is used in two forms:

Pre-decrement Operator:

Pre-decrement operator is defind as when the decrement operator( — ) is written before a variable then it is known as Pre-decrement operator or Prefix. The Pre-decrement operator works in such a way that, first the value is decremented by one( 1 ), and then it returns the new decremented value.

Explanation:

  • In the very first statement, a value 12 has been assigned to a.
  • Then in the next statement, it performs two actions.
  • It decrements the value of a to 11 .
  • Then assign the new decremented value to b.

Post-decrement Operator:

Post-decrement operators, also known as Postfixes, are created when the decrement operator ( — ) is written after a variable. The Post-decrement operator works in such a way that, the operator first returns the value of the operand and then at the end, the value is decremented by one( 1 ).

Explanation:

  • In the above code we can see that, first the post-decrement operator assign the value in the left hand side variable b.
  • Then the operator decrements the value of a by 1 .

Logical Complement Operator

A logical Complement Operator is used to reverse the value of a Boolean value. It means that if the value of an operand is true, then the complement of the operator will be false and vice-versa. It is represented by an exclamatory symbol ( ! ).

Explanation:

  • In the code above, first, we declare two Boolean variables that are Var1 and Var2.
  • Then Logical Complement Operator ( ! ) applies on Var1 and updated the value of Var1.
  • Then Logical Complement Operator ( ! ) applies on the Var2 and updated the value of Var2.
  • The Boolean values of the variables are now reversed.

Conclusion

  • You can modify the sign, conduct an increment or decrement, or change the Boolean value ( true/false ) using the unary operators.
  • Unary Plus is used to represent positive values.
  • Unary Minus is used to convert positive value to negative value.
  • Increment operator is used to increment the value of a variable by one( 1 ).
  • Decrement operator is used to decrement the value of a variable by one( 1 ).
  • Logical Complement Operator is used to reverse the boolean value from true to false and vice-versa.

Источник

What are unary operator in java

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью