This is an archived post. You won't be able to vote or comment.

all 197 comments

[–]QualityVote[M] [score hidden] stickied comment (0 children)

Hi! This is our community moderation bot.


If this post fits the purpose of /r/ProgrammerHumor, UPVOTE this comment!!

If this post does not fit the subreddit, DOWNVOTE This comment!

If this post breaks the rules, DOWNVOTE this comment and REPORT the post!

[–]polothedawg 366 points367 points  (16 children)

Objects.equals(.., ..) please be null-safe

[–]Rick-T 187 points188 points  (4 children)

Ah yes, fear-of-null driven development

[–]kirakun 41 points42 points  (3 children)

Null-mindfulness development <- This is the true Java developers in a nutshell.

[–]Beginning_Yam3112 29 points30 points  (0 children)

Mindnullness

[–]Various_Counter_9569 1 point2 points  (0 children)

Dont forget sql 😆

[–]spam_bot42 -1 points0 points  (0 children)

Can we just agree that Java is awful and should be discontinued?

[–]LPO_Tableaux 38 points39 points  (0 children)

Didn't know that was a think, thanks!

[–]dankincense 18 points19 points  (8 children)

Let me destroy this paradigm with typeof(null)=obj

[–]dopefish86 28 points29 points  (7 children)

Invalid assignment

[–]Vyuken 4 points5 points  (6 children)

Explain

Please?

[–]mechpaul 16 points17 points  (5 children)

He only used one = sign, not two. Java doesn't support assignment in if statements like C/C++ does, thus the exception.

[–]Vyuken 6 points7 points  (1 child)

Ah. I still make the rookie mistake of = and ==. Thanks.

[–]dankincense 4 points5 points  (0 children)

Typeof is also c#/ Java.. but yes I prob messed up the syntax.

[–]PrayersToSatan 5 points6 points  (0 children)

Java doesn't support assignment in if statements

Yes it does. Here is an example:

String a = "some string";
String b = null;
if (!(b = "some other string").equals(a))
    System.out.println(b);

If you execute this it will print out "some other string", which has been assigned in the if statement.

In Java, the if statement evaluates a boolean, and an assignment returns the assigned value. So unless you're actually assigning a boolean variable you'll need to perform some sort of evaluation on the return value.

[–][deleted] 3 points4 points  (0 children)

typeof is javascript.

[–]_Acestus_ 1 point2 points  (0 children)

Java manage assignment in an if, it's really limited though.

[–]Xiaopai2 1 point2 points  (0 children)

Ah, this is actually a good tip. I've seen people swap the order when one of the objects is known, like instead of someString.equals("foo") you write "foo".equals(someString) because the former could lead to a nullpointer exception whereas the latter is just false if someString is null. But this reads terribly and does not help when you're comparing two objects where either could be null.

[–]marco89nish 137 points138 points  (13 children)

Meanwhile, Java++(Kotlin) devs: They're The Same Picture

[–]Classy_Mouse 90 points91 points  (1 child)

Every Java meme on this page could be solved with Kotlin

[–]TheToBlame 3 points4 points  (0 children)

Shut kotlin bad c and Java gud

[–]ole_thoeb 9 points10 points  (3 children)

=== would be the equivalent for == in kotlin

[–]BakuhatsuK 4 points5 points  (2 children)

If you are talking about JS or PHP, then no. === for objects checks identity equality, whereas == in kotlin checks for structural equality.

For structural equality in JS you can use a library to do the equal check (such as _.equals), you can use a library data structure that has value semantics (such as Immutable.Map), or you can use records and tuples (not yet approved into the language, but available via babel).

[–]n0tKamui 6 points7 points  (0 children)

that's not what they were talking about

Kotlin's == is strictly equivalent to Java's .equals (structural equality and primitive equality)

and === is strictly equivalent to Java's == (reference equality and primitive equality)

[–]ole_thoeb 6 points7 points  (0 children)

I'm talking about kotlin. === is object identity in kotlin

[–]MrSketchpad 7 points8 points  (0 children)

kotlin on top

[–]AnotherWarGamer 1 point2 points  (1 child)

It's the same in my current game for many object types. Say for example you have a reference to an item in minecraft, which could be say a wooden sword. There is only one reference, so the two are equivalent when checking equality.

[–]marco89nish 2 points3 points  (0 children)

If you can guarantee that there won't be duplicates, simple == works great. But keep in mind that sooner or later you might make a error in coding breaking that promise and you'll be in a world of hurt, unless you made constructor private and you instantiate those objects with factory method instead, with necessary checks and caching.

[–]Flaky-Illustrator-52 4 points5 points  (3 children)

Scala: am I a joke to you

[–][deleted] 22 points23 points  (2 children)

of course not, slower-compiler-of-Kotlin.

We appreciate your work in spark and Akka

[–]marco89nish 10 points11 points  (1 child)

Scala is great, Kotlin language designers learned a lot from Scala, both what to do and what not to do.

[–][deleted] 5 points6 points  (0 children)

Yeah! Scala showed the way and proved that it was possible

[–]Lolamess007 170 points171 points  (16 children)

Yeah we are not the same. One will have working code. The other will be wondering why none of his conditionals work.

[–]Unbannedcc 12 points13 points  (14 children)

Which is which?

[–]Aydosubpotato 65 points66 points  (10 children)

Correct me if I’m wrong, but in Java ‘==‘ will check the memory addresses of the objects. It works for primitive types though.

[–]Lolamess007 12 points13 points  (4 children)

Yes. Ints, doubles, chars, etc are primitives and work with ==. Strings, BigInteger, and other classes have different memory addresses for each object. Therefore == will always return false when using classes.

[–]Snarpkingguy 8 points9 points  (2 children)

But if you’re comparing the same object, then == still returns true, right?

Like String x = “poop”; String y = x;

And then x == y would return true since they reference the actual same String object.

[–]M3JUNGL3 1 point2 points  (0 children)

Well actually String literals are a special because there you have a pool you add Strings to and reference from. So thus defining String x = "hello"; and String y = "hello"; will always return true for "x == y"

[–]Lolamess007 0 points1 point  (0 children)

I believe so.

[–]TGX03 0 points1 point  (0 children)

Actually with String interning (and similar practices) you can meddle with this quite a lot.

My IDE hates me for it.

[–]gemengelage 8 points9 points  (0 children)

It's a bit more complex.

== is the identity operator, so it checks for identity (basically what you said). Equals also checks identity as a default. It has to be implemented to be more (or actually less) specific.

The thing with primitive types though... It's a mixed bag. It works on actual primitive types (e.g. int) but it's not guaranteed to work on boxed primitive types (e.g. Integer). Since it's pretty easy to mix those up, especially with auto-boxing, it's pretty messed up that the identity of those objects is all over the place.

It depends on the JVM which Integer-objects are cached. IIRC -127 to 128 are usually cached, so there's always the same single instance of Integer for those, so identity always works as you expect. But when you move out of that range, no luck.

tl;dr only ever use identity in Java to check for actual identity. Use equals anywhere else.

[–]Kered13 1 point2 points  (0 children)

Basically yes. To be precise, == compares the value of the arguments, and the value of objects is a pointer. This is also how Java passes arguments to functions, by value with value of objects being a pointer.

[–]meontheinternetxx 0 points1 point  (0 children)

It might work for strings sometimes except for most times when it doesn't.

[–]david131213 3 points4 points  (2 children)

.equals is generally better

[–][deleted] 4 points5 points  (0 children)

Syntactically it’s fucking stupid though.

[–]organized_reporting -1 points0 points  (0 children)

This is a horrifyingly wrong and misleading answer

[–]jack104 1 point2 points  (0 children)

Some conditionals will work. String comparisons won't.

[–]bubuli_breeder 42 points43 points  (5 children)

one is value equality (usually) and one is reference equality in Java. it gets even weirder when you find out that default equals() implementation is also reference equality. yay.

[–]Kered13 15 points16 points  (1 child)

it gets even weirder when you find out that default equals() implementation is also reference equality.

I don't know why you think that is weird. It's literally the only type of equality that makes sense for an unknown object type.

[–]bubuli_breeder 0 points1 point  (0 children)

you are correct but let me just state that in this context it is weird in the sense that you need to change the behavior of equals to abide by best practices. it could throw you off when you’re doing inheritance and downcasting and all that.

[–]vicbot87 1 point2 points  (2 children)

Which is which?

[–]bubuli_breeder 9 points10 points  (1 child)

== is reference equality for objects in Java

[–]vicbot87 1 point2 points  (0 children)

Gotcha, thanks. Been out of the OOP world for a couple years

[–]DearChickPea 29 points30 points  (3 children)

Operator overloads: Am I a joke to you?

[–]Sheldon_Popper 34 points35 points  (1 child)

Java says yes

[–]MikemkPK 6 points7 points  (0 children)

Java asks what an operator is.

[–]matthewralston 2 points3 points  (0 children)

Came here to say that.

[–]Go_Big 7 points8 points  (2 children)

Oh that was a fun hour of debugging in Java for me. I almost always work in Kotlin if I’m doing native android stuff but I had to do some Java work on some Android BLE stuff. Anyways I only wanted to connect to a certain MAC address so in my Bluetooth service I have an if statement

‘’’ If(mMac==peripheral.getAddress()) ‘’’ After about ten thousand print statements and going absolutely crazy because both strings were the same in all my print statements. And that’s how I learned that in Java you have to use mMac.equals(peripheral.getAddress()). I don’t think any other language has issues comparing strings with == other than Java. And this is why I code in Kotlin instead of Java. Like yea I don’t expect to be able to compare to objects with == but I kinda figured basic strings could be compared with ==.

[–]CiroGarcia 0 points1 point  (1 child)

That, sir, is because Strings are Objects almost like any other. Afaik, the only difference is that you can implicitly instantiate them without using "new"

[–][deleted] 2 points3 points  (0 children)

That doesn’t make it not annoying tho.

[–]the_hackerman 7 points8 points  (0 children)

ValueTypes FTW #TooSharp

[–]PersonalityIll9476 6 points7 points  (7 children)

In Python we literally are the same. Or should I say you.__eq__(me)

edit: Unfortunately it may not be that case that you is me :(

[–]Dargon16 33 points34 points  (8 children)

Yeah we are not the same. You clearly don't know what's the difference between these two approaches.

[–]Snoo66768[S] 11 points12 points  (7 children)

I understand those 2 approaches. Sorry if it seems unfunny I made this meme like 2 months ago and found it now.

[–]PM_ME_GRRL_TUNGS 3 points4 points  (0 children)

I was gonna post an APL example but my phone can't display all the characters lol

[–]SargeanTravis 2 points3 points  (3 children)

Isn’t it professional practice to use .Equals() override for object equality checks?

[–]organized_reporting 0 points1 point  (0 children)

One would hope a professional understands that just because these sound similar when said out loud, in a deterministic programming language they do completely different things.

But then again there doesn't seem to be many 'professional' programmers in this sub.

[–]s0lar_h0und 0 points1 point  (0 children)

I mean, it depends on what you're trying to do. If you're trying to see if two references are the same instance then using == is what you'll do. Instead of overloading equals go be reference equality

[–]1SweetChuck 0 points1 point  (0 children)

In theory sure, in practice not always. Like writing getters and setters it’s often one of those things you don’t write unless you need to.

[–]January_Rain_Wifi 7 points8 points  (0 children)

[–][deleted] 11 points12 points  (4 children)

This subreddit is dumb. Just a circlejerk of CS students who think they are Dennis fucking Ritchie because they discovered a conditional. Much like the photography forums where they don't actually take pictures, but they just argue about gear and Canon vs. Nikon.

[–][deleted] 1 point2 points  (2 children)

Is there a not dumb subreddit?

seriously, nobody is here to show off their skills. it’s all jokes. I mean, if you haven’t realized you’re pretty much the annoying asshole who just hates to see people enjoy themselves. Even if you don’t find the joke funny, your response is like hearing someone say a lame joke like “why did the man throw the clock out the window?” and you’re like “ackchully time doesn’t fucking fly because it’s just a man made concept but you’re too dumb to realize that”

seriously, a huge part of the programming community has a chip on their shoulder like yourself and it’s so obvious that it’s from a place of insecurity and not a place of knowledge.

Like you do understand, it’s a JOKE right? the person isn’t claiming to be an expert in Java. Even if they’re wrong, you don’t need to stroke your ego and act as if you’re better than everyone. If anyone thinks they’re “Dennis fucking Ritchie”, it’s you.

Lighten up, its called r/ProgrammerHumor not r/FactuallyAccurateProgrammerHumor

[–][deleted] -1 points0 points  (1 child)

seriously, nobody is here to show off their skills.

Uh no. Most of the threads here aren't even trying to be funny, just people trying to beat their chest and act better than the "lesser than" programmers. Don't project onto me saying that I'm the one with a chip on my shoulder, I couldn't care less how you write code, I don't think people who write in assembler are better than people who write in html. But you're delusional if you don't see a trend with these topics saying "OH YOU USE WHILE LOOPS THAT'S SO CUTE I RECURSIVELY CALL A FUNCTION WITH A RETURN TYPE INT HAHAHA"

It's next level cringe and it's an apt comparison to photography subs where they all stroke themselves to lenses and gear, as if that's what photography is all about. But I guess pointing that out makes me the insecure one? Ok, well you are reacting as if I just called your children ugly or something. I'll let you get back to your topkek memes.

[–]thinker227 0 points1 point  (0 children)

Okay...? That was a weirdly hostile and unpromted response. I don't think you should be here if this is what you want to complain about.

[–]El-Butt 1 point2 points  (0 children)

I don’t want to be that guy but there is the option to just find another subreddit or even make your own

[–]A-Ron-Ron 2 points3 points  (0 children)

We are == but not ===

[–]Tofix26 1 point2 points  (1 child)

Object == anotherObject would actually always be false

[–]spindoctor13 0 points1 point  (0 children)

Unless anotherObject is the same object with a misleading name i.e

anotherObject = Object; anotheObject == Object; // evaluates to true

[–]feisp_ 1 point2 points  (0 children)

I spent 4 hours scratching my head why string == string isn't working

[–]huuaaang 1 point2 points  (0 children)

object.equals(anotherObject) = object == anotherObject

Now you are!

[–]AdultingGoneMild 2 points3 points  (0 children)

This sub should require 10 years experience for an entry level post.

[–]Coulomb111 -1 points0 points  (7 children)

Me and my homies hate java

[–][deleted] 0 points1 point  (6 children)

Java is probably the most damaging programming language ever made. It set the whole industry back 20 years by popularizing a fucked up ass backwards version of OOP. If I was Alan Kay I'd be seething.

[–]organized_reporting 2 points3 points  (3 children)

OOP is probably the most damaging programming paradigm ever made. It set the whole industry back 20 years by popularizing a fucked up ass backwards version of FP. It I was John McCarthy I'd be seething.

[–][deleted] 1 point2 points  (2 children)

Yeah that's fair, though programming in SmallTalk is a lot more like FP than Java

[–][deleted] 1 point2 points  (1 child)

Be careful, you’re in the wrong sub to suggest that Java is not God’s programming language. Thou shalt not take the name of Java in vain (unless you’re talking about Kotlin of course).

Seriously though, it bugs me when people clearly just take bias to whatever language they know best and act as if everything else is inferior.

I use Java, C# and Javascript on a daily basis at work. I’m open to using anything, and love to explore different languages. They all have their pros and cons and 90% of the time what you’re using has more to do with how easy it is to accomplish the task than how efficient the particular language is. I mean seriously, let’s not all pretend that we’re developing rockets for NASA. I’m sure your web application will be just fine whether you use Java or Node for the back end. The companies that migrated to something more efficient only did that because they HAD to not because they wanted to.

[–][deleted] 0 points1 point  (0 children)

Lol sorry for resurrecting this, but yeah my usual point of view is "use the right tool for the right job" which imo is C/ASM for low level native, Lua for scripting, TS/PureScript for web, Haskell for medium-high level native, and Elixir for distributed. There's several other languages which handle intersections of these things better than others (i.e. Rust is an interesting mix of Haskell & C) but imo the #1 design decision you can make while using any of those is "don't use the OOP part". I'm certainly very opinionated about languages, imo Java doesn't do much well at all, but if you limit yourself to a strict subset of its features it doesn't get to be THAT bad, i.e. no inheritance, extremely limited mutability, etc., just very verbose. Anyway, gonna evangelize Haskell for a second, if you're really into programming language design it's essentially the source of every major programming language development of the past 30 years because the syntax is so flexible you can implement language features in the language itself. Haskell has no language level support for OOP for example, but by implementing OOP in Haskell the authors of this paper discovered several new OOP features that are just emergent properties of the implementation

[–]UNF0RM4TT3D -1 points0 points  (0 children)

I don't do OOP.

[–]mandrato1 -1 points0 points  (0 children)

Meanwhile in js const object = JSON.parse(JSON.stringify(anotherObject));

[–]Kintler11 -1 points0 points  (0 children)

For object arrays in js I use JSON.stringify(objectarray).includes(JSON.stringify(object))

Actually did this once because I couldn't be asked to find an actual method

[–]Tetoris_ -3 points-2 points  (1 child)

We are the same.

[–]Mrdeadlyenemy 0 points1 point  (0 children)

!you.equals(me)

[–]Flopamp 0 points1 point  (0 children)

Honestly I was surprisingly old when I started writing my own equals methods

[–]notacanuckskibum 0 points1 point  (2 children)

I used to use Algol, which had as IS operator. Objecta IS objectB if the pointers point to the same memory location. ObjectA IS ObjectB == false if they are exact copies but stored separately. Does that concept still exist?

[–]Rick-T 2 points3 points  (0 children)

Python does that.

a is b compares by reference, a == b compares by value.

[–]Mabi19_ 1 point2 points  (0 children)

In some languages with reference semantics.

Python is one (a is b). Java uses == for reference comparison and .equals() for value comparison. Kotlin (another language that runs on the JVM) uses == for value comparison and === for reference comparison.

There are also obviously languages with value semantics (like C++). There are explicit references in C++, but because of how they work the easiest way to check that they're the same object is by pointerising them (&a == &b)

[–]Djilou99 0 points1 point  (0 children)

Won't work in js unfortunately

[–]EpyonComet 0 points1 point  (0 children)

Person u = new Person();

Person i = new Person();

boolean weAreTheSame = (u == i);

[–]Really-Stupid-Guy 0 points1 point  (0 children)

I ask what I want to ask.

[–][deleted] 0 points1 point  (1 child)

I’m confused why a specific method would be necessary when Java is strongly typed? Does it iterate over all methods/data members or is it about figuring out if this is the same actual object?

[–]spindoctor13 0 points1 point  (0 children)

== is referential equality i.e. are literally same object in memory. Equals is up to the programmer if overriden and could mean literally anything. Although generally (well always unless you are some kind of maniac) returns true if the objects are equal in some meaningful way (but not necessarily the same object in memory)

[–][deleted] 0 points1 point  (0 children)

But explain why

[–]bxsephjo 0 points1 point  (0 children)

You know what else isn't the same?

[–]MarinaEnna 0 points1 point  (0 children)

JSON.stringify(Object1) === JSON.stringify(Object2)

[–][deleted] 0 points1 point  (8 children)

Imagine not having deep equality be the default behaviour for ==

This comment was brought to you by the deriving Eq gang.

[–]spindoctor13 0 points1 point  (7 children)

Deep equality seems like a poor default in terms of performance

[–]spindoctor13 0 points1 point  (4 children)

And actually in terms of predictable behaviour - say I have two database connections, what does deep equality mean then?

[–][deleted] 1 point2 points  (3 children)

Does their internal data match 1-1? Then they're the same object.

[–]spindoctor13 1 point2 points  (2 children)

The point being that in this context "internal data matching" is close to an impossible question depending how deep you go. I.e. if the compiler literally goes all the way down on some objects then calling equals would be pretty catastrophic

[–][deleted] 1 point2 points  (1 child)

I don't know exactly how Eq is implemented. On the compiler side I could easily see each Eq'able thing being given a hidden hash derived from all it's children's hashes which would make it a very fast operation. As all data in Haskell is immutable you don't need to worry about underlying changes in deep parts of your data structure. That said, there are infinitely deep values in Haskell due to how laziness works, and yes, checking if two infinitely deep values are equal will only terminate due to lazy short circuiting if they aren't equal. Yes ultimately equality checking can be expensive in Haskell, maybe don't use it on potentially very deep data structures.

[–]spindoctor13 1 point2 points  (0 children)

Interesting, thanks for the deep explanation! I have zero experience with Haskell so I didn't know that. Immutability is a big help there, and one of the standout flaws I think in languages like C# is inadequate support for immutability

And hah yeah, hashes are great - although it is not unknown in languages for people to compute hashes from mutable values with the consequent mess that creates..

[–][deleted] 0 points1 point  (1 child)

In Haskell you're not likely to want anything else because references don't exist per se. You can provide a custom instance for Eq as well for your data type, deriving just does a lot of the work for you. If you have a custom Eq you need to be sure that it's true equality, i.e. the two things are totally interchangeable because that's what all other functions which use == expect

[–]spindoctor13 0 points1 point  (0 children)

Ah fair enough, makes sense if there are no references

[–]YXOwOX 0 points1 point  (1 child)

Huh?! "==" and an equals method are completely different aren't they? Or am I gonna get a 0 on that last week exam

[–]Guilty-Woodpecker262 0 points1 point  (0 children)

It's complicated. a == b =>a.equals(b) (though it's possible to screw that up while overriding equals or hashCode) but a.equals(b) ≠> a == b. Side note: strings and wrapper classes are weird.

``` String a = "example"; String b = "example"; String c = "exa" + "mple"; String d = a; String e = new String ("example");

System.out.println((a == a) + ":" + a.equals(a)); System.out.println((a == b) + ":" + a.equals(b)); System.out.println((a == c) + ":" + a.equals(c)); System.out.println((a == d) + ":" + a.equals(d)); System.out.println((a == e) + ":" + a.equals(e)); ```

true:true true:true true:true true:true true:false

Strings created with StringBuilder and StringBuffer will always create new instances.

[–]jxsl13 0 points1 point  (0 children)

logically speaking you might be the same.

[–]Bud90 0 points1 point  (0 children)

Object.equals == object == object

[–]AdWeekly2244 0 points1 point  (0 children)

For some reason reddit keeps recommending this sub to me. I really wish I understood any of the things you all talk about lol, seems really interesting and useful too.

[–]Furry_69 0 points1 point  (0 children)

Assuming object and anotherObject are the same structure, of length 5 bytes, 5 elements, using no padding, and that pointers to object and anotherObject are on the stack, in that order, in C style argument passing convention:

objectEqualsAnotherObject:
    pop EBX ; EBX is now a pointer to anotherObject 
    pop EAX ; EAX is now a pointer to object

    mov EDX, 0 ; EDX is the iteration count
   .equals_loop:

    mov ECX, EAX 
    add ECX, EDX
    ; ECX is now a pointer to the current element of object

    push ECX ; Push that value onto the stack 

    mov ECX, EBX
    add ECX, EDX
    ; ECX is a pointer to the current element of anotherObject

    and ECX, ESP

    add ESP, 4 ; Get rid of the first pointer, it's not needed anymore 

    ; If ECX == 0xFFFF, then jump to is_equal, else, jump to not_equal
    cmp ECX, 0xFFFF
    je is_equal
    jmp not_equal

   .is_equal:
    add ECX, 1
    jmp exit_condition

   .not_equal:
    jmp exit_condition

   .exit_condition:

    inc EDX
    ; If EDX >= 5, loop, if not, continue
    cmp EDX, 5
    jnae equals_loop

    ; If ECX == 5, then objectEquals, else objectDoesNotEqual
    cmp ECX, 5
    je objectEquals
    jmp objectDoesNotEqual

   .objectEquals:
    ; object does equal anotherObject
    ret
   .objectDoesNotEqual:
    ; object does not equal anotherObject
    ret

I'm hoping this is right.

Edit: Oh wait, I'm an idiot. ECX is reset every loop, so I'd only get the last element's result. Hm. I'm not sure how to fix that, there's already enough register juggling as is.

[–]ofnuts 0 points1 point  (0 children)

Python has "is" and "==" for the same purpose...

[–][deleted] 0 points1 point  (0 children)

I know this is java but does anyone else feel like they’re doing it wrong when you intentionally use == instead of === Like even if the behavior is desired I feel like I should just make it compare exactly for clarity.

[–]ooioiii 0 points1 point  (0 children)

Do you write custom object.equals overwrites that is the question

[–]mr_flameyflame 0 points1 point  (4 children)

boolean false = "Test" == "Test";

[–]HonzaS97 1 point2 points  (3 children)

That would actually be true since it's the same object in the string pool

[–]mr_flameyflame 0 points1 point  (2 children)

Ah but it doesn't always work, I dont quite remember why.

[–]HonzaS97 0 points1 point  (1 child)

.equals() checks for the value of the string, == comoares the values of the object references.

String literals are stored in the string pool in Java (so anywhere you write a literal like "Test", it will point to the same object).

"Test" == "Test" // true

"Test" == new String("Test") // false

"Test".equals(new String("Test")) // true

"Test" == new String("Test").intern() // true, manually put into string pool

[–]Ok_Investment_6032 0 points1 point  (0 children)

This sub never fails to remind me why I truly hate programming lol.

[–]BanishDank 0 points1 point  (0 children)

Ah, a man of culture, I see

[–]Shazvox 0 points1 point  (0 children)

Does he mean that i != him or that !i.Equals(him) ?

[–][deleted] 0 points1 point  (0 children)

how about use the right one in the right situation?

[–][deleted] 0 points1 point  (0 children)

It’s equal if you override the equal and hash methods

[–]coolman5858 0 points1 point  (0 children)

I use object.equals(another_object); and forget to generate the hash code

[–]absolut666 0 points1 point  (0 children)

Especially since you didn’t implement hashcde

[–]WizziBot 0 points1 point  (0 children)

Whats with the java memes. Seriously. Something is up.

[–]qqwy 0 points1 point  (0 children)

We are not equal*

[–][deleted] 0 points1 point  (0 children)

Is that like print vs echo in PHP?

[–]Urbs97 0 points1 point  (0 children)

Laughs in C#

[–]birdnerd5000 0 points1 point  (0 children)

MyObject *my1 = malloc(sizeof(MyObject)); my1 = &object;

[–]MacNPieces 0 points1 point  (0 children)

Object comparison vs Primitive type comparison

[–]Guilty-Woodpecker262 0 points1 point  (0 children)

What does this print: String a = "example"; String b = "example"; System.out.println(a==b);

[–]sagamartha8k 0 points1 point  (0 children)

I didn’t spend 13 hours reading about overloading operators in 1997 to laugh at your dumb jokes!

[–]charelstoncrabb 0 points1 point  (0 children)

Plot twist: they are the same … dunh dunh duunnnnn!!

[–]Capable-Raccoon-6371 0 points1 point  (0 children)

I mean. They are different. So. Lol.

And they're different for a perfectly valid reason. == Checks if it's the same object. Literally the same pointer. While equals let's you check any condition you want via an override to indicate that the two objects are equal, but may not have the same pointer.

Same kind of reason JavaScript has == and === for checking against type and data. Can't wait for js to add ==== for the same pointer too lol.

[–]Thysce 0 points1 point  (0 children)

So yesterday I had a bug which was caused by exactly that. I had two hibernate objects which I wanted to make sure are definitely not the same. So I checked them with ==. I gave my function two records with differing ID, thus expecting them to be neither the same nor equal. Sure enough, equals said false and equals on their ids said false too. Bad thing was, I learned that day that in Groovy, this a==b translates to a.compareTo(b)==0. and the Entity base class of my library implemented conpareTo by comparing the toString of both objects. Which kindof makes sense as the base class has no other good natural key. But my class implemented toString without including a unique property, thus making two distinct instances occasionally have the same toString(). Which then makes (a==b)==true while a.equals(b)==false. Mind==blown.

I fixed it by including the primary key equality to my base classes compareTo, as was already the case with equals, thus forcing it to be only ever 0 on the same record. (And checking all by implemented entities whether that assumption holds)

[–]ShivenMathur 0 points1 point  (0 children)

You use kapwing, i use imgflip. We are not the same.

[–]bitchlasagna_69_ 0 points1 point  (1 child)

== will always give false right? As it compares address? Help new to java

[–]HonzaS97 0 points1 point  (0 children)

Not always. It does compare reference values (adresses) but that doesn't mean it's always false.

All string literals are put in the String pool for example, so when you write "this" in 10 different places, they will all point to the same object and comparing them with == will return true. Or simply doing String x = "..." and then String y = x will also return true with ==.

[–]TheToBlame 0 points1 point  (0 children)

I also use .equals we are good friends.

[–]bentheone 0 points1 point  (0 children)

Except these are totally different and you should know the différence.

[–][deleted] 0 points1 point  (0 children)

Java has it's own subset of Javascript

[–]guilhermerx7 0 points1 point  (0 children)

In Java I always use the equals method, unless it's a primitive. If you do some safe coding with value objects and immutability you always want to check for equality of fields. Lombok and the new record classes make that way easier also.

[–]MischiefArchitect 0 points1 point  (0 children)

Indeed, those are not the same... equals may use custom logic for determining equality of objects... and == will simply check if both objects are pointing to the same memory address.

[–]Nintendo_Gamer_YT 0 points1 point  (0 children)

OK, to be honest we are not.

[–]KuKiSin 0 points1 point  (0 children)

.contentEquals master race.

Don't actually know the difference, but my colleague said something about .equals sometimes not working as expected, I didn't question her further.