What does β€œ!” mean in Java?


Introduction

Java supports eight types of operators; the β€œ!” operator is one of them. In Java, an operator is a symbol used to execute operations. Operators are entities that can modify the values of operands. Java makes authoring, compiling, and debugging code simple. It is helpful in creating code that can be reused and applications that are modular. It was created with the goal of having a few implementation dependencies as feasible. Let’s discuss in detail about the Java operators, β€œ!” operator and where to use it with some working examples.

Java operators

Operators in Java are symbols that are used to conduct operations on variables and alter the values of the operands. Operators may also be used to create new variables. Each operator is responsible for a certain set of tasks. Consider the statement "5 plus 1 equals 6." In this expression, the numbers 5 and 1 are referred to as the operands, while the symbol "plus" is known as the operator.

The β€œ!” symbol is an operator in Java. The operator, as the name suggests, is used to perform some operation on the numbers or the variables, these operators are used in mathematics like subtraction, and addition which is used to perform some operation on the numbers. These variables or numbers are called the operand on which the operation is performed.

Syntax

int variable_sum= variable_1+ variable_2

Types of Java operators

  • Unary Operators βˆ’ These are the operators where only one operand is there. The increment operator is one of them. For example: A++, A--.

  • Arithmetic Operators βˆ’ These have two operands, like addition, subtraction multiplication, and division. For example, A+B, A-B, A*B.

  • Assignment Operators βˆ’ This operator is used for assigning the value to the variable. for example βˆ’

int a;
a=1;
  • Logical Operators βˆ’ Logical operators are used for the expressions and it evaluates whether the expression is true or false. For example -&&

    If((a>b)&&(b<c))

    The if statement will only execute only when both the expression given in the if statement is correct otherwise else statement will be executed.

  • Shift Operators βˆ’ The shift operator is used to move the bits of an integer left or right using multiplication or division. For example βˆ’

    A<<2

    B>>2

  • Bitwise Operators βˆ’ The bitwise operator acts on bit arrays, bit strings, and binary numbers. It is quick and straightforward, and the CPU directly supports it. Programming at the bit level is also known as the bitwise operation. For example βˆ’

    a &b

    a ^b

  • Ternary Operators βˆ’ In computer programming, a ternary operator is a kind of conditional operator that shortens the lines of code needed to execute comparisons or conditions. It takes the place of if-else statements or statements with nested if-else constructs. There are a few other names for it, including inline if, conditional operator, and ternary if.

    (b<y)?b:y;

  • Relational Operators βˆ’ The relational operator performs a comparison of two integers and produces a result of type boolean. Defining a relation or conducting a test between two operands is the purpose of this operator. For example βˆ’

    A<B

    A<=B

    A==B

What is "!" in Java?

The logical NOT operator in Java is represented by the symbol "!". Its purpose is to flip the value of a Boolean expression in the other direction.

For instance, if the value of the variable "x" is set to "true," the evaluation of the expression "!x" will provide the result "false." In the event, if "x" is equivalent to the value false, the phrase "!x" will evaluate the value true.

To generate more sophisticated boolean expressions, it may also be used in conjunction with other logical operators, such as && (which stands for AND) and || (which stands for OR).

It is also possible to use it as a postfix operator in some circumstances. One example of this would be the β€œ!” operator in a for-each loop, which allows one to iterate through a collection in the opposite order.

It is a unique operator that may be used to boolean expressions in order to signify the logical negation of such expressions.

For instance, if x is lower than y, the expression "!x > y" will be evaluated as true, but if not, it will be evaluated as false.

It is important to keep in mind that the symbol β€œ!” may have a number of distinct meanings depending on the setting; nevertheless, in this particular instance, we were referring to the NOT operator in logic.

Example 1

import Java.io.*;
public class example {
   public static void main(String[] args) {
      int a = 10; 
      int b = 5; 
      if(a != b) { 
	     System.out.println("a is not equal to b"); 
      } else {
         System.out.println("a is equal to b");
      }
   }
}

Output

Java -cp /tmp/NdRBVzS8LD example
a is not equal to b

Example 2

import Java.io.*;
public class example {
   public static void main(String[] args) {
      int x1 = 2; 
      int x2 = 5; 

      if(!(x1>x2))
      {
         System.out.println("true");
      }
      else
      {
         System.out.println("false");
      }
   }
}

Output

Java -cp /tmp/NdRBVzS8LD example
true

Conclusion

We have learned about Java operators and types of Java operators. We have also learned about the β€œ!” expression in Java. There are many ways to use the β€œ!” symbol with Java it depends on the setting. It can be used as a logical not operator or as a Boolean operator.

Updated on: 23-Aug-2023

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements