+ 8
Reference type
I am sorry for repeating the question does the string have a reference type or no?? when I wrote sorting algorithms someone told me you don't have to return a value.... look at the answer below https://www.sololearn.com/discuss/1461519/?ref=app s/he said that the array does not have to be returned because it is reference type but the String have to be returned because it is a primitive type I ask for an explicit define of reference please https://code.sololearn.com/cw81bEZ2qe1q/?ref=app does this code show the defence between reference and value type ?
4 Answers
+ 4
String is not a primitive type, but a reference.
However, String is immutable = cannot be changed. Therefore, if you want to replace characters or sort it, you create a new instance.
All types except primitive types (boolean, char, byte, int, long, short, float, double) are in Java references.
+ 3
michal please does the previous code show the difference between......
String can not be changed but it is reference type I did not understand I supposed cannot be changed mean it is not reference
+ 3
The first method simply assigns a new value to the parameter. It takes a new string and places it into 'in'.
You achieve similar behaviour if the second method does in=new String[]{"abada"}. The value of the array in main will remain the same.
You should look at it a bit like at pointers. in and x are different variables pointing to the same string. When you do in="abada", you just make the pointer point somewhere else.
in and y are also different variables pointing to the same array, and changing the array's content will reflect at all pointers to it.
+ 3
so it is like a pointer ... the name of an array is the address of it
so that the method affected on y[]
Is it true the reference is not related to affection