Boolean not equal java

Class Boolean

The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean .

In addition, this class provides many methods for converting a boolean to a String and a String to a boolean , as well as other constants and methods useful when dealing with a boolean .

This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.

Field Summary

Constructor Summary

Method Summary

Returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.

Returns true if and only if the system property named by the argument exists and is equal to, ignoring case, the string «true» .

Читайте также:  Java compiling more classes

Methods declared in class java.lang.Object

Field Details

TRUE

FALSE

TYPE

Constructor Details

Boolean

It is rarely appropriate to use this constructor. The static factory valueOf(boolean) is generally a better choice, as it is likely to yield significantly better space and time performance. Also consider using the final fields TRUE and FALSE if possible.

Boolean

It is rarely appropriate to use this constructor. Use parseBoolean(String) to convert a string to a boolean primitive, or use valueOf(String) to convert a string to a Boolean object.

Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string «true» . Otherwise, allocates a Boolean object representing the value false .

Method Details

parseBoolean

Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string «true» . Otherwise, a false value is returned, including for a null argument. Example: Boolean.parseBoolean(«True») returns true .
Example: Boolean.parseBoolean(«yes») returns false .

booleanValue

valueOf

Returns a Boolean instance representing the specified boolean value. If the specified boolean value is true , this method returns Boolean.TRUE ; if it is false , this method returns Boolean.FALSE . If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean) , as this method is likely to yield significantly better space and time performance.

valueOf

Returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string «true» . Otherwise, a false value is returned, including for a null argument.

toString

Returns a String object representing the specified boolean. If the specified boolean is true , then the string «true» will be returned, otherwise the string «false» will be returned.

toString

Returns a String object representing this Boolean’s value. If this object represents the value true , a string equal to «true» is returned. Otherwise, a string equal to «false» is returned.

hashCode

hashCode

equals

Returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.

getBoolean

Returns true if and only if the system property named by the argument exists and is equal to, ignoring case, the string «true» . A system property is accessible through getProperty , a method defined by the System class. If there is no property with the specified name, or if the specified name is empty or null, then false is returned.

compareTo

compare

Boolean.valueOf(x).compareTo(Boolean.valueOf(y))

logicalAnd

logicalOr

logicalXor

describeConstable

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Equality, Relational, and Conditional Operators

The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand. The majority of these operators will probably look familiar to you as well. Keep in mind that you must use » == «, not » = «, when testing if two primitive values are equal.

== equal to != not equal to > greater than >= greater than or equal to < less than 

The following program, ComparisonDemo , tests the comparison operators:

class ComparisonDemo < public static void main(String[] args)< int value1 = 1; int value2 = 2; if(value1 == value2) System.out.println("value1 == value2"); if(value1 != value2) System.out.println("value1 != value2"); if(value1 >value2) System.out.println("value1 > value2"); if(value1 < value2) System.out.println("value1 < value2"); if(value1 >

The Conditional Operators

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.

&& Conditional-AND || Conditional-OR

The following program, ConditionalDemo1 , tests these operators:

Another conditional operator is ?: , which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson). This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: "If someCondition is true , assign the value of value1 to result . Otherwise, assign the value of value2 to result ."

The following program, ConditionalDemo2 , tests the ?: operator:

Because someCondition is true, this program prints "1" to the screen. Use the ?: operator instead of an if-then-else statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).

The Type Comparison Operator instanceof

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

The following program, InstanceofDemo , defines a parent class (named Parent ), a simple interface (named MyInterface ), and a child class (named Child ) that inherits from the parent and implements the interface.

class InstanceofDemo < public static void main(String[] args) < Parent obj1 = new Parent(); Parent obj2 = new Child(); System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent)); System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child)); System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface)); System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent)); System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child)); System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface)); >> class Parent <> class Child extends Parent implements MyInterface <> interface MyInterface <>
obj1 instanceof Parent: true obj1 instanceof Child: false obj1 instanceof MyInterface: false obj2 instanceof Parent: true obj2 instanceof Child: true obj2 instanceof MyInterface: true

When using the instanceof operator, keep in mind that null is not an instance of anything.

Previous page: Assignment, Arithmetic, and Unary Operators
Next page: Bitwise and Bit Shift Operators

Источник

not equal example : (opposite of .equals java)

In this post, I will be sharing not equal example in Java. Before moving on to the examples, first, we will understand how do you write the not equals sign in Java, what is != operator, the difference between != and !a.equals(b).

How do you write the not equals sign in Java?

What is != operator?

!=(pronounced not equal to) is the opposite of the equality(==) operator. It will evaluate to true if the values of the two operands are different. It is a relational operator. != operator always returns the boolean value (true or false).

!= operator when operands are primitives(int, long, float, double)

When you are using primitive data types (int, long, float, double) then use the operator != to test that the primitive x is not equal to the another primitive y as shown below in the example:

public class PrimitiveNotEqualToExample  public static void main(String args[])  int x=10; int y=25; System.out.println( x!=y );// true long l1 = 24l; long l2 = 26l; System.out.println( l1!=l2 );// true double d1 = 23.0d; double d2 = 23.0d; System.out.println( d1!=d2 );// false byte b1 = 2; byte b2 = 5; System.out.println( b1!=b2 );// true short s1 = 12; short s2 = 13; System.out.println( s1!=s2 );// true float f1 = 12.0f; float f2 = 12.0f; System.out.println( f1!=f2 );// false > > 

When operands are objects (e.g String)

String class contains equals() method to compare one string to another. equals() method returns true if the strings compared are equal, otherwise false.
To do the opposite just put an exclamation mark at the start of the statement, for example !str1.equals(str2)

String str1 = "Java"; String str2 = "Hungry"; boolean notEqual = !str1.equals(str2); System.out.println( notEqual ); //true 

Given below is the example to compare objects

public class ObjectNotEqualExample  public static void main(String args[])  Student s1 = new Student("John", 123, "Male"); Student s2 = new Student("Alexa", 234, "Female"); Student s3 = s1; System.out.println(!s1.equals(s2));// true System.out.println(!s1.equals(s3));// false > > class Student  private String name; private int rollNo; private String gender; public Student(String name, int rollNo, String gender)  this.name = name; this.rollNo = rollNo; this.gender = gender; > > 

Difference between != and !x.equals(y) method

The main difference between the != relational operator and !x.equals(y) method is that != is used for primitives whereas the equals method is used to compare Objects.

That's all for today, please mention in the comments in case you have any questions related to not equal example in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry

Источник

Not Equals in Java

Not Equals in Java

This article shows how to use the != operator that we also call the not equals operator. We can also use ! with the equals() method to check the non-equality of the data.

Using the Not Equals Operator in Java

The most basic way to use the not equals operator is to check for equality between two variables.

The program has two int variables, num1 and num2 . Here, num1 contains the value 123 , and the num2 variable has 321 .

We create an if condition to check if the variables match or not. In the condition, write num1 != num2 where the variable on the left side of the operator is compared. The variable is on the right side of the operator.

The true block of the if condition executes when the condition is not met (when num1 is not equal to num2 ), and if they match, then the false block is executed.

As both the variables have different values, the true block of the condition executes.

public class JavaExample   public static void main(String[] args)    int num1 = 123;  int num2 = 321;   if (num1 != num2)   System.out.println("str1 and str2 are not equal");  > else   System.out.println("str1 and str2 are equal");  >   >  > 
str1 and str2 are not equal 

Using the Not Equals Operator With equals()

We can use the ! operator with the equals() method to check if the contents of the variables match or not.

In the example, we take two String variables. In the if condition, we check the str1.equals(str2) with a ! operator at the beginning.

The ! operator makes the result opposite, which means if the str1.equals(str2) statement returns true as a result, the operator ! makes it false.

So, in our cases, we check if the str1.equals(str2) throws true , and if yes, we use the operator, which proves that the variables are not the same.

public class JavaExample   public static void main(String[] args)    String str1 = "String A";  String str2 = "String B";   if (!str1.equals(str2))   System.out.println("str1 and str2 are not equal");  > else   System.out.println("str1 and str2 are equal");  >   >  > 
str1 and str2 are not equal 

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

Related Article - Java Operator

Copyright © 2023. All right reserved

Источник

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