In this article, we will discuss a sample program on String which is commonly asked to test skill on overridden toString() method
Q) What will be output of below program ?
There are 2 classes viz.,;
- Employee POJO
- Employee main, to begin/start JVM execution
Employee.java
package in.bench.resources.interview.programs;
public class Employee {
// member variables
String name;
String address;
// getter & setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
// override toString() method
@Override
public String toString() {
return "Employee [name=" + name
+ ", address=" + address
+ "]";
}
}
EmployeeMain.java
package in.bench.resources.interview.programs;
public class EmployeeMain {
public static void main(String[] args) {
// creating employee object
Employee employee1 = new Employee();
// update employee info
updateEmployee(employee1);
// printing employee information
System.out.println(employee1);
}
// employee update operation
public static void updateEmployee(Employee employee) {
employee.setAddress("Address 1");
}
}
Output:
Employee [name=null, address=Address 1]
Related Articles:
- String length() method
- String matches(String regex) method
- String regionMatches() method (2)
- String replace(char oldChar, char newChar) method
- String replace(CharSequence target, CharSequence replacement) method
- String replaceAll(String regex, String replacement) method
- String replaceFirst(String regex, String replacement) method
- String split(String regex) method
- String split(String regex, int limit) method
- String startsWith(String prefix) method
- String startsWith(String prefix, int toffset) method
- String subSequence(int beginIndex, int endIndex) method
- String substring(int beginIndex) method
- String substring(int beginIndex, int endIndex) method
- String toCharArray() method
- String toLowerCase() method (2)
- String toUpperCase() method (2)
- String toString() method
- String trim() method
- String valueOf() method (9)
Happy Coding !!
Happy Learning !!