|
Re: String manipulations
|
Posted: Jan 6, 2004 5:06 PM
|
|
1. "String" == "String" 2. "String".trim() == "String" 3. "String".trim() == "String".trim()
All three will return true. I don't know how you are getting it false in one case. Try again. Make sure you don't mistype any characters.
As far as abs(int) in concerned, please read the Java Documentation for it. I have included it for you below. The reason, it gives negative value is that in all integer range the negative extreme is one more than positive extreme if you consider only its value. For example, byte range is -128 to +127. See 128 on negative side and 127 positive side. So, when you say -(-128), it becomes +128, which is out of range for byte. If you add 1 to 127 byte value then it will be -128, because +128 is not in byte range. The same logic goes with Integer.MIN_VALUE.
Please read any computer science book on how to represenet integers in computer and then it will be clear to you.
Here is code for int abs(int). You can see abs() is doing what it is supposed to do, that is, return the value if it is positive, or return its negation. It is returning negative value because of limitation or the way integers are represented in computer. If we go by mathematical definition of abs then it is wrong. But, this is how abs() works in Java. Other way to get positive value is to use a long so that abs(long) is called.
public static int abs(int a) {
return (a < 0) ? -a : a;
}
Here Java doc for int abc(int)
public static int abs(int a)
Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. 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.
|
|