Please answer the following questions are question relating to java. 1. Declare
ID: 3695468 • 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
Answer :
1) First statement is true. second statement would be false.
2)Java is a Object Oriented Programming(OOP) language, which means we need an object to access any method or variable inside or outside the class. However there are some special cases where we don’t need any object, in order to access static methods we don’t need any object.A static method belongs to the class itself and a non-static (aka instance) method belongs to each object that is generated from that class. If your method does something that doesn't depend on the individual characteristics of its class, make it static (it will make the program's footprint smaller). Otherwise, it should be non-static.
3) The promotion rules apply to expressions containing values of two or more primitive types and to primitive-type values passed as arguments to methods. Each value is promoted to the "highest" type in the expression.
4) A Random is intended to be random. Usually you want two Random to produce different numbers rather than to produce the same numbers.
This is my code to generate random numbers using a seed as an argument.
5)
Two variables or procedures with the same name cannot be declared in the same scope. The result is a name conflict. The compiler reports an error when it encounters a name conflict in a script.
Variables or procedures declared in different scopes can have the same name. A variable or procedure of the same name declared at a scope outside of this innermost visible scope is not accessible. This effect is called shadowing: the outer declarations of the name are shadowed, or made invisible, by the inner declaration.
6)If a class have multiple methods by same name but different parameters, it is known as Method Overloading.
Thanks