operator overloading in java
I heard the java doesnt support operator oveloading as c++
But look at this code
String str="abcd"+"PQRS";
or
String str="ABCD"+5;
or
boolean var=true;
System.out.println("ABCD"+var);
is it operator oveloading
I am confused plzzzzzzzzzzz help
This question is asked in interview
Thanks
[361 byte] By [
it_shailua] at [2007-11-27 10:58:35]

True answer: Yes. Sun cheated when they made Java and included the + operator for strings because it was common practice; they overloaded it to be the same as:
public String operator+(String str1, String str2) {
StringBuffer s = new StringBuffer();
s.append(str1);
s.append(str2);
return s.toString();
}
Sugar-coated answer: When using "+" with strings, it is technically a concat operator, not an addition operator, so it isn't overloading.
> I am confused plzzzzzzzzzzz help
I am confused as to why anyone would want to write "plzzzzzzzzzzz" instead of "please".
~
Java does not allow "user defined" operator overloading.
Parzzzzeltounge.
*ducks and hides*
sorry for plzzzzzzzzzzzzz
Please
+ has given different meaning
but still its not operator ovrloading
its not logical
But thanks for ur valuable help friend
No. Thats not what anybody said. It IS operator overloading. The almighty and high Sun developers are the only people "responsible" enough to deal with operator overloading. The rest of us clowns are too irresponsible and can't be trusted.
Essentially, everyone knows what it means to add two strings together. But APPARENTLY, with user-defined classes,
"Foo + Bar;" is WAY too ambiguous; "Foo.add(Bar);" is the preferred method.
> its not logical
See reply #3.
> But thanks for ur valuable help friend
This valuable Ur?
http://www.mnsu.edu/emuseum/archaeology/sites/middle_east/ur.html
~
The LANGUAGE defines the same operator for several types (eg. + for String, byte, short, int,...), but the programmer has no way to define an operator.
> > public String operator+(String str1, String str2) {
>StringBuffer s = new StringBuffer();
> s.append(str1);
>s.append(str2);
> return s.toString();
> }
>
No, I don't think it works like that. The compiler will just replace ALL +-operations in one expression where at least one string is involved (must it be the first operand? I can't remember, IDE will tell you ;-) ) with StringBuffer (or StringBuilder since jdk1.5) and 'append' calls. There is no method! Eg.
String s = "foo" + 4 + "bar";
will be transformed to:
StringBuilder sb = new StringBuilder("foo");
sb.append(4);
sb.append("bar");
More or less like that, but the point is that even with more than 2 Strings just one StringBuilder is used.
-Puce
Message was edited by:
Puce
Pucea at 2007-7-29 12:17:06 >

Puce: I am pretty sure there was some performance issue...
Consider "A" + "B" + "C"
As I recall, I think it does the equivalent of operator+("A", "B") to yield "AB"
Then it does operator+("AB", "C"); to yield "ABC"
I could be wrong.
Taking the following code:
public class Foo {
public static void main(String args[]) {
System.out.println("A" + "B" + "C");
long l = System.currentTimeMillis();
System.out.println("<" + l + ">");
}
}
and compiling & decompiling it with javac Foo.java && javap -c Foo results in the following output:
public class Foo extends java.lang.Object{
public Foo();
Code:
0:aload_0
1:invokespecial#1; //Method java/lang/Object."<init>":()V
4:return
public static void main(java.lang.String[]);
Code:
0:getstatic#2; //Field java/lang/System.out:Ljava/io/PrintStream;
3:ldc#3; //String ABC
5:invokevirtual#4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8:invokestatic#5; //Method java/lang/System.currentTimeMillis:()J
11: lstore_1
12: getstatic#2; //Field java/lang/System.out:Ljava/io/PrintStream;
15: new#6; //class java/lang/StringBuilder
18: dup
19: invokespecial#7; //Method java/lang/StringBuilder."<init>":()V
22: ldc#8; //String <
24: invokevirtual#9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
27: lload_1
28: invokevirtual#10; //Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
31: ldc#11; //String >
33: invokevirtual#9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
36: invokevirtual#12; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
39: invokevirtual#4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
42: return
}
This shows two interesting facts:
- "A"+"B"+"C" is reduced to "ABC" at compile time.
- "<" + l + ">" is generated using a single StringBuilder only.
Jad produces the following code, btw, which is easier to read:
// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3)
// Source File Name:Foo.java
import java.io.PrintStream;
public class Foo
{
public Foo()
{
}
public static void main(String args[])
{
System.out.println("ABC");
long l = System.currentTimeMillis();
System.out.println((new StringBuilder()).append("<").append(l).append(">").toString());
}
}
> True answer: Yes. Sun cheated when they made Java and
> included the + operator for strings because it was
> common practice; they overloaded it to be the same
> as:
The true answer is NO there is no such thing as operator overloading in java. Regardless of whether you are discussing strings or not.
As noted by the OP they are referring to operator overloading as it occurs in C++.
C++ and java both allow one to add doubles and integers together but no one claims that that is operator overloading.
Many languages allows strings to be concatenated and none of those claim that they allow operator overloading.
Java allows string concatenation just as those other languages do and that is specifically how it is defined.
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#39990
Note that "string" concatenation in C++ is specifically defined via the standard library to use operator overloading (again specifically defined by the language) to implement a class which allows string concatenation. That does not mean that it is in any way the same as concatenation in java.
> I heard the java doesnt support operator oveloading
> as c++
> But look at this code
>
> String str="abcd"+"PQRS";
> or
> String str="ABCD"+5;
> or
> boolean var=true;
> System.out.println("ABCD"+var);
>
> is it operator oveloading
> This question is asked in interview
>
Operator overloading is a defined part of the C++ language. The string class in C++ is implemented using that feature of the language.
String concatenation is a defined part of the java language.
The two are not the same.