In software development, the reuse of code is a good practice. In addition, collaboration among programmers is almost the norm. However, in a learning environment for an assignment, lab or exam you might be required to write your program code individually (e.g. your Java program) from scratch and copying other people's code might not be allowed. The purpose of the exercise might be for you to come up with the program code yourself through critical thinking and individual experimentation.
Where an assignment or lab permits copying or modifying code by other people, it is important to cite where you got the code from. You may be charged with plagiarism if you fail to do so. Plagiarism can be broadly defined as taking credit for academic or intellectual work that is not your work. It is considered a serious academic offense at Langara and at other post-secondary institutions.
An added benefit of citing correctly is that it makes your code easier to maintain.
Remember, your instructor is the final expert in this matter. When in doubt, consult your instructor, especially as to whether any reference to other work is allowed.
As a student, it can be difficult to to know what resources need to be cited and what resources don't. Generally, if you used a function or algorithm that you did not create and it came from someone else, then this should be cited so as to give the creator credit. This applies also to code given in class and to code copied from a textbook.
Changing variable names or any non-algorithm elements (often referred to as cosmetic) does not make the code yours. You still need to cite where you got the code.
Ideas and programs that are "common knowledge" do not generally need to be referenced or cited. If there is only one way to program for a specific task and this is so commonly used, then it may not need to be cited. A good example of this is a "hello world" program such as the following (in Java):