Many people get confused about this issue, therefore IRS provides a detailed tutorial discussed many of these issues for foreigners. You may dig it out from IRS website.
One example I remember that relates to this topic is like this: Mr.A is a citizen of India. He had been studying in Canada for 1 year before he came to US as a J1. Then he will be judged as a resident of Canada, NOT India for tax purpose. He is not eligible to claim benefits from treaty between US and India, but he can claim benefits from treaty between US and Canada, if there is any.