(a) There are two ways to insert comments into a Java program. What are they, an
ID: 3868144 • Letter: #
Question
(a) There are two ways to insert comments into a Java program. What are they, and what are their advantages and disadvantages?
(b) In Java, a non-void method must have a return instruction somewhere in the body. But a situation can arise where the Java compiler complains that a return instruction is needed, even when the method already contains one. Why does this happen and how do you resolve the problem?
(c) What is the difference between an internal and an external method call? In what situation would only internal calls be needed?
(d) If a method given an ArrayList as a parameter calls a second method giving it the same ArrayList, and that second method deletes one of the elements, will this change be visible in the first ArrayList? Explain your answer.
(e) How are static variables and methods created in Java? What happens if a non- static method of a class tries to access a static variable of that class? What happens if a static method references a non-static variable? What happens to the static variables of a class once the class has no instances?
Explanation / Answer
1)
and
The first form is called Javadoc. You use this when you're writing formal APIs for your code, which are generated by the javadoc tool. For an example, the Java 7 API page uses Javadoc and was generated by that tool.
Some common elements you'd see in Javadoc include:
@param: this is used to indicate what parameters are being passed to a method, and what value they're expected to have
@return: this is used to indicate what result the method is going to give back
@throws: this is used to indicate that a method throws an exception or error in case of certain input
The second form is a block (multi-line) comment. You use this if you want to have multiple lines in a comment.
Since Javadoc is the more descriptive of the two, and you can generate actual documentation as a result of using it, using Javadoc would be more preferable to simple block comments.Another nice benefit of using Javadoc instead of simple block comments is that when you put a Javadoc comment before a Java element (f.ex. a method signature, a field declaration, a class etc.) this enables IDEs - at least Eclipse for sure - to show your comment (f.ex. in a tooltip) when you move the cursor - or hover with the mouse - on a reference to that Java element.
b) It happens in case of unreachable statements.Like if you place return statement inside if, while ,etc
For example
The if statement, whether or not it has an else part, is handled in an unusual manner.In foo4() the compiler is not so clever that it understands that the function always returns 5. It just checks that not all code paths return something.
c)In C, and its syntax buddies, when a file is compiled it doesn't need to contain every function/method that it references. In other words, you can call some "mthd(a,b)" even if it doesn't appear in your program.
If you define: "function mthd(int a, int b) { ... }" in that file, than any call to it is an internal method call.
If you don't define it there, but you include another file that defines it, or a header that calls a library that defines mthd(a,b) for you, then that's an external method call.
Internal means called in File A and defined in File A. External means called in File A but defined in some other file (which is linked somehow to File A at compile or run time).
d)It will be visible in first ArrayList. The reason is that when you pass an ArrayList as argument, the called method can change the content of the array. The ArrayList contains references to Objects.The = operator in Java will just copy the ArrayList reference (the same is for all objects).. If you want to avoid that some class will change the content of your ArrayList you have to give back Copy of your ArrayList where all objects inside are clone()s of the objects in your list.
Use object.clone()
e)The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.
The static can be:
Java static variable
If you declare any variable as static, it is known static variable.
Static variable example:
static String college ="ITS";
Java static method
If you apply static keyword with any method, it is known as static method.
Example:
static void change(){
college = "BBDIT";
}
A static function can not access non-static member variables of class because non-static member variables of a class always belong to an object – meaning that every object has it’s own personal copy of non-static member variables (also known as instance variables). And, static functions have no object to work with since they belong to the class as a whole. Remember that you can call a static function without an object – and for that exact reason a static function can not call instance variables.
if a non- static method of a class tries to access a static variable of that class there is no issue.
for example
class Student9{
int rollno;
String name;
static String college = "ITS";
Student9(int r, String n){
rollno = r;
name = n;
}
void display (){System.out.println(rollno+" "+name+" "+college);}
public static void main(String args[]){
Student9 s1 = new Student9 (111,"Karan");
Student9 s2 = new Student9 (222,"Aryan");
s1.display();
s2.display();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.