what is testing for equality in javatesting for


What is Testing for Equality in java?

Testing for equality is a little trickier. You would expect to test if two numbers are equal through using the = sign. However the = sign has already been used as an assignment operator in which sets the value of a variable. Thus a new symbol is required to test for equality. Java borrows C's double equals sign, ==, for this reasons.

It's not uncommon for even experienced programmers to write == while they mean = or vice versa. Actually this is an extremely common cause of errors in C programs. Fortunately in Java, you are not allowed to use == and = in the same places. Thus the compiler can catch your mistake and make you fix it before you can run the program.

Therefore there is one way you can still get into trouble:

boolean b = true;
if (b = false) {
System.out.println("b is false");
}
To prevent this, a few programmers get in the habit of writing condition tests such as this:
boolean b = true;
if (false = b) {
System.out.println("b is false");
}

Because you can't assign to a literal, this causes a compiler error if you misuse the = sign while you mean to write ==.

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: what is testing for equality in javatesting for
Reference No:- TGS0284370

Expected delivery within 24 Hours