<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Arunkv</id>
		<title>OWASP - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Arunkv"/>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php/Special:Contributions/Arunkv"/>
		<updated>2026-04-23T13:22:57Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.2</generator>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Java_gotchas&amp;diff=19267</id>
		<title>Java gotchas</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Java_gotchas&amp;diff=19267"/>
				<updated>2007-06-22T20:34:08Z</updated>
		
		<summary type="html">&lt;p&gt;Arunkv: Added link to JLS 5.1.7&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Equality ==&lt;br /&gt;
Object equality is tested using the == operator, while value equality is tested using the .equals(Object) method.  For example:&lt;br /&gt;
 String one = new String(&amp;quot;abc&amp;quot;);&lt;br /&gt;
 String two = new String(&amp;quot;abc&amp;quot;);&lt;br /&gt;
 String three = one;&lt;br /&gt;
 if (one != two) System.out.println(&amp;quot;The two objects are not the same.&amp;quot;);&lt;br /&gt;
 if (one.equals(two)) System.out.println(&amp;quot;But they do contain the same value&amp;quot;);&lt;br /&gt;
 if (one == three) System.out.println(&amp;quot;These two are the same, because they use the same reference.&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The output is:&lt;br /&gt;
 The two objects are not the same.&lt;br /&gt;
 But they do contain the same value&lt;br /&gt;
 These two are the same, because they use the same reference.&lt;br /&gt;
=== Autoboxing ===&lt;br /&gt;
Java 5's autoboxing and unboxing features add some new gotchas to testing for equality.  Consider the following example:&lt;br /&gt;
 Integer i = 100;&lt;br /&gt;
 Integer p = 100;&lt;br /&gt;
 if (i == p)  System.out.println(&amp;quot;i and p are the same.&amp;quot;);&lt;br /&gt;
 if (i != p)   System.out.println(&amp;quot;i and p are different.&amp;quot;);	&lt;br /&gt;
 if(i.equals(p))  System.out.println(&amp;quot;i and p contain the same value.&amp;quot;);&lt;br /&gt;
The output is:&lt;br /&gt;
 i and p are the same.&lt;br /&gt;
 i and p contain the same value.&lt;br /&gt;
 &lt;br /&gt;
The code above shows that i == p because the values are first unboxed into the primitive int type, and then the comparison is performed.  But this is only true for Integer values &amp;gt;= -128 and &amp;lt;= 127!  In other words, if the above example is changed so that i = 200 and p = 200, then i == p evaluates to false and the output would be:&lt;br /&gt;
 i and p are different.&lt;br /&gt;
 i and p contain the same value.&lt;br /&gt;
&lt;br /&gt;
This behavior is documented in the [http://java.sun.com/docs/books/jls Java Language Specification] [http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7 section 5.1.7]. Quoting from there:&lt;br /&gt;
:If the value ''p'' being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let ''r1'' and ''r2'' be the results of any two boxing conversions of ''p''. It is always the case that ''r1'' == ''r2''.&lt;br /&gt;
&lt;br /&gt;
Notice in the above example that the declaration was:&lt;br /&gt;
 Integer i = 100;&lt;br /&gt;
If this had been:&lt;br /&gt;
 Integer i = new Integer(100);&lt;br /&gt;
Then the output would have been different:&lt;br /&gt;
 i and p are different.&lt;br /&gt;
 i and p contain the same value.&lt;br /&gt;
&lt;br /&gt;
== Incrementing values ==&lt;br /&gt;
Be careful of the post-increment operator:&lt;br /&gt;
&lt;br /&gt;
  int x = 5;&lt;br /&gt;
  x = x++;&lt;br /&gt;
  System.out.println( x );&lt;br /&gt;
&lt;br /&gt;
Prints 5.&lt;/div&gt;</summary>
		<author><name>Arunkv</name></author>	</entry>

	</feed>