If statement help

if ((isPrime == true) && (answerFlag = 1) && (computeranswer = 'Y'))

The tldr is (assuming answerFlag is an int and computeanswer is a String and not a char) use

if (isPrime && answerFlag == 1 && "Y".equals(computeanswer))

Be sure to use == for answerFlag ("double equals")

The longer answer...

Some of the posters below mention using ==, but there's more to it than that.

Java has 2 types of variables. Primitive types and reference types. Primitive types are:

boolean, char, byte, short, int, long, float, and double

Reference types are objects and arrays. Like String, Object[], and int[]

Objects and primitives (lets ignore arrays) have different equality checking methods.

But before we get there, let's take one more diversion and remember what a variable is. A variable is a named pointer to a location in your computer's memory. Like a house address on a street. For primitive types, the amount of data allocated at the memory address is associated to the type:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

For example, a byte is 8 bits. An int is 32bits. When you assign

byte b = 1;

You're literally assigning bits 0000 0001 to a location in memory, let's say it's location 7. So "b" is actually a pointer to location 7, and the value at 7 is 0000 0001

An object is a bit different. Like the name reference type implies... an object variable is a pointer to a loan on memory (like a primitive), but the value there is actually another pointer to data located somewhere else.

So in this code, s is pointing to a location in memory, let's say 8, and the value at 8 is another location in memory, let's say 37... and at 37 is "STRING" (and some other stuff that makes up the object): String s = "STRING";

Now 7 and 8 are in a part of memory called the stack. And 37 is in a part of memory called the heap. This article has a good picture:

https://www.scientecheasy.com/2018/06/memory-allocation-primitive-nonprimitive.html?m=1

When you write the double equals operator, ==, you're asking the jvm to verify that the contents of 2 locations in stack memory are equal. (This is VERY fast and is I've reason why comparing primitives in Java can be almost as fast as C... but that's another story). So let's keep doing in our example...

byte b = 1;
String s = "s";

byte b2 = 1;
String s2 = "s";

So here's what you get in memory:

Variable Stack location Stack value Heap location Heap value
b 7 0000 0001
s 8 heap@37 37 <String "s" id=1234>
b2 9 0000 0001
s2 10 heap@50876 50876 <String "s" id=6789>

So now here's what == is doing, b == b2 is asking "are the values at stack locations 7 and 9 the same?", And that is true.

s==s2 is asking "are the values at stack locations 8 and 10 the same?", And that is false.

This is when we need to use s.equals(s2) for the object comparison.

Now what were saying to the jvm with s.equals is "dereference the pointer at stack location 8, and call the equals method on that object with parameter 1 being the object deferenced at stack location 10. Now we can use our Java implementation of equals on our objects to define equality. In this case String will compare itself as a char array, but let's skip those details.... And for s.equals(s2) you get true...

Lastly, a style nit. You don't need to compare boolean types against a boolean literal for equality. The following checks are equivalent, but the first is better:

boolean bool = true;
if (bool) ...
if (bool == true) ... // don't do this

Good luck

/r/javahelp Thread