Similarities and differences between C and Java strings and string Arrays

Source: Internet
Author: User

 

1. C Language

In C language, there is basically no difference between a string and a character array, and both require Terminator; for example, char s [4] = {'A', 'B', 'C ', 'D '}; the definition of this character array can be compiled, but the array is not closed. If you need to apply for Memory later, all future data will be placed in it, although it is not long enough, if it is Char s [5] = {'A', 'B', 'C', 'D '}; the system automatically stores an ending character at the end of the string and closes the array, indicating that the character array has an ending character;

The length defined by the string must be greater than the length of the Character Sequence, for example, char S1 [4] = {"ABCD"}. Compilation fails, it should be written as char S1 [5] = {"ABCD"}; and the system will automatically store an terminator at the end of the string, indicating that the string has an Terminator;

Use the strlen () function in the C language to test the length of the array. The strlen () function does not contain the terminator '/0 '.

Char s [5] = {'A', 'B', 'C', 'D '};

Char S1 [5] = {"ABCD "};

Int A = strlen (s );

Int B = strlen (S1 );

The result is a and B is 4;

2. Java language

The Terminator is not required for both string and string arrays;

For example, char [] value = {'J', 'A', 'V', 'A', 'yu', 'yan '};

String S1 = new string (value );

String S2 = "Java language ";

Int A = value. length;

Int B = s1.length ();

Int c = s2.length ();

The running result A, B, and C are both 6, indicating that the Terminator is not required for both the string and string arrays. But pay attention to the value here. length and s1.length (). The length of an array object can be recorded by the famous Constant Length in the array. Length () is an instance method in the file class and is used to return the file size, of course, you can also return the size of the string.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.