• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

What Does != Mean in Java

July 28, 2023 //  by Talha Malik

In Java, the operators play a key role in evaluating the initialized or user input values and computing the mathematical expressions as well. The “!=” is one such comparison operator that compares the provided values and returns the corresponding outcome as a Boolean or can be used in loops as well.

What Does != Mean in Java?

The “!=” corresponds to the not equal comparison operator that returns a Boolean outcome “true” if the left operand does not equal the right operand and “false” otherwise.

Example 1: Applying Java “!=” Operator

This example applies the “!=” operator to evaluate the initialized integers and return the corresponding Boolean outcome:

public class Notequaloper {
 public static void main(String[] args) {
  int x = 3;
  int y = 5;
  boolean out = x!=y;
  System.out.println("Are the Values Equal ? " +out);
}}

In this code:

  • Initialize the stated integers that need to be checked for inequality.
  • Now, apply the “!=” operator to check if both the defined values are not equal and store the result in the “Boolean” type variable.
  • Lastly, return the corresponding Boolean outcome.

Output

This outcome indicates that the Boolean value “true” is returned against the applied “!=” operator since the values are unequal.

Example 2: Applying Java “!=” Operator With “if/else” Statement

In this specific example, the discussed operator can be implemented with an “if/else” statement to invoke the corresponding condition: 

public class Notequaloper {
 public static void main(String[] args) {
  int x = 3;
  int y = 5;
  if (x!=y)
   System.out.println("The values " +x+ " and " +y+ " are not equal!");
  else
   System.out.println("The values " +x+ " and " +y+ " are equal!");
}}

According to these code lines:

  • Likewise, initialize the given integers.
  • After that, apply the “!=” operator in the “if” statement to verify if both values are unequal.
  • If so, the “if” condition executes. Else, the “else” condition will come into effect.

Output

Example 3: Applying Java != Operator Upon the User Input Values

In this particular demonstration, the “!=” operator will be applied to evaluate the user input values for inequality:

import java.util.Scanner;
public class Notequaloper {
 public static void main(String[] args) {
  Scanner x = new Scanner(System.in);
  System.out.println("Input the first value: ");
  int a = x.nextInt();
  System.out.println("Input the second value: ");
  int b = x.nextInt();
  if (a!=b)
   System.out.println("The values " +a+ " and " +b+ " are not equal!");
  else
   System.out.println("The values " +a+ " and " +b+ " are equal!");
   x.close();
}}

According to this block of code:

  • Import the “java.util.Scanner” package to enable user input.
  • Create a Scanner object to allow user input.
  • Now, input two values from the user via the “nextInt()” method.
  • After that, apply the not equal “!=” operator to check if both the user input values are not equal and invoke the corresponding “if/else” condition.

Output

In this outcome, both scenarios (values being equal and not) are demonstrated accordingly.

Conclusion

The “!=” corresponds to the “not equal” operator that returns a Boolean outcome “true” if the left operand does not equal the right operand and “false” otherwise. This tutorial demonstrated the concept and working of the Java “!=” operator.

Category: Java

Previous Post: « What is the Difference Between ++i and i++ in Java
Next Post: What is a Continuation in Java? »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Initialize an Array in Java

Introduction to Java Server Faces (JSF)

Introduction to Java 6.0 New Features, Part–1

Java 6.0 Features Part – 2 : Pluggable Annotation Processing API

Introduction to Java Server Faces(JSF) HTML Tags

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact