2
(a) State the difference between == operator and equals() method.
Show SolutionHide Solution↓
| equals() | == |
|---|---|
| It is a method | It is a relational operator |
| It is used to check if the contents of two strings are same or not | It is used to check if two variables refer to the same object in memory |
| Example: String s1 = new String("hello"); String s2 = new String("hello"); boolean res = s1.equals(s2); System.out.println(res); The output of this code snippet is true as contents of s1 and s2 are the same. | Example: String s1 = new String("hello"); String s2 = new String("hello"); boolean res = s1 == s2; System.out.println(res); The output of this code snippet is false as s1 and s2 point to different String objects. |
(b) What are the types of casting shown by the following examples:
- char c = (char) 120;
- int x = ‘t’;
- Explicit Cast.
- Implicit Cast.
(c) Differentiate between formal parameter and actual parameter.
| Formal parameter | Actual parameter |
|---|---|
| Formal parameters appear in function definition. | Actual parameters appear in function call statement. |
| They represent the values received by the called function. | They represent the values passed to the called function. |
(d) Write a function prototype of the following:
A function PosChar which takes a string argument and a character argument and returns an integer value.
int PosChar(String s, char ch)
(e) Name any two types of access specifiers.
- public
- private