Thursday 27 May 2021

Java Comparison for Beginners

So, we all know about boxing and unboxing in Java, and the fact that we have "int" which is a primitive, and we have "Integer" which is an Object.

So, when comparing "int", we can use ==. When comparing "Integer" it's better to use .equals().

It may seem like it works, but this is only for small values that are cached in the JVM. Do not rely on that!

See the following example:

So, what happens when using different comparisons? let's explain the test below:

It works fine! Exactly as we would expect.

In short, according to the JLS1, first unboxing takes place, in order to be able to compare two primitives.

Then a widening primitive conversion2 takes place, in this case the int will be converted up to type long.

Now the comparison can take place.

References

[1] JLS Java 16 - 15.20. Relational Operators
https://docs.oracle.com/javase/specs/jls/se16/html/jls-15.html#jls-15.20
[2] JLS Java 16 - 5.6. Numeric Contexts
https://docs.oracle.com/javase/specs/jls/se16/html/jls-5.html#jls-5.6

Thursday 20 May 2021

Java Inheritance for Beginners

Just a little exercise, to get things in perspective.

Besides, I thought I was being clever, but made a rookie mistake and it just didn't work1.

Something similar can be found in Java Puzzlers2.

During compile time a decision is made which method is to be called (§15.12)3. During compile time it is as yet unknown which type/subtype it will be. The compiler tries to be as specific as possible, but in this case, it will just be Animal in the second testcase.

References

[1] Stackoverflow - Overloaded method selection based on the parameter's real type
https://stackoverflow.com/questions/1572322/overloaded-method-selection-based-on-the-parameters-real-type
[2] Java Puzzlers - Traps, Pitfalls, and Corner Cases - By Joshua Bloch and Neal Gafter
http://www.javapuzzlers.com/
[3] JLS 16 - 15.12. Method Invocation Expressions
https://docs.oracle.com/javase/specs/jls/se16/html/jls-15.html#jls-15.12

Thursday 13 May 2021

Method determination ignored variable arity and implicit auto boxing/unboxing

Recently encountered a tweet by Simon Ritter. With a response by Brian Goetz.

Simon ritter:

TIL that determining method applicability initially ignores variable arity and implicit auto boxing/unboxing.

This code prints "long, long", which surprised me.

See section 15.12.2. of the Java Language Spec.

Brian Goetz:

The three-phase overload selection algorithm was added at the time autoboxing and varargs was added; if we had not done so, adding autoboxing/varargs would have been source-incompatible as it would have changed existing overload selection decisions.

The unfortunate consequences of trying to be backwards compatible.

References

[1] Java Language Spec 11 - 15.12. Method Invocation Expressions
https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.12

Thursday 6 May 2021

USB-C Hubs for MacBooks - Review

So I had the opportunity to checkout no less than three USB-C HUBS for my MacBook Pro.

Belkin Thunderbolt 3 Dock-Pro

An expensive beast at 259 euros, but works like a charm.

Also comes with an Ethernet port, can charge the MacBook Pro at the same time (without using the white charging block I got with my MacBook).

The thunderbolt cable provided with the Belkin is long and very good.

The thunderbolt cable provided with the Belkin for connecting between the MacBook Pro to the Belkin is the one you should use. Other cables likely won't work.

May take some time getting ready when first turned on.

First time use may have issues with the Ethernet, but the company has a fix for that on their website1.

Satechi USB-C MultiPort V2

Not that expensive at 80 euros.

The USB-C cable for conecting to the MacBook Pro is really short.

It requires the white charging block.

However, I'm continually having problems with external monitors. The MacBook reports that I'm still using the monitor from work (with 1920x1080) when working at home (with 2560x1440).

Quite likely, this problem is because I'm waking up the macBook from hibernate, and it doesn't query the external monitor when it wakes up?

I've posted some EDID settings below.

The only thing that seemed to work was to restart my Mac, and immediately close the lid. Somehow that causes the MacBook to reevaluate what external monitors it has, and get the proper EDID settings.

Vitamo 8-in-1 USB-C Hub Adapter

At 37 euros by far the cheapest, and it shows.

It requires the white charging block.

It suffers from the same problems with External Monitors as the Satechi.

The Ethernet port, while initially working perfectly, seems to have suffered from some mishaps as I've had no connection twice in a row now.

The HDMI port, whilst working fine originally, now has a tendency to cause the external monitor to lose the connection occasionally.

Remove the cable and reinsterting it seems to fix the problem.

Conclusion

The Belkin is fantastic, but expensive.

The Satechi seems fine.

The Vitamo is cheap, but you get what you pay for.

I do find these simple USB-C hub to be very low-quality plastic thingies, and they tend to "dangle" next to the MacBook.

I read on the Internet (make of that what you will) that all these lower-cost USB-C hubs tend to use the same cheap chipset.

EDID

Problems with external monitors seem to be effecting quite a large number of people when dealing with MacBooks.

I can apparently export the EDID settings using an app called SwitchResX.

The EDID settings of the Dell 1424H from work:

https://gist.github.com/maartenl/e39efad1899d80371306311b24c2573e

The EDID settings of the HP 27G from home:

https://gist.github.com/maartenl/4b317b337deb7b26cbbf27e70a300d00

Checking things

In order to check things, like, what resolution is my Mac running, you can use Spotlight Search for look for "About This Mac". Thie app can show the used Displays in the tab Displays (second tab).

Of course you can always check your monitor settings themselves.

Some magic happens in the "Displays" configuration settings on the MacBook, when you press "Command" function. When pressed the "Gather windows" button changes into "Detect Displays". When pressed and clicking on "Scaled" shows many more monitor resolutions than normal.

References

[1] Belkin - Troubleshooting ethernet connectivity on the Belkin F4U095
https://www.belkin.com/us/support-article?articleNum=304840
StackExchange - How do I get my monitor to run at the correct resolution reliably?
https://apple.stackexchange.com/questions/374982/how-do-i-get-my-monitor-to-run-at-the-correct-resolution-reliably
StackExchange - Incorrect external monitor resolution detected, true resolution not available
https://apple.stackexchange.com/questions/344608/incorrect-external-monitor-resolution-detected-true-resolution-not-available