0

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?

1
  • Override the Object.toString() method in Employee class. Commented Aug 29, 2012 at 3:12

7 Answers 7

4

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Essentially, the compiler has looked at the two values & decide that it can use the .toString() method of the aEmployee to concatenate the two values. This is a feature of the language. Before, it looked at te to values & couldn't duduce how he two values should be added together
4

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).

Comments

1

May be

String str = aEmployee.EName + i;

Where EName is a property of Employee Class and of type string

Comments

0

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

2 Comments

He is actually concatinating an Employee to an int
@ Ya thats what my answer explains...but i digged in to explain what actually aEmployee is....
0

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;

Comments

0
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.

Comments

0

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;  

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.