Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does this method work?

 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The output is "his_".

What is the variable buf[] for? My teacher told me to put it but I don't know why!

Another thing I don't understand is why does it stop at the first "_"? Shouldn't it display "his_i"? (i.e. Up until index 5 since int end=5)
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Olivier Legat:

What is the variable buf[] for? My teacher told me to put it but I don't know why!


The variable "buf" with the pair of square brackets "[]" means an array! A set of contiguous memory locations being used to store the similar kinda data value. Here in this case, you use a container to store a set of characters. so you need an array of characters.

In Java, you declare the array in this way!


Another thing I don't understand is why does it stop at the first "_"? Shouldn't it display "his_i"? (i.e. Up until index 5 since int end=5)


Well, end is 5 and start is 1. so, (end-start) will give you 4 (5-1=4 ). As the arrays always start with 0 instead of 1, you will get upto 4.

Does that help?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, in nearly all API calls that need an end index, this end index is exclusive. So if you want characters 1 to 5, you need to give the end as 6.
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YAH! Helps loadz. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic