Author Topic:   Important Question From MarcusGreen
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


1) Bic
2) ic
3) icy
4) error: no method matching substring(int,char)

See for More Questions this Link

Ajay Kumar
posted May 08, 2000 07:30 AM             

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)
throws IndexOutOfBoundsException

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.


"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"

Hope this helps


[This message has been edited by Ajay Kumar (edited May 08, 2000).]

maha anna
posted May 08, 2000 08:04 AM             
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.

maha anna

Rajani Deshpande
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
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.
At the same time this is NOT valid.

byte b = 1000; // Not valid
byte b1 = 100; //Valid
byte b2 = 127; //valid

Also note that this rule is NOT APPLICABLE for method invocation.
For example there is a method like the foll

void m1(byte b, char c, short s) { }

The you CAN NOT call the above method like this

m1(10,20,30); // NOT VALID

Here the 'int' literals are treated as 'int' type and you have to specifically cast them to byte/char/short.

m1((byte)10,(char)20,(short)30); // VALID

maha anna

[This message has been edited by maha anna (edited May 08, 2000).]

posted May 08, 2000 11:05 PM             
one small correction!
That rule works for method invocation too!!
I tried this program and it works!!

class TestProgram
public static void main(String[] args)
byte a = 100;
char c = 67;
testMethod(a, c);
static void testMethod(byte b, char d)
System.out.println("byte = " + b + " char = " + d);

The output is:
byte = 100 char = C

Hope it helps. - suresh.

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
posted May 09, 2000 04:30 AM             
That's ok Suresh. This rule is for 'int' literals.
maha anna