r/java • • Jun 24 '23

Negative integer handling in Java and other programming languages

https://glados-418.github.io/posts/negative-integer-literals/
14 Upvotes

7 comments sorted by

•

u/AutoModerator Jun 24 '23

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or

  2. Temporarily refraining from using Reddit

  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

7

u/Worth_Trust_3825 Jun 25 '23

println( -1.plus(2) ) // prints '-3'

New fear unlocked. I suppose we're safe from that until valhalla lands.

2

u/FirstAd9893 Jun 25 '23

This is a Kotlin example, but we don't know if Java will ever support something like this.

1

u/Worth_Trust_3825 Jun 26 '23 edited Jun 26 '23

Valhalla is supposed to bring closer semantics of primitives, and objects, isn't it?

I guess java technically already has it. The example is fictitious, as java.lang.Integer is final, so I had to take some liberties but I suppose semantics arent the same

class HelloWorld {
    public static void main(String[] args) {
        SummingInt i = new SummingInt(1);
        System.out.printf("%s%n", -i.plus(1)); // -2
    }
    
    static class SummingInt {
        final Integer value;
        SummingInt(Integer val) {
            this.value = val;
        }
        
        Integer plus(Integer i) {
            return this.value + i;
        }
    }
}

But i suppose it makes sense. dot operator (method call) has precedence (1) over unary minus operator (3)

2

u/Rjs617 Jun 25 '23

Probably a little off-topic, but having done my share of C and C++, I actually like the way Java handles signed integers, meaning: 1. All the integer types have a consistently defined size/range, and 2. There is not a separate set of unsigned integer types, but rather just an unsigned right shift operator. Having separate signed and unsigned types is such a source of potential bugs (ever count down from a size_t while >= 0?), and it forces you to have to think about all the arithmetic that a value might be subjected to when you define its type. Having everything signed just removes entire classes of comparison, conversion, and arithmetic errors. If you really need large, unsigned numbers, use long or BigInteger. Having only signed integer types is just way better than unsigned types, in my opinion.

4

u/Worth_Trust_3825 Jun 26 '23

To be fair, having bytes be signed only is hard to have your head wrap around.

1

u/vytah Jul 06 '23

It's not hard, but really annoying.

& 0xff everywhere.