| Author |
some questions,please
|
andy wang
Greenhorn
Joined: Apr 30, 2002
Posts: 17
|
|
The 1st question: The String class has a method equals(Object ob),it takes an Object argument as its parameter. One of the qualifications which make the method returns true is that: ob instanceof String returns true. As the String class is final,does it mean that the ob must be an instance of the string class? Because A instanceof B is true when the A is an instance of class B or class B's subclass. The 2nd question: How does the method compareTo(String str) work? it's puzzle to me. The 3rd question: The method indexOf(int ch) takes an int as its parameter,but when i tried this code it prints -1 but when i tried this code: it prints 4 I wonder why the method can not recogenize an int value and when the method will return -1? The 4th question: can you explain how does the code work? Thank you very very much! [ April 30, 2002: Message edited by: andy wang ] [ April 30, 2002: Message edited by: andy wang ]
|
Name:Andy Wang<br />From:SouthEast University China.<br />Major:Communication Engineering
|
 |
brent spearios
Ranch Hand
Joined: Apr 10, 2002
Posts: 48
|
|
Ok ,lets start at the begining. As String is a final class it cannot be subclassed and as a result the object must be of type string The compare to method returns a positive or negative value if it is not valued to be true. So if you do "test".compareTo("t"); it will return a positive number if it were "t".compareTo("test"); it will return a negative number if they are the same then it returns 0. I remeber it by substituting compareTo with a - so test-t=est thats postive and t-test=-est is negative if you know what i mean, indexOf returns a -1 when it has not found the value in () so -1 is return. Try running this bit of code it will help: char t='t';//value of t as an int is 116 System.out.println((int)t); System.out.println ("erty".indexOf (116)); hope this helps
|
 |
shailesh kendale
Greenhorn
Joined: Apr 29, 2002
Posts: 1
|
|
answer for 3rd question: indexOf function of string accepts ASCII value of the character. if u try String s3=new String("012345"); System.out.println(s3.indexOf(52)); it will give u value as 4 because 52 is ASCII value of 4
|
 |
andy wang
Greenhorn
Joined: Apr 30, 2002
Posts: 17
|
|
|
what about the 4th question?
|
 |
brent spearios
Ranch Hand
Joined: Apr 10, 2002
Posts: 48
|
|
|
There is no problem with the last question, it runs fine
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
Originally posted by brent spearios: There is no problem with the last question, it runs fine
Brent is right - the code for the 4th question compiles and runs fine. There is no RuntimeException caused by that code. Corey
|
SCJP Tipline, etc.
|
 |
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
|
|
The 1st question: The String class has a method [b]equals(Object ob),it takes an Object argument as its parameter. One of the qualifications which make the method returns true is that: ob instanceof String returns true. As the String class is final,does it mean that the ob must be an instance of the string class? Because A instanceof B is true when the A is an instance of class B or class B's subclass. [/b] First of all ob is not instance of any class. It is object reference of Type SomeClass. So now let us frame your question again. Que: Does it mean that the ob must be of type String class? No, it must not to be type of String class. As String is subclass of Object class and any Object class ref var might be holding refernce to some Sring class object. Exa: String str = new String("andy"); Object obj = new String("andy"); if(str.equals(obj)) S.o.p("yes it is true"); And if ob is not referencing to String then in that case it will return false. HTH CMIW
|
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
|
 |
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
|
|
The 3rd question: The method [b]indexOf(int ch) takes an int as its parameter,but when i tried this code it prints -1 but when i tried this code: it prints 4 I wonder why the method can not recogenize an int value and when the method will return -1? [/b] indexOf() method takes int as parameter but it expects character. As char are unsigned integer. form API: ------------------------------- indexOf public int indexOf(int ch)Returns the index within this string of the first occurrence of the specified character. ------------------------------- [B] The 4th question: can you explain how does the code work? [/B] String / StringBuffer index starts from 0. So in the second case it should throw IndexOutOfBoundException. As at index 4 there is no Char to append OR in other words StringBUffer obj length is 3. append() does not add blanks by default. HTH CMIW
|
 |
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
|
|
The 2nd question: How does the method [b]compareTo(String str) work? it's puzzle to me. [/b] hope it helps HTH CMIW
|
 |
andy wang
Greenhorn
Joined: Apr 30, 2002
Posts: 17
|
|
thank you very much! i tried the 4th question again,and it is the same as it was. i tried the code as follows: at first i tried: then i tried: i wonder why the compiler can reciganize the index "4" but the code can not run? wait for your answer  [ April 30, 2002: Message edited by: andy wang ]
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
I don't understand the problem that you're having. In the first line, you declare a new StringBuffer object: At that point, inserting something into position 4 would cause an error. However, in the next line, you append 3 more characters to the StringBuffer: Now, there are 6 characters in the StringBuffer ("abc123") so inserting more characters into the 4th spot is now valid. Exeecuting the final line: produces the output: "abc112323" If you're having problems running this, please let us know what the error is, not just that you're getting one. If you omit the second line, however, and try to execute the third line right after creating the StringBuffer, you'll get a StringIndexOutOfBoundsException. Take a look at the API Spec for StringBuffer to get more detailed information about the insert method. I hope that helps, Corey
|
 |
andy wang
Greenhorn
Joined: Apr 30, 2002
Posts: 17
|
|
maybe i didnt express my question clearly,now i am trying again: at first i tried this code: StringBuffer s=new StringBuffer("abc"); System.out.println(s.insert(3,"123")); the result is "abc123" secondly,i tried this code: StringBuffer s=new StringBuffer("abc"); System.out.println(s.insert(4,"123")); the code can be compiled but it will throw an Exception at run time. My question is how can the compiler recoganize the index '3' and '4' as the length of the original StringBuffer is 3 (and the index of each character is 0,1,2,)? Thank you very much? [ April 30, 2002: Message edited by: andy wang ]
|
 |
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
|
|
Originally posted by andy wang: My question is how can the compiler recoganize the index '3' and '4' as the length of the original StringBuffer is 3 (and the index of each character is 0,1,2,)?
Compiler's job is to make sure that you are using correct syntax. For a method call, right thing is that actual param type should match the defined param type. In your case that is correct: System.out.println(s.insert(4,"123")); i.e 4 is int, second param is String. thats why compile time error is not generated, but at runtime it gives you error as JVM finds that length is only 3. Hope now you have got it ..... compiling a program does not mean that it will run correctly
|
 |
wang jie
Greenhorn
Joined: May 01, 2002
Posts: 8
|
|
why the code insert(3,"123") can run but the code insert(4,"123") can not? thank you !
|
 |
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
|
|
Hello wang, This is from the API for StringBuffer.insert thus new StringBuffer("123").insert(4, "456");will give a RuntimeException because the length of the StrinBuffer is 3, but the insertion is at 4.
|
SCJP2. Please Indent your code using UBB Code
|
 |
wang jie
Greenhorn
Joined: May 01, 2002
Posts: 8
|
|
thank you! where can i download the API for free?
|
 |
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
|
|
|
go to "SCJP FAQ"
|
 |
 |
|
|
subject: some questions,please
|
|
|