2

If String is not a Primitive datatype in Java and if Java does not support operator overloading then how is this possible

String x = "foo";
System.out.print( x + "bar");

It Prints foobar. How?!

Cybermonk
  • 514
  • 1
  • 6
  • 28
  • 19
    Because the language designers accepted that operator overloading is useful, even though they wouldn't trust it to users. – Deduplicator Jun 26 '14 at 11:15
  • @Deduplicator aptly said... – madteapot Jun 26 '14 at 11:15
  • @Deduplicator - they could have accepted string[index] as well. :) – TheLostMind Jun 26 '14 at 11:17
  • @TheLostMind: How often do you use `+` on strings and how often would you use `string[index]`. Not worth the hassle IMHO – gexicide Jun 26 '14 at 11:20
  • @gexicide - For people who have come from C++ background, string.charAt() is ridiculously long to type ('ch'+ctrl+space isn't small either). I would prefer string[index] any day. – TheLostMind Jun 26 '14 at 11:21
  • @TheLostMind: People coming from C++ will find a lot of things in Java ridiculously long to type ;). Typedefs for long generic classes would be nice for example, we can go on like this forever... – gexicide Jun 26 '14 at 11:24
  • @gexicide - ya.. ya.. every labguage has its own advantages... :) – TheLostMind Jun 26 '14 at 11:25

0 Answers0