Junior — Middle
Is the String type in Java or other programming languages a primitive or reference type?
sobes.tech AI
Answer from AI
In Java and most programming languages, the String type is a reference type, not a primitive.
This means that a variable of type String contains a reference to an object in memory, not the actual value. For example, in Java, strings are immutable, and when modified, a new object is created.
Example in Java:
String s1 = "hello";
String s2 = s1;
s1 = s1 + " world"; // a new object is created
System.out.println(s2); // will output "hello"
In C#, String is also a reference type, but with the feature that strings are immutable and optimized for performance.