Please answer the following questions are question relating to java. 1. Declare
ID: 3694437 • Letter: P
Question
Please answer the following questions are question relating to java.
1. Declare a string variable: String s1 = new String(“hello”); Will the following two statements return true or false? Why?
s1.equals(“hello”)
s1 == “hello”
2. What’s the difference between non-static method and static method?
3. About argument promotion and casting, what’s the promotion rule?
4. When and why do we need a Random object with a seed value?
5. What is Shadowing in Scope of Declaration? How to call shadowed variable in the main method?
6. What is method overloading?
Explanation / Answer
1) The first one returns two true and the second one returns false. .equals() tests for value equality and == tests for reference equality.
2) A static method belongs to the class itself, but the non-static method belongs to an object instance of a class.
3) Argument promotion is converting an arguements value to the type the method expects to receive according to promotion rules. Values in an expression are promoted to the highest type in an expression.
Converting to a lower type can only be done with explicit mention of that conversion. This is called casting.
4) A seed value is a number is to initialize the random number generator. In reality this is just a pseudo random number generator. The generation of random numbers has uses in statics, for random sampling and simulation.
5) A variable is shadowed if there is another variable with the same name that is closer in scope. For example a local variable shadows an instance variable..
class temp
{
int b;
void setB(int b) {
this.b = b; //In this case the b on the right hand side referes to the input variable but on the left hand side is the instance variable.
}
}
6) Method overloading is when we create another method with the same name but different input arguements.