In Java, Why String is non-primitive data type?

后端 未结 10 2076
闹比i
闹比i 2021-01-01 03:10

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

相关标签:
10条回答
  • 2021-01-01 03:43

    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?

    0 讨论(0)
  • 2021-01-01 03:44

    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.

    0 讨论(0)
  • 2021-01-01 03:44

    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.

    0 讨论(0)
  • 2021-01-01 03:48

    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.

    0 讨论(0)
提交回复
热议问题