The Artima Developer Community
Sponsored Link

Java Answers Forum
String manipulations

8 replies on 1 page. Most recent reply: Jan 9, 2004 3:40 AM by Andrew Cowan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
suneetha

Posts: 4
Nickname: venussun
Registered: Nov, 2003

String manipulations Posted: Jan 6, 2004 4:03 PM
Reply to this message Reply
Advertisement
hi everyone
I have trouble understanding the String operations.Please help me.Heres my question

if("String"=="String")
it prints true

if("String".trim()=="String")
it too prints true;
but if we give

if("String".trim()=="String".trim()
it evaluates false Y so I dont know.
Please make this clar to me


and again

Math.abs(Integer.MIN_VALUE())
gives a negative answer ie Integer.MIN_VALUE().But not the negation of it ....y is so??????

please reply my ansers and make these topics clear to me

thanking u
suneetha.


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: String manipulations Posted: Jan 6, 2004 5:06 PM
Reply to this message Reply
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.

Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: String manipulations Posted: Jan 7, 2004 7:57 AM
Reply to this message Reply
I am guessing because you are trimming the String and testing if it is equal that you are trying to see if the values are the same. if so you should be using .equals to compare

String s = "hello ";
if (s.trim().equals(s))

Bill Fink

Posts: 6
Nickname: wrfink
Registered: Jan, 2004

Objects Posted: Jan 7, 2004 12:43 PM
Reply to this message Reply
Correct me if I am wrong, but I believe to answer your question you must first understand what you are comparing.

The equal sign == will compare values found on the stack. Since you are comparing two Strings, you are making a comparison between two object references. Your question can further be explored by examining how Java stores Strings. When you create a String object with quotes, Java creates an immutable String object. If you create another String object with quotes having the same value, Java will just give you reference to the initial String object.

String x = "Test";
String y = "Test";

A string object with the value of "Test" is placed on the heap. Variables x and y will have reference values contained on the stack. So, if we examine the statement x==y, it will return true...because Java is comparing two reference values pointing at the same Object on the heap.

Try this:

String x = new String("Test");
String y = new String("Test");

Now, x==y will return false. Why? Because the keyword new forced Java to create a new Object on the heap and place reference to it on the stack.

When you need to test the equality of a String, you should use the equals method (or one of the many other equality testing methods).

Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: Objects Posted: Jan 7, 2004 1:47 PM
Reply to this message Reply
I was thinking that, that is why i mentioned the .equals

then suneetha started a third thread (it is much easier to keep track of a thread when it is on 1 not 3 places) and said:

"Thank u soo much for ur reply.yaa it works well using equals but i need to know wots happening in "if" coz iam preparing for scjp.So please reply my letter.
thanking u,
suneetha."

so in the second thread I gave the answer to what i think she was asking. Witch I believe is "What java is doing behind the scene with the code". Since certification is what this was relating too.

....yeah one thread is much easier to deal with then

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: String manipulations Posted: Jan 7, 2004 3:19 PM
Reply to this message Reply
Mr. Brozny
Since she is triming the string and the string after trimming is unchanged, she will get the reference to the same string object. So, "String".trim() == "String" will return true. If you notice she has changed her question content in next post and she is asking about "String ".trim() == "String", which will return false.

Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: String manipulations Posted: Jan 7, 2004 3:59 PM
Reply to this message Reply
Yes it is a slightly different questions but she also replied to the ".equals" from either you or myself in another thread http://www.artima.com/forums/flat.jsp?forum=1&thread=27696"


"hi
Thank u soo much for ur reply.yaa it works well using equals but i need to know wots happening in "if" coz iam preparing for scjp.So please reply my letter.
thanking u,
suneetha."


That cleared it up for me that it was not a total novice question misusing "==" where they should be using ".equels" since most people going for scjp know the difference. (maybe I am wrong) But then there was another answer in this post after she stated that about using equals again.

...that is not a little more confusing than it has to be replying to an answer in a different thread???

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: String manipulations Posted: Jan 7, 2004 4:12 PM
Reply to this message Reply
I agree with you. String is the most used and least understood class in Java. I have also replied to your another post in another thread. I didn't want to reply to your thread because there is no problem with that. You are trying to help someone. But, I couldn't stop myself when saw verbose and misleading explanation about string in the quote you have quoted in your post. So, it is about the quote and not about your answer.

Andrew Cowan

Posts: 4
Nickname: methusaleh
Registered: Feb, 2003

Re: String manipulations Posted: Jan 9, 2004 3:40 AM
Reply to this message Reply
From what Kishori Sharan has said about the functionality of abs() it sounds like the function is not fullfilling its contract. I would have thought that an int version of the abs function would throw an overflow exception when passed -128 as an parameter.
Is this sort of thing normal in Java and/or other modern languages?

Flat View: This topic has 8 replies on 1 page
Topic: Detecting Changes in a JTextField Previous Topic   Next Topic Topic: Swing

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use