Reputation: 133
I have a class :
class Employee{...}
then :
Employee aEmployee = new Employee(...);
int i = 10;
String str = aEmployee + i;
it generates a ERROR when compiled, Why ?
EDIT:
I didn't override the toString()
method in the Employee
class, but if I try this:
Employee aEmployee = new Employee(...);
String h = "hello";
String str = aEmployee + h;
this time will be fine, both compiling and running.
So: why is it OK after changing the int
variable i
to a String
variable h
?
Upvotes: 0
Views: 204
Reputation: 718708
The reason that the first example is failing to compile is because of the nature of the +
operator. In Java, +
is an overloaded operator whose meaning depends on the static types of the left and right operand sub-expressions.
If both operands are both primitive numeric types (or their wrapper types), the operator is a numeric addition.
If either or both operands are Strings, the operator is a String concatenation.
If neither of the above is true, the operator is not defined - a compilation error.
In your first example, Employee
is neither a String or a number, and i
is not a String either, so the +
operator is not meaningful.
In the second example, you have changed the second operand to a String, so the operator is now recognized as a String concatenation. In fact, the Java treats this as equivalent to:
String str = aEmployee.toString() + h;
As a corollary, you could have made the first example compile by changing it to:
String str = aEmployee.toString() + i;
Upvotes: 0
Reputation: 4114
Employee aEmployee = new Employee(...);
aEmployee is an object reference not an String .so you can not concatenate it .
to combine you need to convert object to toString() then can concatenate.
Upvotes: 0
Reputation: 4171
May be
String str = aEmployee.EName + i;
Where EName is a property of Employee Class and of type string
Upvotes: 1
Reputation: 878
Because aEmployee
is a object of the Employee
class so that object not belongs to the String class. if you need to concatenate with string you have to use .toString()
method.
String str = aEmployee.toString() + i;
Upvotes: 0
Reputation: 33534
Employee aEmployee = new Employee(...);
aEmployee
is an Object Reference Variable of Type Employee pointing to Employee object on the heap.
String str = aEmployee + i;
In the above statement you are trying to concatenate a String and an Object Reference Variable.
Try doing this.....
String str = aEmployee.toString() + i;
If you have not overridden the toString()
method in Employee class, then you will get something like this, getClass().getName()@hashCode + i
Upvotes: 0
Reputation: 347184
aEmployee
is not a String
it's an Object
.
Without knowing how your Employee
object is set up, it's difficut to provide you with a suitable solution, but ...
String str = aEmployee.toString() + i;
Will stop the compiler from complaining, but may not give you the result you're expecting.
You'd be better trying to perform this action with a known property of theEmployee
String str = aEmployee.aMethodThatReturnsString() + i;
Upvotes: 4
Reputation: 42849
Employee
is not a String
, and cannot be concatenated using the +
operator.
In fact, in Java, there is no such thing as operator overloading (besides a few that are baked into the language, String
is a prime example of that).
Upvotes: 4