Reference type | SoloLearn: Learn to code for FREE!


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 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 does this code show the defence between reference and value type ?

9/9/2018 10:33:04 AM


4 Answers

New Answer


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.


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


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.


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