Languages
[Edit]
EN

Java - compare long vs Long (primitive vs boxed long)

1 points
Created by:
Waleed-Gates
322

Quick solution:

// import java.util.Objects;            
Long number1 = 100L;
long number2 = 100L;

boolean equals = Objects.equals(number1, number2);
System.out.println(equals); // true

Full example:

import java.util.Objects;

public class java_compare_long_vs_Long {

    public static void main(String[] args) {

        {
            Long number1 = null;
            long number2 = 100L;
            System.out.println(Objects.equals(number1, number2)); // false
        }

        {
            Long number1 = 100L;
            long number2 = 100L;
            System.out.println(Objects.equals(number1, number2)); // true
        }

        {
            Long number1 = null;
            Long number2 = 100L;
            System.out.println(Objects.equals(number1, number2)); // false
        }

        {
            Long number1 = 100L;
            Long number2 = 100L;
            System.out.println(Objects.equals(number1, number2)); // true
        }
    }
}

 

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join