Boxing and unboxing equality java - wrapper

Integer a = new Integer(3);
Integer b = 3;
System.out.println(a==b);
System.out.println(a.equals(b));
output:
false
true
can someone explain why this example is valid?

The == operator compares the equality of the objects. Since both objects have their own id/location in the heap memory, this check evaluates to false.
The .equals() method compares the actual values of both objects, in this case 3, which is true.

Related

Kotlin: What is the difference between = vs ==?

I'm trying to set a certain value, monsterHealth, to display 0 instead of a negative number. If I'm trying to reassign the value to show 0 instead of, say, -2, would I want to use = or ==?
Those are different operators:
= - assignment operator,
== and != - equality operators,
=== and !== - referential equality operators.
also
val - read only variable/property (it cannot be reassigned/changed),
var - mutable variable/property.
Kotlin Documentation - Keywords and operators

Use walrus operator when checking value

How can I check if a variable is equal to something, and set to a new variable in the child scope?
For example:
bar = 'foobar'
my_slice = bar[:3]
if my_slice == 'foo':
print(my_slice)
It seems like the new walrus operator here would be useful, but it's not immediately straightforward how you'd go about using it here
Walrus operators work here very well, we just need to understand exactly how they work.
if (my_slice := bar[3:]) == 'foo':
print(my_slice)
Walrus operators set a variable to the output of some expression. It's almost identical to the equal sign in function except it can be used inline.
So this expression:
(my_slice := bar[3:]) == 'foo'
Can be boiled down to (variable = expression) == value
So because the output of my_slice := bar[:3] is equal to bar[:3], the above is equivalent to
bar[3:] == 'foo'
Note: The parenthesis here are required, or else variable is going to equal the output of the comparison operation, i.e. True or False

Multiple Boolean statements VBA

I am having issues with some Boolean logic.
Essentially I want something to program in VBA a filter such that,
A = True AND (B = True OR B = False)
I just cannot seem to get the coding right to do this in VBA (I am using MS Access).
I have tried:
A = True AND B = True OR B = False
But this obviously fails (looks for either A,B = True OR B = False, essentially).
Am I missing something obvious here?
I chose to left out the actual code, but if requested I can post. My thinking is that I am missing some basic with the Boolean logic.
If A, B, and C are Boolean expressions, then comparing them to a Boolean literal (True, False) is redundant: the expression is already a Boolean expression.
That makes the original logic go like this:
If A And (B Or Not C) Then
The And operator has greater priority than the Or operator, so it gets evaluated first; if the (B Or Not C) part needs to be evaluated as a whole, then the parentheses are required, otherwise A And B takes precedence, making the expression (wrongly) evaluate as:
If (A And B) Or Not C ' redundant parentheses to illustrate operator precedence
Edit: I misread the OP, my brain put that C in there. If A And (B Or Not B) Then is redundant, as BigBen pointed out - the simplified logic would be If A Then. Still worth noting operator precedence and using parentheses when applicable and appropriate though.

Eiffel: Expanded classes with no fields are `=` or not?

In Eiffel, if comparing objects of an expanded type, the = operator compares them field by field, checking if the content of each field is identical in both objects.
Let's imagine two expanded classes with no features defined in them:
expanded class A
end
expanded class B
end
How can Eiffel tell them apart? Or can't it? Does it have to do with some field(s) inherited from ANY?
both_are_equal: BOOLEAN
local
a: expanded A
b: expanded B
do
Result := a = b
end
The field-by-field comparison is applied only when both objects are of the same type. If they are of different types the equality operator gives false. In other words, the equality operator = for expanded types is identical to the equality operator ~ with the semantics
type_of (a) = type_of (b) and then a.is_equal (b)
Therefore both_are_equal will give False.
The result will be the same if instead of a and b of expanded types there will be x and y of reference types attached to expanded objects - the comparison takes types of objects into account:
both_are_equal: BOOLEAN
local
a: expanded A
b: expanded B
x: ANY
y: ANY
do
x := a
y := b
Result := x = y -- Gives False
end
But if the reference expressions are attached to expanded objects of the same type, field-by-field comparison is used, not reference equality:
both_are_equal: BOOLEAN
local
a: expanded A
b: expanded A -- Note the type change
x: ANY
y: ANY
do
x := a
y := b
Result := x = y -- Gives True even though x and y are different objects
end
Some details on equality operators can be found in the Standard ECMA-367 (section 8.21) and in contracts of specific comparison features in the class ANY.

Using two equals signs in Visual Basic 2008

In code, why wouldn't this work?
intMax = intTopValue = 20
This is interpreted as intMax = (intTopValue = 20).
intTopValue = 20 will check whether intTopValue is equal to 20 and return true or false.
This boolean will then be assigned to intMax.
Most languages don't have this issue, since they use separate operators for assignment (= or :=) and equality (== or =).
By contrast, VB shares = for both operations. Therefore, when a = b is written as an expression, it always means equality.