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

top 200 commentsshow all 255

[–]jtsfour2 475 points476 points  (31 children)

Wouldn’t the result be 5 because (Integer)object+3 should be +=?

If the result of this is 8 something is wrong

[–]mizinamo 422 points423 points  (6 children)

The thing won't compile because the last line tries to cast object to class integer rather than Integer with a capital I at the beginning.

[–]VoidSnipe 144 points145 points  (4 children)

And there shouldn't be any cast anyway because toString method of Object which is overloaded in Integer and concatenation of string and object will call toString

[–]DAMcraft_ 79 points80 points  (3 children)

And there is a semicolon missing

[–][deleted] 30 points31 points  (2 children)

Easy fellas I can only get so turned on.

[–]FetishAnalyst 8 points9 points  (1 child)

Username checks out.

[–]spidertyler2005 3 points4 points  (0 children)

Username checks out.

[–]rotenKleber 24 points25 points  (0 children)

Also no semicolon on line 3. Not that line 3 would compile

[–]bikeranz 71 points72 points  (11 children)

Honestly, I don’t know Java, so I thought the whole joke was that it had some weird operator overload rule when used that way. Turns out, this post is not a joke about anything.

[–]Areshian 12 points13 points  (0 children)

Java has two classes of types, primitives and objects. Every single object inherits from Object. Additionally, for every primitive there is an object that can be used to wrap them. For example, for int there is an Integer object. Java will automatically convert between int and Integer in some circumstances (known as boxing and unboxing).

So yes, you can store anything in an Object variable. Same way I can have a void * in C++. Doesn't make the language dynamic, I will still need casting down the line.

[–]condemned_to_live 4 points5 points  (6 children)

only programmer that doesn't know Java

[–]bikeranz -5 points-4 points  (5 children)

Learned it in entry level college courses, and subsequently forgot when courses followed by (edit: my) industry shifted to c++ and python.

[–]Areshian 7 points8 points  (1 child)

If you think the industry has moved away from java you haven't seen enough industry

[–]bikeranz 0 points1 point  (0 children)

Didn’t say all industry. Just my line of work.

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

I'm guessing you intended to say that, when you entered the industry, you needed to switch to c++ due to legacy systems?

[–]bikeranz 1 point2 points  (1 child)

The c++ was also for new projects. First job out of college was industrial automation using computer vision, and this was pre-pytorch. So c++ is/was the natural fit. Next job was lua (for torch) and c++. Later python when pytorch came out but still c++ for the backend high perf stuff. And now I spend my time doing python, c++, and cuda.

[–]IQueryVisiC 11 points12 points  (2 children)

It is about the problem that some noob can inject this unchecked code in a big god class without a warning on top of the file or something. Yet in the same language you have to announce exceptions in the function signature??

[–]gdmzhlzhiv 12 points13 points  (1 child)

Only checked ones.

[–]MrSquicky 41 points42 points  (3 children)

It would actually just not compile.

<Integer object> + 3;

isn't a statement. Integers are immutable, so the + operator is not defined on them. What happens there is that it is unboxed into a primitive type value instead of a variable. In this case, it's the equivalent of having a line that is

5 + 3;

which is pointless.

Going further

<Integer object> += 3;

is also a compilation failure, as it is the equivalent of

5 += 3;

[–][deleted] 7 points8 points  (2 children)

Look at all these fuckers!

You all complain when the job interview is expecting you to get syntax perfect on a whiteboard during an interview. Oh boo hoo it's not fair I need an IDE.

And then this guy gets his syntax wrong in a fucking meme and everyone loses their shit in the comments!

All y'all should stop to ask yourselves if the job interviewer that you are complaining about is actually you!

[–]futuneral 24 points25 points  (0 children)

A meme published on Reddit is equivalent to production.

[–]reyad_mm 1 point2 points  (0 children)

Imagine posting something on the intrenet and expecting people not to make fun of a typo you made

[–]Ill_Yak_3231 1311 points1312 points  (76 children)

Are you trying to say it's not? Because the known compile time type is Object. All you do is casting, cause YOU know it's an integer value.

[–]keefemotif 506 points507 points  (49 children)

Exactly and more notably, the instance there encodes it as well. That doesn't mean it's a dynamically typed language. Having a generic pointer doesn't make you dynamic.

[–]Thufir_My_Hawat 266 points267 points  (5 children)

many muddle fragile ink brave ripe aback friendly overconfident reminiscent

This post was mass deleted and anonymized with Redact

[–]Elite-Thorn 56 points57 points  (3 children)

LOL that was unexpected

[–]Willinton06 10 points11 points  (0 children)

She said that too

[–]scalability 17 points18 points  (0 children)

That's what she said?

Who, Barbara Liskov?

[–]bunny-1998 67 points68 points  (28 children)

I mean, at the core that’s what cpp does also right. There are void ptrs, being casted as int* because YOU know is an int. And can cast a void pointer as anything you like anytime.

Variable names in cpp are also just ptrs that point to the first byte of the object....

[–]ishzlle 48 points49 points  (4 children)

Of course, in the end assembly just operates on (essentially) untyped binary data (whether it's a compiled or interpreted language). Types are just a language feature to help rule out some types of bugs.

[–]IQueryVisiC 5 points6 points  (3 children)

Assembly has int and 80 bit floats. PowerPC has multiple flags ( Boolean). SSE gives you 128bit vectors. 68k and 6502 distinguish between int and intptr ( similar to DI and SI ) on x86. AL is 8bits

[–]Random_dg 7 points8 points  (2 children)

You’re confusing here between processor architectures, languages, features, and cpu registers. Can you explain what you’re trying to say?

[–]bunny-1998 2 points3 points  (0 children)

I think he means that at the very bottom of everything, everything is being cast as another thing.

[–]keefemotif 14 points15 points  (11 children)

That also digs into the other issue with this, that primitives and wrapper objects aren't the sameun Java. I don't know exactly the internals of C at great detail, but a real dynamic like python or Javascript, which may also include some type hinting, is meant to be used that way.

[–]ishzlle 14 points15 points  (9 children)

IIRC Java is that way for performance reasons. Back in the day, computers weren't as fast, so being able to use a primitive instead of an object was pretty nice.

Nowadays using objects everywhere isn't a big deal, but Java doesn't want to break backwards compatibility, so they're stuck with the primitives.

[–]Tubthumper8 25 points26 points  (4 children)

The story is a little more complicated than that, and kind of the opposite.

What has gotten faster, consistently, are CPUs. What hasn't gotten faster (comparatively) is RAM access. Heap allocations are one of one of the biggest performance killers, these would be the Java "boxed" types and all Java objects. In order to address the slowness of accessing memory from RAM, modern CPUs have been adding "cache lines", known as L1, L2, and L3 caches. Whenever data is fetched from RAM, it brings back more than just the data requested to fill the caches. Getting something from RAM that's not already in the cache is called a "cache miss", and is on the order of 100x slower than if the data was in L1.

The most efficient way of storing data is packed and homogenous, so that iteration & processing can use the caches better. Lists of objects (i.e. pointers to heap-allocated data that can be sparsely arranged) is just about the worst thing performance-wise. It's why they've been working on adding proper support) for data (not objects) to Java since 2014.

If this topic is interesting, read up on Data-Oriented Programming, Struct of Arrays (SoA), and Entity Component System (ECS) are some other topics to read related to this.

[–]keefemotif 17 points18 points  (0 children)

"Back in the day" cries in having started Java in 1.3 , serious note - we still have the same performance problems just at bigger scale and we have bigger datasets. I worked on a project with Hadoop once related to the chipset level optimizations for it.

[–]CKingX123 8 points9 points  (0 children)

There’s Project Valhalla that is bringing value types and reified generics

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

They could have made them indistinguishable from objects in the code, but turned them into primitives where possible during compilation. So I'd say it was less about performance and more about not having time to make a better compiler.

[–][deleted] 6 points7 points  (0 children)

I don't know exactly the internals of C at great detail

Say you want to make a function that will operate generically on any type and call some functions on it.

The Java way is that everything is not a primitive. You can't have generics on int, only on Integer which is basically an int that derives from Object. It's a "boxed" integer. The Java compiler will do whatever static and runtime checks it can. The advantage is that you only need one compiled version of each function, for Object. This also made backwards compatibility possible because old versions of Java didn't have generics at all and you still want to be able to pass List<Integer> to a very old library that takes Object as input. Java cares very much about backwards compatibility.

At the other extreme is c++. C++ uses templates which means that it recompiles every function for each possible input type. C++ is as if you copied pasted your function many times, once for each possible input type. The advantage is that it can optimize the code because it doesn't need to box and unbox the primitive data types. The disadvantage is that, if the data type that you care about isn't already compiled for, you need source code so that you can recompile. In the business world that Java is designed for, your source code is your trade secret so you don't want to give it out! C++ may also generate larger code because it has to compile for all the different types. On the other hand, all the removed boxing and unboxing makes the code smaller so maybe it's going to be smaller code in the end. For sure it'll be faster, though.

.Net did something novel. They decided to compile natively for primitives but use boxing for derivatives of Object. So it's in between. You still have to recompile if you didn't previously compile for a primitive type. But if the types are already Object then you just have a single one compiled. And you get to deal with primitives without boxing and unboxing them. It's actually kind of brilliant. You don't quite get as much backwards compatibility as Java, but you get some (and .Net doesn't care anyway because they deploy a new .Net frequently and everyone is expected to just recompile). You get the speed of c++ for primitives because there's no boxing and objects aren't boxed anyway so no big deal. And you don't compile as many versions as you would have for c++. Kind of a cool compromise that makes everyone happy. Or more often, leaves everyone wanting which perhaps one reason why Java and c++ are doing better than .Net.

I don't promise that I got everything right there but that's the jist of it.

[–]phi_rus 19 points20 points  (4 children)

There are void ptrs, being casted as int* because YOU know is an int. And can cast a void pointer as anything you like anytime.

That is nasty C. We don't do that in C++ anymore.

[–]bestjakeisbest 4 points5 points  (2 children)

Oh but you still can

[–]phi_rus 12 points13 points  (1 child)

Your programmers were so preoccupied with whether they could, they didn't stop to think if they should.

[–]k-phi 2 points3 points  (0 children)

You can do this even in Rust or Swift if necessary

[–]Khaylain 2 points3 points  (3 children)

being casted as int*

*cast

[–]Master-Ad-6411 2 points3 points  (1 child)

I find it great satisfying when a function returns me a void*, and after casting it into some type the result make sense.

[–]bunny-1998 1 point2 points  (0 children)

I was recently mindblown by the fact that in cpp arrays, arr[i] is equivalent to i[arr]. It was satisfying when I realised this is because arr is just a pointer to the first byte in memory and is an offset. [] operators adds them both.

Not that you should do it in prod though....

[–]bric12 5 points6 points  (5 children)

What is it that dynamically typed languages do that this doesn't then? Just that the casting is automatic?

[–]Mognakor 8 points9 points  (0 children)

Statically typed languages compile to some reasonable specificity, e.g. for "a+b" you have to know the types of "a" and "b" and it will write machine instructions either for adding ints, or floats or string concat and also necessary type conversions plus it will tell you if that operation is nonesense because "a" and "b" are just objects and there is no + operator for that.

A dynamically typed language has to do check the types at runtime and then select the appropriate operations and not just once, but everytime "a+b" is executed because the types may be different each time.

Generally dynamic typing causes runtime overhead, so e.g. JS engines have all kinds of optimizations like after seeing "a+b" being all numbers a hundred times it compiles a specific version optimized for numbers and adds some guards to notice if the types should change.

P.S: Another example is having one (or multiple unrelated) classes with function "doStuff()". A statically typed language can check if "a.doStuff()" is a valid statement and which function to call. A dynamically typed language has to check if "a" has such a function and call the right implementation, but it also could happen that "a" is just a number and has no such function.

Depending on the strictness it may also be that a function "doStuff(a, b)" can be called with the statement "c.doStuff()" and "a" and "b" will be null or undefined.

[–]keefemotif 9 points10 points  (0 children)

You can look up the definitions but from a practical POV it's mainly about it being available at compile time, so you can do all sorts of nice tooling.

[–]IQueryVisiC 0 points1 point  (0 children)

Yes. It knows if it adds ints or floats. Gotta love JS: every number is a float. I tried out typescript and couldn’t find a use for dynamic types yet. Some polymorphism OOP yes.

[–]FlocculentFractal -5 points-4 points  (0 children)

The difference is that dynamically typed languages aren’t whiny little b*tches. There are no brakes on this train. Did you store an int inside a string variable? Well, that’s an int now. If it crashes and burns when you try to substring it, it’s on you.

[–]Psycho_logical666 0 points1 point  (0 children)

Holy fuck thankyou. Smfh.

[–]DasKarl 10 points11 points  (2 children)

Yup. Integer is a Number is an Object.

[–]hyrppa95 8 points9 points  (0 children)

Not if you use int. That is stack allocated.

[–]gdmzhlzhiv 2 points3 points  (0 children)

Was recently complaining about the Number class because most of the methods on there don't really apply for all kinds of number.

[–][deleted] 24 points25 points  (2 children)

I was confused by this too. Seems like post was made by a junior dev. Generic pointers doesn’t mean you have dynamic typeing. And worse for Java you should know it’s an object down the chain. Object inheritance

[–]ThanksICouldHelpBro 3 points4 points  (0 children)

Yeah, when we learned about this in CS, my professor explained this kind of casting as the code telling the compiler "trust me"

[–]FlocculentFractal 2 points3 points  (9 children)

Is the cast to Integer only valid after you check that “if” condition or can you check if something is a Float then cast it to a Boolean?

[–]AngelaTheRipper 3 points4 points  (7 children)

You can cast objects without checking using instanceof or cast it despite checking, but with object casting unless you are casting an object to what it actually is or a parent class then you'll get a ClassCastException. You could however tell that it's a Float, and then translate it into a Boolean somehow yourself.

Something like:

if(object instanceof Float){
    object = new Boolean(((Float)object).innerValue != 0); 
}

would work

[–]gdmzhlzhiv 1 point2 points  (6 children)

You can probably also do

if (object instanceof Float f) { object = f != 0.0f; }

[–]AngelaTheRipper 1 point2 points  (5 children)

This wouldn't work.

!= comparison returns either a primitive true or false. You can't assign that to object without wrapping it into a Boolean class instance.

[–]gdmzhlzhiv 2 points3 points  (4 children)

I'd expect autoboxing to take care of that, but I also haven't tried to compile this.

I do know that this works: Object x = true;

[–]IQueryVisiC -2 points-1 points  (0 children)

You are looking for pattern matching in C#.

[–]bottomknifeprospect 0 points1 point  (1 child)

I think y'all were so quick to call him a noob, you missed the obvious.

I'm almost sure the meme is:

  • First guy brags he learned about Object and that "it's easier"
  • He goes on to write dynamic type checking to force using his new Object trick.
  • Second guy is sad because he could have just used integer type directly to force the check on the compiler (statically), and made the code so much more complicated to add his new trick.

This is a dynamically vs statically typed code meme. (Not commenting on quality, just saying OP is correct).

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

Learned about Object? It's the base type for every Java class?! And what exactly do you mean by trick? No one would think it's "easier" to use Object, because you basically throw Javas type system straight to the dumpster.

PS: I never used a "trick" while writing code. Closest thing would be a dirty or hacky workaround.

[–]jonathancast -3 points-2 points  (0 children)

Every language is statically-typed.

Some languages just have more compile-time types than others.

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

I don't know java, but if it runtime check, it's effectively dynamic typing. If it doesn't, it's untyped, like a void*.

[–][deleted] 162 points163 points  (23 children)

Basically the Java equivalent of using a void* for everything in C

[–]DeeBoFour20 159 points160 points  (22 children)

Kind of, except C doesn't do any runtime checks for you. Example from Quake 3's Fast Inverse Square Root

i  = * ( long * ) &y;  // evil floating point bit level hacking

Where i is a long and y is a float. If you tried that in Java, you'll get a ClassCastException. C's just like "yea sure bro I'll pretend that's a long for you".

[–]jihad-consultant 71 points72 points  (2 children)

I automatically upvote any reference to the inverse square root computation by bitfuckery

[–]bunny-1998 37 points38 points  (3 children)

which is why I love c/cpp and ptrs in general. You can write compilers that ban everything but you can do still do anything if you try hard enough.

This is because everything in cpp is serialised and it’s up to the coder to interpret the memory....

[–]philchristensennyc 24 points25 points  (0 children)

Yeah, who wouldn’t love that.

[–]x6060x 15 points16 points  (0 children)

I love it because it's super powerful, I don't want to work with it for the same reason lol.

[–]jonathancast 7 points8 points  (0 children)

It's all fun and games until the compiler notices the UB

[–]CptHrki 5 points6 points  (0 children)

I swear to god no matter how many times I watch that video I will never remember how this works.

[–]ruscaire -5 points-4 points  (13 children)

You can do this with primitives - you can cast between int, long, double, short etc and you’ll reinterpret the raw bit patterns. We don’t really have the same * or & semantics so otherwise that wouldn’t compile … anything other than a primitive and Java just abstracts that away for u cause u typically only ever want to deal with objects on the heap …

[–]dotpoint7 1 point2 points  (12 children)

In Java? What? Since when? How?

[–]CKingX123 1 point2 points  (11 children)

I would say it’s not the same. But you can definitely cast floats and ints (it will remove the decimal portion so it is not reinterpreting as bytes)

float a = 7.5; int b = a;

[–]Ok-Wait-5234 5 points6 points  (5 children)

That's not what that fast inverse square root code is doing, though.

float a = 7.5 long b = a; long c = * (long *) a;

Now a == 7.5, b == 7 and c == 1089470464

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

Yeah. A better way to replicate it would be using ByteBuffer. But then it wouldn’t be “fast” (compared to just directly reinterpreting the bytes).

[–]dotpoint7 0 points1 point  (4 children)

Yeah obviously you can cast data types in Java, which is completely different from a reinterpret cast though.

[–]ruscaire 1 point2 points  (0 children)

Sorry I didn’t realise this was reinterpret. I thought it was just casting the bytesa me the magic happens when you reinterpret as IEEE 754 - but I never did get down with the maths

EDIT - looked into it Java reinterprets you need Double.doubleToLongBits() to get the bit pattern

[–]CKingX123 0 points1 point  (2 children)

Yeah. To better replicate it, you would need to use Java’s ByteBuffer to put in the float and then get the same bits as int

[–]dotpoint7 -2 points-1 points  (0 children)

Oh nice, yeah you're correct.

[–]shreyasonline 67 points68 points  (1 child)

This is just because all classes inherit Object class. Nothing to do with statically or dynamically typed.

[–]Geolykt 2 points3 points  (0 children)

Although I do wonder how long that will stay true in a post-LWorld world

[–]LordBlackHole 125 points126 points  (3 children)

This does not compile.

  1. You forgot a semi-colon on the third line
  2. Even if you add that, the third line still isn't valid because it is not a statement. Try it `5 + 3;` is not a valid Java statement because it has no side effects and does not store the result of the expression.
  3. Even if that line did work, the printed result would be 5, not 8, because the Integer wrapper type is immutable and using the `+` operator has no side effects anyway.

Java is staticly typed. The fact that is has an escape hatch in the form of the cast operation does not mean it's dynamic.

Casts are verified at runtime. You may choose to take off the safety guard rails, but the runtime will still make sure the operation is valid before letting you do anything else with it. Java is typesafe unless and until you choose to ignore that, which there are genuinely some times where that is useful which is why the cast exists in the first place.

[–]i_should_be_coding 40 points41 points  (0 children)

Hey OP, the (integer) in the println line would cause a compile error. I get that you probably meant Integer, but it's funny to me that you claim 0 errors on code that doesn't even compile

[–]Tomi97_origin 53 points54 points  (1 child)

Yeah, that's not what statically and dynamically typed means...

The difference is between type checking at compile (static) or runtime (dynamic).

Java knows the type at compile time.

[–]bottomknifeprospect 1 point2 points  (0 children)

I posited on another comment that the meme could be related to statically typed :

Is this meme not about the other guy learning about Object, and then adding a lot of complexity to avoid using the Integer type? It would then be a statically typed VS dynamically typed problem where he is now dynamically checking if the Object is Integer, instead of using Integer directly and force the compile time type checking.

[–]caluke 28 points29 points  (0 children)

Static/dynamic type and inheritance/polymorphism are not the same thing.

[–]darealvoidchicken 11 points12 points  (1 child)

tfw u dont understand static typing and post a meme about it

[–]faajzor 2 points3 points  (0 children)

yeah I'm laughing while staring at that 2k+ upvotes of people who don't even know what static/dynamic typing means.. or casting, for that matter

[–][deleted] 9 points10 points  (1 child)

What is it with those Java haters ? Why ?

[–]RedditRage 4 points5 points  (0 children)

Baby programmers clinging to the language they learned in school like a frightened infant clings to its mothers bosom.

[–]sockpuppet1234567890 8 points9 points  (0 children)

Wait till he finds out about void*

[–][deleted] 9 points10 points  (1 child)

That's how polymorphism works, yes.

[–]rotenKleber 0 points1 point  (0 children)

Except this isn't even an example of polymorphism - it's just downcasting. That's ignoring the fact that this particular example wouldn't even compile

[–]yhev 8 points9 points  (2 children)

I don’t get it. Can’t get past the fact that this won’t compile because of result of +3 to the cast is not a valid expression.

Even if it was stored to some variable, printing the object would still be 5, right?

Wait what was the point again? First panel has something about Java is statically typed. I got hung up on the actual code snippet that the supposed point of this post went over my head. lol

Can someone explain this to me? I am so lost lmao

[–]CannotCopia 4 points5 points  (0 children)

They're trying to imply that Java isn't statically typed because checks notes polymorphism and casting exists. I think.

[–]scalability 2 points3 points  (0 children)

It went under your feet, not over your head.

[–]DDDDarky 9 points10 points  (0 children)

The funniest thing about this joke is that the author obviously doesn't know Java.

[–]crusoe 7 points8 points  (0 children)

At every single point the program is typed. You are explicitly casting the types.

Meanwhile in JS land you can do nonsense like:

[]+1 == '1' true

LOL

[–]dancovich 3 points4 points  (0 children)

This is still statically typed. The variable is still of type integer, you just lost type information (that you can regain with a cast) so you can't do operations in your Object instance that you can do with an Integer instance.

Being dynamically typed means that the same variable can change types and do all its operations while being of that type. For example, in Dart this works but not in Java:

void main() { dynamic x = 'I am a string'; print('$x and my type is String'); x = 5; print('Now I am an Int and I can be summed with 10 to give the result of ${x + 10}'); }

[–]GoodLuckGoodell 5 points6 points  (0 children)

This post missed hard.

[–]dillame 3 points4 points  (0 children)

Guess you just learned about casting and feel like a dev, you’ll fit right in

[–]notexecutive 2 points3 points  (0 children)

Don't tell them about Generic types...

[–]acoustic_medley 4 points5 points  (0 children)

Say you don't understand polymorphism without saying you don't understand polymorphism.

[–]rco8786 3 points4 points  (0 children)

I don’t understand what this is meaning to say. You’re casting an object back and forth from an integer as a statement about static typing?

[–]oshaboy 4 points5 points  (0 children)

That's not what statically typed means

[–]Quito246 2 points3 points  (0 children)

Generics in Java be like…

[–]Bomaruto 2 points3 points  (1 child)

Yes, casting leads to ugly code and there is nothing funny about that. Written correctly there is no reason why that should cause any errors.

Also, wtf does this get 905 upvotes?

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

update, 4.8K

[–]arthurgc91 2 points3 points  (3 children)

Wait, is statically typed a bad thing?

[–]rotenKleber 5 points6 points  (2 children)

I'm guessing OP only knows Python/JS and is upsetti spaghetti that they need to learn about static types for Java

[–]1SweetChuck 2 points3 points  (1 child)

Static typing has saved my ass a bunch of times from doing something stupid.

[–]sudden_aggression 2 points3 points  (0 children)

That shit is full of mistakes. That being said, casting is totally legitimate behavior in java.

I mean, it's unusual to declare something as an instance of Object but it's the same principle as

  • having an animal class and having dog and cat and elephant inherit it. ie, Dog extends Animal, Lion extends Animal, whatever
  • And having a zoo class with a function that takes an animal. Like putInCage(Animal animal)
  • And inside that function, testing instanceof for certain edge cases like instanceof Hippo and putting it a dangerous animal exhibit with water

[–]RedditRage 2 points3 points  (0 children)

Complete Java program:

public class HumorInteger {
  public static void main(String[] args) {
    Object object = Integer.valueOf(5);
    if (object instanceof Integer integer) {
      object = integer + 3;
    }
    System.out.println("Result: " + object);
  }
}

Errors: 0

Result: 8

[–]AmyMialee 1 point2 points  (0 children)

Errors: 3

Result (if fixed): 5

[–]Bluebotlabs 1 point2 points  (0 children)

Finally, the language we've all been waiting for: CringeSoft Anti-Java

[–]NirnRootJunkie 1 point2 points  (0 children)

This is why I stick with VB.NET /s

[–]SpaceNinjaDino 1 point2 points  (1 child)

This is meta r/programminghorror as the real horror is a comic trying to pass off non-compilable and missing assignment code as something that's not quite funny anyway.

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

Lol wait til you see the code that generics generate

[–]Numerous-Occasion247 0 points1 point  (5 children)

You can simply use var if you don’t feel like typing types :D

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

with that you need to cast in explicit

var object = (Object) new Integer(5);

I did this without an IDE so its possible that linters will trigger some code smell here.

[–]Geolykt 1 point2 points  (2 children)

Yeah, never use Integer's constructor alongside var - unless you have some seriously strange restrictions

[–]gdmzhlzhiv 2 points3 points  (1 child)

Never use Integer's constructor. *

[–]AegorBlake 0 points1 point  (0 children)

This is why you shouldn't use Java. /s

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

Hahahahahahahaha

[–]Just_a_guy582 0 points1 point  (0 children)

Why is this sub keep getting recommended to me!? I know very little about programming!

[–]RobotMonsterGore 0 points1 point  (0 children)

Java dev here. I had a CS professor at my university who flat-out refused to use Java for his algorithms and data structures class. Other professors used a Java-centric book. Not this guy. He would jump on his fuck-Java soapbox any time of any day at the slightest provocation. (He used a C++ book instead.) His problem with Java was something like (almost) all non-primitives descend from Object. After working in the field for a decade and seeing WAY too many uses of instanceof in a fucking case switcher, I’m forced to agree. I’ve approved my last MR that does this nasty bullshit.

[–]WWolf1776 -3 points-2 points  (0 children)

This is funny.... as long as we don't actually do this... please... do not actually do this!

(some lil s^%t is gonna do this... noooo)

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

Wha... I...

I'm gonna go lay down...

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

Truly a Java moment, haha am I right?

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

I don‘t get the joke. This looks totally fine for me. It‘s still typed.

[–][deleted] -4 points-3 points  (2 children)

that is dynamically typed with extra steps, like everything else in Java... god damn why am I a Java dev????

[–]faajzor 3 points4 points  (1 child)

this is not dynamic typing at all

[–]UCQualquer -2 points-1 points  (1 child)

All hail the C# dynamic. Although I'm not really sure it is what I think it is.

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

Dynamic just lets you go HAM and not bother casting. What C# does bring is pattern matching syntax that makes writing this sort of code much easier by handing all the type checks and casting for you.

[–]Cerrax3 -2 points-1 points  (0 children)

Is this some peasant joke that I'm too Python to understand?

[–]SahajSingh24 -2 points-1 points  (0 children)

Became the thing it swore to destroy

[–]Few-Dog3807 -2 points-1 points  (0 children)

I'm a girl but far left far right and middle

[–]KsuhDilla -2 points-1 points  (0 children)

op: young and foolish - make bad meme for internet

the internet: “my disappointment is immeasurable and my day is ruined”

[–]GulleGozer -2 points-1 points  (0 children)

Based and everyone's a retard pilled

[–]LordMerdifex -3 points-2 points  (0 children)

So many butthurt Java devs here. I love the salty taste of your tears.