Search code examples
javasubtraction

Subtracting two Long values


Why do I get error when I run the code below? How can I fix this? I want to System.out.print(hi-hello);

Long hello = 43;
Long hi = 3523;
public class HelloWorld{
    public static void main(String[] args){
        System.out.print(hi-hello);
    }
}

Solution

  • Because hi and low are declared as LONG objects, they must be either declared as literal by adding the L at the end or use the Long class

    public class HelloWorld {
         public static void main(String[] args) {
            Long hello = 43L;
            Long hi = 3523L;
            System.out.print(hi-hello);
         }
    }