3. String in JAVA

JAVA Tutorial (6 Part Series)

1 1. Hello World in JAVA
2 2. Variables, Datatypes, Operators in JAVA
2 more parts…
3 3. String in JAVA
4 4. Operators, Operands, Expression, Operator Precedence
5 5. If-then block in JAVA
6 6. Expressions, Statements, if-then-else , Methods, Method Overloading

A string is a sequences of characters. In the case of the char, char contains a single character only.
A String can contain a sequence of characters.
Exmaple:
String name = "Rittwick Bhabak"

3 Strings in Java are Immutable

String name = "Rittwick";
String name = name + "Bhabak";

Enter fullscreen mode Exit fullscreen mode

In the above code, name doesn’t get appended the value “Bhabak” instead a new String is created which consists of the previous value of name plus “Bhabak”.
The net result is same, name has the right values, however, a new String got created and the old one got discarded.
So, String is immutable in Java.
There are a class in Java called StringBuffer which can be changed.

原文链接:3. String in JAVA

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容