Q1. System.out.println(Math.abs(Integer.MIN_VALUE)); it is printing a negative value. why? Q2. if("String".replace('g','G') == "String".replace('g','G')) System.out.println("Equal"); else System.out.println("Not Equal"); it is printing not equal. why? when the result for both side of == is same, it should point to the same location in the pool. so it should be equal. Thanks
A1. Look at the API-doc concerning abs: "Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative." That is somehow logical, because the "real" abs-value would be 2147483648, but Integer.MIN_VALUE is 2147483647 (think about zero!).
A2. Again, have a look at the API-doc concerning String.replace: "Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar." So each of the "replace"s creates a new String, i.e. a new object; so you have two new objects, which are not the same. Nothing else is checked in the if-statement. Therefore, if you change from "replace('g','G')" to "replace('x','G'), the result is...?! Got it?!
Hope it helps Detlev [This message has been edited by Detlev Beutner (edited July 16, 2001).]
Ans1. Why does it do so / why does it have to do so ??? Hey Detlev could u explain it a bit more !!!
Ans2. This is what the the docs say's 'bout the String's ---- Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. This means that once a String object is created , u can't change the contents of that string object . Everytime u need an altered version of an existing string , a new String object is created with the modifications . And since a String object is created for every string literal , one can use a string literal anywhere as a String object . So in u'r if statement there are two instances of String objects . Next the == operator with objects compares two object refernces & returns true only if 'em two refer to the same instance . In u'r code they do not & so u get false . To get true in the if statement u need to use equals() from the String class. U should do like this to get true --- if("String".replace('g','G').equals("String".replace('g','G'))) public boolean equals(Object anObject) Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. (From the docs) Check this out String x = new String("String"); if(x.replace('g','G') == x.replace('g','G'))//I thought this would give true . silly me ! String y = new String(); if(y.replace('g','G') == y.replace('g','G'))//this is true. To get String objects that can be modified use StringBuffer class , but it inherits it's equals method from the Object class which behaves essentially like the == operator . Hope this helps ! Thought -- Once a String object is created , u can't change the contents of that string object . I think this is 'cause String is declared final . And if String objects are automatically created for string literals , then howcome a == b is true in the following case String a = "abc"; String b = "abc"; System.out.println(a == b);
Originally posted by Detlev Beutner: A1. That is somehow logical, because the "real" abs-value would be 2147483648, but Integer.MIN_VALUE is 2147483647 (think about zero!).
Hi Integer.MIN_VALUE is -2147483648 Integer.MAX_VALUE is 2147483647. Now I don't know why this is so but maybe it is because if you convert 2147483648 to binary you get 1000 0000 0000 0000 0000 0000 0000 0000 which in signed form is equal to -2147483648 Now folks I don't claim it to be the right answer it is just a speculation, so don't hold me to it
That is somehow logical, because the "real" abs-value would be 2147483648, but Integer.MIN_VALUE is 2147483647 (think about zero!).
this was (obviously!?) a mistake in writing, I meant Integer.MAX_VALUE. For the rest look at Anshul's answer; he's right.
2.) Ashish: Look at this (it's almost your code):
String x actually gets replaced, so two new strings are created, which are different objects, so no "true1" is printed. String y does not get replaced, so nothing is done, so you just check if y == y, which obviously is true, and "true2" is printed.
3. Look at this:
Java is so "intelligent" to check whether a constant exists, if another is created (everything else would be stupid, because a constant is a constant is a constant, and there would be no sense to hold two equal constants in memory). So when "b" (the reference) is created, it is a reference to the constant "a" is already referencing. That's the reason why a == b.
Hope it helps Detlev
Joined: Jul 14, 2001
Hey Det Is this how i can explain it then -- "An object is created for every string literal " -- this statement is rite & what happens with b = "abc" is that b will refer to the same instance of the String object created by a = "abc" & hence a == b is true . Thanx [This message has been edited by Ashish Hareet (edited July 17, 2001).]
Joined: Jul 13, 2001
You are perfectly right :-) (The confusing point might be the sentence: "An object is created for every string literal" which could be differentiated: "For every string literal there will be be an object; created, if not yet there, just referenced, if already existing." Or so.) Detlev
Joined: Jul 14, 2001
Cheers Det ! It helped me understand better [This message has been edited by Ashish Hareet (edited July 17, 2001).]