539
Java (lemmy.world)
top 50 comments
sorted by: hot top controversial new old
[-] RoyaltyInTraining@lemmy.world 105 points 1 year ago

Makes sense, cause double can represent way bigger numbers than integers.

[-] lysdexic@programming.dev 33 points 1 year ago

Also, double can and does in fact represent integers exactly.

[-] Killing_Spark@feddit.de 19 points 1 year ago

Only to 2^54. The amount of integers representable by a long is more. But it can and does represent every int value correctly

load more comments (3 replies)
[-] fsxylo@sh.itjust.works 27 points 1 year ago

Also because if you are dealing with a double, then you're probably dealing with multiple, or doing math that may produce a double. So returning a double just saves some effort.

Yeah it makes sense to me. You can always cast it if you want an int that bad. Hell just wrap the whole function with your own if it means that much to you

(Not you, but like a hypothetical person)

load more comments (14 replies)
[-] jayrhacker@kbin.social 67 points 1 year ago

It's the same in the the standard c library, so Java is being consistent with a real programming language…

[-] idunnololz@lemmy.world 24 points 1 year ago

Implying java isn't a real programming language. Smh my head.

[-] Glome@feddit.nl 6 points 1 year ago

Java has many abstractions that can be beneficial in certain circumstances. However, it forces a design principle that may not work best in every situation.

I.e. inheritance can be both unnatural for the programmer to think in, and is not representative of how data is stored and manipulated on a computer.

[-] whats_a_refoogee@sh.itjust.works 10 points 1 year ago

You don't have to use inheritance with Java. In fact, in most cases it's better that you don't. Practically all of the Java standard library doesn't require the use of inheritance, same with most modern libraries.

On the contrary, I think inheritance is a very natural way to think. However, that doesn't translate into readable and easy to maintain code in the vast majority of the cases.

I am not sure what you mean by how it's stored or manipulated on a computer. A garbage collected language like Java manages the memory for you. It doesn't really care if your code is using inheritance or not. And unless you're trying to squeeze the last drops of performance out of your code, the memory layout shouldn't be on your mind.

[-] shotgun_crab@lemmy.world 3 points 1 year ago

Composition + Interfaces is love

load more comments (1 replies)
[-] lightsecond@lemmy.world 4 points 1 year ago

We’re gate-keeping the most mainstream programming language now? Next you’ll say English isn’t a real language because it doesn’t have a native verb tense to express hearsay.

load more comments (1 replies)
[-] jayrhacker@kbin.social 5 points 1 year ago

Java is, of course, Turing Complete™️ but when you have to hide all the guns and knives in jdk.internal.misc.Unsafe something is clearly wrong.

load more comments (2 replies)
load more comments (3 replies)
[-] korstmos@kbin.social 50 points 1 year ago

Doubles have a much higher max value than ints, so if the method were to convert all doubles to ints they would not work for double values above 2^31-1.

(It would work, but any value over 2^31-1 passed to such a function would get clamped to 2^31-1)

[-] karlthemailman@sh.itjust.works 8 points 1 year ago

So why not return a long or whatever the 64 bit int equivalent is?

To avoid a type conversion that might not be expected. Integer math in Java differs from floating point math.

Math.floor(10.6) / Math.floor(4.6) = 2.5 (double)

If floor returned a long, then

Math.floor(10.6) / Math.floor(4.6) = 2 (long)

If your entire code section is working with doubles, you might not like finding Math.floor() unexpectedly creating a condition for integer division and messing up your calculation. (Have fun debugging this if you're not actively aware of this behavior).

[-] korstmos@kbin.social 4 points 1 year ago

Because even a long (64-bit int) is too small :)
A long can hold 2^64-1 = 1.84E19
A double can hold 1.79E308

Double does some black magic with an exponent, and can hold absolutely massive numbers!

Double also has some situations that it defines as "infinity", a concept that does not exist in long as far as I know (?)

[-] parlaptie@feddit.de 4 points 1 year ago

But there's really no point in flooring a double outside of the range where integers can be represented accurately, is there.

load more comments (5 replies)
[-] MarekKnapek@programming.dev 37 points 1 year ago

Makes sense, how would you represent floor(1e42) or ceil(1e120) as integer? It would not fit into 32bit (unsigned) or 31bit (signed) integer. Not even into 64bit integer.

[-] natanael@lemmy.ml 7 points 1 year ago

BigInt (yeah, not native everywhere)

[-] 1rre@discuss.tchncs.de 3 points 1 year ago

I feel this is worse than double though because it's a library type rather than a basic type but I guess ceil and floor are also library functions unlike toInt

[-] maniacal_gaff@lemmy.world 31 points 1 year ago

It would be kinda dumb to force everyone to keep casting back to a double, no? If the output were positive, should it have returned an unsigned integer as well?

[-] larvyde@sh.itjust.works 15 points 1 year ago

int coerces to double automatically, without explicit casting

[-] snake_baitman@lemmy.sdf.org 18 points 1 year ago

The CPU has to do real work to convert between float and int types. Returning an int type would just be giving the CPU extra work to do for no reason

[-] notnotmike@programming.dev 3 points 1 year ago

I'm learning so much from this thread and I don't even use Java

load more comments (5 replies)
[-] rev@ihax0r.com 27 points 1 year ago

python is like this also. I don’t remember a language that returned ints

[-] shotgun_crab@lemmy.world 3 points 1 year ago

Python 2 returns a float, Python 3 returns an int iirc.

[-] CaptainHowdy@lemm.ee 17 points 1 year ago

My God this is the most relevant meme I've ever seen

[-] Acetamide@lemmy.world 15 points 1 year ago

AFAIK most typed languages have this behaviour.

[-] Lmaydev@programming.dev 4 points 1 year ago

Because ints are way smaller. Over a certain value it would always fail.

load more comments (2 replies)
[-] Aelorius@jlai.lu 6 points 1 year ago

Logic, in math, if you have a real and you round it, it's always a real not an integer. If we follow your mind with abs(-1) of an integer it should return a unsigned and that makes no sense.

[-] kogasa@programming.dev 24 points 1 year ago

in math, if you have a real and you round it, it's always a real not an integer.

No, that's made up. Outside of very specific niche contexts the concept of a number having a single well-defined type isn't relevant in math like it is in programming. The number 1 is almost always considered both an integer and a real number.

If we follow your mind with abs(-1) of an integer it should return a unsigned and that makes no sense.

How does that not make sense? abs is always a nonnegative integer value, why couldn't it be an unsigned int?

[-] Aelorius@jlai.lu 6 points 1 year ago

I'm ok with that, but what I mean is that it makes no sense to change the type of the provided variable if in mathematics the type can be the same.

load more comments (2 replies)
[-] Fantasmita@lib.lgbt 5 points 1 year ago

It's like Java not having unsigned integers...

[-] MrGeekman@lemmy.world 3 points 1 year ago

Try Math.round. It’s been like ten years since I used Java, but I’m pretty sure it’s in there.

load more comments
view more: next ›
this post was submitted on 05 Aug 2023
539 points (92.4% liked)

Programmer Humor

19463 readers
15 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS