Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

= operator in java

can somebody explain me why it's possible to do:

String s = "foo";

how is this possible without operator overloading (in that case the "=")

I'm from a C++ background so that explains...

like image 601
allaire Avatar asked Dec 22 '22 05:12

allaire


2 Answers

In this case there is no overloading. The java piece that differs from C++ is the definition of "" - The java compiler converts anything in "" into a java.lang.string and so is a simple assignment in your example. In C++ the compiler converts "" into a char const * and so needs to have a conversion from char const* to std::string.

like image 124
mmmmmm Avatar answered Jan 05 '23 00:01

mmmmmm


This assigns a simple literal of type String to s

In Java Strings are immutable, if you need to define a constant value you would use the final keyword.

like image 44
stacker Avatar answered Jan 04 '23 23:01

stacker