In Java, we can directly use String
to declare a string variable name and specify its value. We do not have to define the string as an array by using new keywor
Yes String is an object in Java. The fact that it can be used similar to primitives does not contradict
Please refer - Strings are objects in Java, so why don't we use 'new' to create them?
String creats an Object each time you assign a value in its String Pool. Where every time if you create a similar object it will look for that and refer, if that value is not there it will again create a new one. Study more on String Pool you will automatically come to know the difference.
In Java, String is an object that stores the location to where the actual "value" of where the String is located.
You DO need to use the new
keyword when making an array of Strings, as you do with making an array of anything else.
String[] text = new String[4]
This create's four String references that lead the computer to where the text is located at. Also, all Strings default to a value of null
because until you give them a value to store at a memory address, there is nothing to be stored.
String is object, is immutable, that means that you cannot change the object itself, but you can change the reference to the object.
This is how String works
String myStr = "test";
This as usual, creates a string named "test" and assign it a reference "myStr".
Important point to note here is, while the String object is immutable, its reference variable is not.