Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31632 > unrolled thread

If java doesn't support operator overloading, how does String + concatenation work

Started byAjay Paul <ajaykpaul@gmail.com>
First post2015-04-30 07:10 -0700
Last post2015-05-01 06:33 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  If java doesn't support operator overloading, how does String + concatenation work Ajay Paul <ajaykpaul@gmail.com> - 2015-04-30 07:10 -0700
    Re: If java doesn't support operator overloading, how does String + concatenation work Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-04-30 11:32 -0400
      Re: If java doesn't support operator overloading, how does String   + concatenation work Robert Klemme <shortcutter@googlemail.com> - 2015-04-30 23:46 +0200
    Re: If java doesn't support operator overloading, how does String + concatenation work v_borchert@despammed.com (Volker Borchert) - 2015-05-01 06:33 +0000

#31632 — If java doesn't support operator overloading, how does String + concatenation work

FromAjay Paul <ajaykpaul@gmail.com>
Date2015-04-30 07:10 -0700
SubjectIf java doesn't support operator overloading, how does String + concatenation work
Message-ID<3eceaed0-2cfa-4fac-89d6-23445a670f84@googlegroups.com>
I understand that java doesn't support operator overloading. If so, how does String + concatenation work ? 

[toc] | [next] | [standalone]


#31633

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-04-30 11:32 -0400
Message-ID<mhths3$c00$1@dont-email.me>
In reply to#31632
On 4/30/2015 10:10 AM, Ajay Paul wrote:
> I understand that java doesn't support operator overloading. If so, how does String + concatenation work ?

     Java *does* support operator overloading.  You've noted that `+' can
concatenate two Strings; it can also add two doubles, or two floats, or
two longs, or two ints.  Similarly, `<' can compare doubles, floats,
longs, ints, shorts, chars, or bytes.  `==' and `!=' can compare all
those, and can compare references also.

     However, Java does not support programmer-defined overloads: Its
operators and their overloads are built in to the language, and cannot
be extended by writing Java code.  (In a trivial sense, defining a new
class or interface extends the `==' and `!=' and `?:' operators to
apply to new reference types -- but that's a pretty weak "overload.")

     A useful way to think about this is to separate the notion of an
operator from the particular source symbol that represents it.  We
may then say that the source symbol `+' can represent any of five
different operators, depending on context:

	String concatenation
	double addition
	float addition
	long addition
	int addition

(Note that although `+' can be used with short and char and byte as
well, these types are widened to int before the operator operates,
so there's really no "short addition" operator.)

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#31636 — Re: If java doesn't support operator overloading, how does String + concatenation work

FromRobert Klemme <shortcutter@googlemail.com>
Date2015-04-30 23:46 +0200
SubjectRe: If java doesn't support operator overloading, how does String + concatenation work
Message-ID<cqfm6fFfctoU1@mid.individual.net>
In reply to#31633
On 30.04.2015 17:32, Eric Sosman wrote:

> (In a trivial sense, defining a new
> class or interface extends the `==' and `!=' and `?:' operators to
> apply to new reference types -- but that's a pretty weak "overload.")

I am not sure I agree here: for == and != it would be sufficient to be 
defined on (Object, Object) - there is no need for an overloaded version 
for each type and in fact I could not find indication in the JLS that 
there are overloaded versions let alone overloaded versions of the 
reference equality operator with different semantics.  With your 
argument each new type would create a ton of overloaded versions - for 
each combination of reference types that have an inheritance relationship.

see JLS Java SE 8 15.21.3
http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.21.3

Cheers

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#31637

Fromv_borchert@despammed.com (Volker Borchert)
Date2015-05-01 06:33 +0000
Message-ID<mhv6nr$lj4$1@Gaia.teknon.de>
In reply to#31632
Ajay Paul wrote:
> I understand that java doesn't support operator overloading.
> If so, how does String + concatenation work ? 

There is no method String::operator+() as there would be in C++.
String "+" is just "syntactic sugar", i.e. a shorthand notation
which is translated by the compiler into bytecode equivalent to
new StringBuilder().append(...).append(...).toString(). If you're
using Eclipse, get the "bytecode outline" plugin and see yourself.

-- 

"I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy@ncc1701.starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert  <v_borchert@despammed.com>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web