Author | Topic: Important Question From MarcusGreen |
Bidyut unregistered |
posted May 08, 2000 05:09 AM
This is a Question From MarcusGreen Mock Exam-1. What will be the Out Put if the following code is written in Main Method ? String s=new String("Bicycle"); int iBegin=1; char iEnd=3; //seeThis is char and Not int System.out.println(s.substring(iBegin,iEnd)); 1) Bic See for More Questions this Link
|
Ajay Kumar greenhorn |
posted May 08, 2000 07:30 AM
Hi, Well the answer is right and the extract from JLS is produced below for reference. 20.12.32 public String substring(int beginIndex, int endIndex) The result is a newly created String object that represents a subsequence of the character sequence represented by this String object; this subsequence begins with the character at position beginIndex and ends with the character at position endIndex-1 . Thus, the length of the subsequence is endIndex-beginIndex. If beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex, then this method throws an IndexOutOfBoundsException. Examples: "hamburger".substring(4, 8) returns "urge"
Thanks [This message has been edited by Ajay Kumar (edited May 08, 2000).]
|
maha anna bartender |
posted May 08, 2000 08:04 AM
Bidyut, As far as I know, this is the ONLY strange method to which we have to be careful about the 2nd index. All other methods from String class adhere to indexes (0,1,2,3..) pattern. In other words .charAt(3) means it is char at 0..1..2..(3) , which means the forth char. regds
|
Rajani Deshpande greenhorn |
posted May 08, 2000 11:34 AM
Hi, My q is does the char get promoted to int? So that means its legal to define a variable char as char myInt = 10; I have compiled this and it compiles perfectly fine.
|
maha anna bartender |
posted May 08, 2000 11:55 AM
Yes. All 'int' literals are assignable to lesser range primitive types char/short/byte provided the value of the 'int' literal is within the range of the left hand side var type. Here char range is from 0 to (2^16)-1. So value 10 is within this range. So this is valid statement. Also note that this rule is NOT APPLICABLE for method invocation. regds [This message has been edited by maha anna (edited May 08, 2000).]
|
Suresh greenhorn |
posted May 08, 2000 11:05 PM
Maha, one small correction! That rule works for method invocation too!! I tried this program and it works!! class TestProgram The output is:
|
Suresh greenhorn |
posted May 08, 2000 11:10 PM
Hey sorry for my previous post!! That was wrong!! That works only when you send those variables!! It doesn't work when you pass the literals as testMethod(100, 67). Sorry again!! - suresh.
|
maha anna bartender |
posted May 09, 2000 04:30 AM
That's ok Suresh. This rule is for 'int' literals. regds maha anna
|
| | |