Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strings memory model

Tags:

java

What I read about strings is when a string object is created in Java it is immutable. For example:

String s=new String();
s="abc";
s="xyz";

Does the String s no longer point to "abc"?

And another thing: what is size of s; Is the String object analogous to a char* pointer in C in terms of the memory model?

like image 387
hemanth Avatar asked Aug 06 '26 19:08

hemanth


1 Answers

No, Java String is not as char* in C. If you are looking for the analogue char[] in java is something like this.

String is a class that wraps char array and provides a lot of functionality. Moreover it is immutable, i.e. you cannot change its content. You can only create another string. Additionally String is final, so you cannot subclass it.

String is special class. Only string supports operator (+). All other classes do not support operators at all, even primitive wrappers (Integer, Double etc). Presence of string constant in code "foobar" invokes java.lang.String constructor.

like image 133
AlexR Avatar answered Aug 09 '26 08:08

AlexR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!