Why Comparison Integer Using == Inward Coffee V Is Bad?

Java v introduced auto-boxing which automatically converts int to Integer together with because of that many Java developers started writing code for comparing int variable to Integer object together with Integer to Integer using == operator. You should avoid that because it tin shipping away create subtle bugs inwards your programme because it alone industrial plant for a detail hit of integers together with non for all. There is about other employment also when yous compare int variable to Integer object using == operator, the Integer object is converted to a primitive value. This tin shipping away throw nix pointer exception if the Integer object is null, which tin shipping away crash your program. Now when yous compare ii Integer object using a == operator, Java doesn't compare them yesteryear value, exactly it does reference comparison. When way fifty-fifty if the ii integer has the same value, == tin shipping away provide imitation because they are ii dissimilar objects inwards the heap. That's why yous should purpose equals() method to compare objects.

There is i to a greater extent than betoken which makes affair tricky, caching of integer objects yesteryear Integer.valueOf() method. What this hateful is that your programme volition function for about input exactly non for others.

This happens because motorcar boxing uses Integer.valueOf() method to convert Integer to int together with since method caches Integer object for hit -128 to 127, it provide same Integer object inwards heap, together with that's why == operator provide truthful if yous compare ii Integer object inwards the hit -128 to 127, exactly provide imitation afterwards, causing bug.

Before Java 1.4 that was non a employment because at that spot was no autoboxing, so yous e'er knew that == volition banking concern tally reference inwards heap instead of value comparison.



Comparing Integer object alongside == inwards Java

boxing which automatically converts int to Integer together with because of that many Java developer Why Comparing Integer using  == inwards Java v is Bad?Some of the JVM cache objects of about wrapper cast e.g. Integer from -128 to 127 together with provide same object which if compare via “ ==” tin shipping away provide truthful exactly after this hit this validity doesn't function together with to last out far worse this demeanour is JVM theme so improve avoid this sort of banking concern tally together with purpose equals() method. e.g.

Integer i1 = 260; Integer i2 = 260;  if (i1 == i2) {     System.out.println("i1 together with i2 is equal"); } else {    System.out.println("i1 together with i2 is non equal "); }

Here yous volition around belike larn "i1 together with i2 is non equal " at to the lowest degree inwards my machine.
because inwards this case, unboxing functioning is non involved. The literal 260 is boxed into ii dissimilar Integer objects ( over again it varies betwixt JVM to JVM) , together with and then those objects are compared alongside ==. The effect is false, every bit the ii objects are dissimilar instances, alongside dissimilar retentiveness addresses. Because both sides of the == seem comprise objects, no unboxing occurs.

Integer i1 = 100; Integer i2 = 100;  if (i1 == i2) {     System.out.println("i1 together with i2 is equal"); } else {    System.out.println("i1 together with i2 is non equal "); }


Here, around belike yous volition larn the text "i1 together with i2 is equal".
Because int values from -127 to 127 are inwards a hit which around JVM volition similar to cache so the VM genuinely uses the same object instance (and thus retentiveness address) for both i1 together with i2. As a result, == returns a truthful result.

This is really indeterministic together with alone shown yesteryear illustration since about JVM exercise optimization at for certain integer value together with endeavor to provide same object every fourth dimension exactly its non guaranteed or written behavior.

So best is to avoid this sort of code inwards shipping service Java 1.5 together with instead purpose equals() method to compare Integers inwards Java, which is to a greater extent than deterministic together with correct.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!


Sumber https://javarevisited.blogspot.com/

0 Response to "Why Comparison Integer Using == Inward Coffee V Is Bad?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel