...
String
is associated with string literal in the form of double-quoted texts such as "Hello, world!". You can assign a string literal directly into aString
variable, instead of calling the constructor to create aString
instance.- The
'+'
operator is overloaded to concatenate twoString
operands.'+'
does not work on any other objects such asPoint
andCircle
. String
is immutable. That is, its content cannot be modified once it is created. For example, the methodtoUpperCase()
constructs and returns a newString
instead of modifying the its existing content.
Java String vs Python string
In the following example, we initialize an integer to zero, then convert it to a string, then check to see if it is empty. Note the data declaration (highlighted), which is necessary in Java but not in Python. Notice also how verbose Java is, even in an operation as basic as comparing two strings for equality.