File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Exam #1, Q 26 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Exam #1, Q 26 " Watch "Marcus Exam #1, Q 26 " New topic
Author

Marcus Exam #1, Q 26

Wasim Ahmed
Ranch Hand

Joined: Jan 31, 2001
Posts: 90
Here is the code.
String s=new String("Bicycle");
int iBegin=1;
char iEnd=3;
System.out.println(s.substring(iBegin,iEnd));
Output is ic
I think signature of substring method is substring(int x,int y);
How is the char got converted to int?
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
char is implicitly converted to int like byte and short.
Jimmy Blakely
Ranch Hand

Joined: Jul 10, 2001
Posts: 57
With ...
char iEnd = 3;
.... an int is implicitly converted to a char.

With ...
s.subString(iBegin, iEnd)
...the variable iEnd ( as long as its an int or something narrower) is converted (implicit widening) to an int.
Hope that helps
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
I think the output should be bc and not ic... correct me if I am wrong
Originally posted by Wasim Ahmed:
Here is the code.
String s=new String("Bicycle");
int iBegin=1;
char iEnd=3;
System.out.println(s.substring(iBegin,iEnd));
Output is ic
I think signature of substring method is substring(int x,int y);
How is the char got converted to int?

Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Roopa
Here is the description of the substring method form the API.

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Keep in mind the index starts at zero not 1. So the first character 'B' is at index 0 not index 1. So the code will print "ic", becasue it takes a substring starting at index 1 (which is the letter 'i') and going to the character at index 3 - 1 (which is the letter 'c').
Hope this helps you out

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Percy Densmore
Ranch Hand

Joined: Mar 06, 2001
Posts: 214
Good explanation, Dave
Percy
Metal Zhang
Ranch Hand

Joined: Aug 05, 2001
Posts: 52
You can try this code:

The output will be:

a = (heart-shape signal)
b = 3

Now,we can draw a conclusion:If you need the char variable to appear as an integer,it will be what you want.And by default,it is still a char variable.
Originally posted by Wasim Ahmed:
Here is the code.
String s=new String("Bicycle");
int iBegin=1;
char iEnd=3;
System.out.println(s.substring(iBegin,iEnd));
Output is ic
I think signature of substring method is substring(int x,int y);
How is the char got converted to int?

------------------
Java lover from hell!
[This message has been edited by Metal Zhang (edited August 28, 2001).]


Java lover from hell!
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
"I think the output should be bc and not ic... correct me if I am wrong"
Why speculate on what it "should" be when you can compile it and know what it will be?
Marcus
------------------
http://www.jchq.net Mock Exams, FAQ, Tutorial, Links, Book reviews
=================================================
Almost as good as JavaRanch
=================================================


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Exam #1, Q 26
 
Similar Threads
Question from Marcus Green's exam
String method
Output of code snippet
Doubt in Marcus Test 1
String Tokenizer