3

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?

0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"Presence of string constant in code "foobar" invokes java.lang.String constructor." What does it means?
s = "abc" is the same as s = new String("abc").
1

Yes, Strings in Java are immutable. That is to say, the content pointed too by a string variable cannot be changed after it has been initialized. Using your own examples to illustrate:

String s=new String();

In the above code, you have created a new, empty String and assigned it to the s variable.

s="abc";

You have created another new String, this time with content "abc". You have set the variable s to point to this new String. Your previous, empty string will get garbage collected at some point in the future.

s="xyz";

Similar to above. You have created another new String, with content "xyz", and set the variable s to point to it. The previous "abc" string will get garbage collected at some point in the future.

Note that at no point did you actually modify the empty string to become "abc", or modify the "abc" string to become "xyz". All operations on String that concatenate, convert case, or otherwise appear to modify the String actually return a new String with the function results. To illustrate:

String s = new String("Hello");
String b = s.concat(" World");
System.out.println(s); // This will NOT produce 'Hello World'

System.out.println(b); // Whereas, this will

As to the last part of your question, you would use the length() function to determine the 'size' of your string. Note that this is based on Unicode encoding, and might not be what you expect the length to be as defined in ANSI C.

Comments

0

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

Ans: No, it point to "xyz" regarding your code.

Que: what is size of s?

Ans: Its a bit tricky, "s" is the reference to "xyz". It is true for any declared variables which are referencing an object. So it is better to say, s is pointing to "xyz".

One important hint, memory of an object & referencing an object takes different memory space.

Immutable: A object is immutable if you cant change its property after instantiation.

1 Comment

so it is reference that takes 32 bit memory in compiler right??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.