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...
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...
It is an assignment operator in java which is used to assign the value to the declared type, where no operator overloading is required. Even in c++
It's not as easy as this, but think of all objects as pointers.
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.
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.
"Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";
"is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);