Please answer the following questions are question relating to java. 1. Declare
ID: 3696265 • 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. Declare a string variable: String s1 = new String(“hello”); Will the following two statements return true or false? Why?
s1.equals(“hello”)
s1 = = “hello”
Ans:
String s1=new String("hello"); :--- This statement will create two objects.One would be the same string literal as above and would be placed in String constant pool.Second object would be created and placed in the normal(non pool) memory and s1 would refer to it.The second object would act as a normal object in a heap.
String s1="hello"; :---Here string literal "hello" will be created and this would be placed in the String constant pool.Hence one object is created with s1 as a reference variable to it.Also,when any other reference variable is created(n the same manner as above) in future with the same value as "hello",it would refer to the already created String literal present in the constant pool (i.e. one which s1 is already reffering to) rather than creating a new one.
s1.equals("Hello") will return true as equals function checks the individual characters in both the reference variables.
s1 == "Hello" will return false.In this case , "Hello" creates a literal in the pool which has a different memory location than S1's heap memory.
2. What’s the difference between non-static method and static method?
Ans:: 1] Static method can be called without creating any object e.g. Collections.sort(). This makes static method useful utility.Static methods are also pretty useful on several design pattern including Factory and Singleton.
To call non static method in Java you need to instantiate an object .
2) We can not access a non static variable inside any static method in Java.
but we can access static variables or call static method from a non static method without any compile time error.
3) We can not override static method in Java. They are bonded during compile time using static binding. Though you can create a similar static method in sub class, that is know as method hiding in Java.
4) static methods are known as class methods, if you synchronize static method in Java then they get locked using different monitor than non static methods e.g. both static and non static methods are locked using different monitor in Java.
5) Static methods are commonly used inside utility classes e.g. java.util.Collections or java.util.Arrays, because they are easier to access, as they don't need an object. One of the popular example of static method is though main method, which act as entry point for Java programs.
3. About argument promotion and casting, what’s the promotion rule?
Argument Promotion is converting an argument’s value, if possible, to the type that the method expects to receive in its corresponding parameter. For example, a program can call Math method sqrt with an int argument even though a double argument is expected.
4. When and why do we need a Random object with a seed value?
A Random is intended to be random. Usually you want two Random to produce different numbers rather than to produce the same numbers.
5. What is Shadowing in Scope of Declaration? How to call shadowed variable in the main method?
A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope , the one in the outer scope is shadowed.
6. What is method overloading?
Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different. In the last tutorial we discussed constructor overloading that allows a class to have more than one constructors having different argument lists.