开发者

= 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 c开发者_如何转开发ase the "=")

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


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.


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++


The API says:

"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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜