(array & string) Difference in Java vs. C [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this questionI know about C and I am entering into Java and confused about its approach towards arrays and strings. It's totally different from arrays and strings in C. Please help me understand what is actually the difference between C and Java (for strings and arrays).
In C
Arrays
Arrays in C are simply syntactic sugar to access contiguous memory spaces, or - vulgarizing it shamelessly here - a variant of a pointer notation. To avoid allocating big chunks of contiguous memory and avoid having to reallocate your memory yourself manipulating data of variable size, you then resort to implementations of common Computer Science Data Structure concepts (for instance, a linked list, which uses a pointer to indicate the memory address of the next element in a series).
You can substitute pointer arithmetic with array notations in C, and vice versa.
The following will print the 5 elements of an array using different access methods:
#include <stdio.h>
int main(int ac, char **av) {
char arr[2] = {'a', 'b'};
printf("0:%c 0:%c 1:%c 1:%c\n", arr[0], *arr, arr[1], *(arr + 1));
return (0);
}
The following will be valid with int variables. Notice the slight modification to accomodate for the size of an integer:
#include <stdio.h>
int main(int ac, char **av) {
int arr[2] = {42, -42};
printf("0:%d 0:%d 1:%d 1:%d\n", arr[0], *arr, arr[1], *(arr + 4));
return (0);
}
(To obtain the size of a given data type, resort to the use of sizeof.)
Strings
Here I assume you want to know about the conventional C-string implementation, and not one provided by a 3rd-party library.
Strings in C are basically simply arrays of characters. The main reason for this is obvious: as you need to often manipulate strings and print them to a stream, using a contiguous memory space makes sense and is an easy implementation. However, as you need to remember the size of your contiguous memory space to not inadvertently access something forbidden, we rely on the concept of a "NULL-terminated string", meaning a string of N characters is a actually an array of N + 1 characters terminated by a trailing '\0' character, which is used as the de-facto character to look for when you want to reach the end of a string.
A straightforward declaration would be:
char *test = "my test";
which would be equivalent to:
char test[8] = { 'm', 'y', ' ', 't', 'e', 's', 't', '\0' };
(Notice the trailing '\0')
However, you have to realize that in that case, the string "my test" is static, and that's the memory space you are directly pointing to. Which means you will encounter issues when trying to dynamically modify it.
For instance, this would blow up in your face (following thee previous declaration):
test[4] = 'H'; /* expect a violent complaint here */
So to have a string you can actually modify you can declare a string simply as:
#include <stdio.h>
#include <stdlib.h>
int main(int ac, char **av) {
char *test = strdup("my test");
printf("%s\n", test);
return (0);
}
Where strdup() is a function of the C standard library allocating memory for your string and injecting the characters in there. Or you can allocate memory yourself with malloc() and copy characters manually or with a function like strcpy().
This particular declaration is thus mutable, and your are free to modify the content of the string (which in the end is just a dynamically allocated array of characters, allocated with malloc()).
If you need to change the length of this string (add/remove characters to/from it), you will need to everytime be wary of the allocated memory. For instance, calling strcat() will fail if you haven't reallocated some additional memory first. Some functions, however, will take care of this for you.
The C string does NOT support Unicode by default. You need to implement to manage code points yourself, or consider using 3rd-party library.
In Java
Arrays
Arrays in Java are very close to their C parent (to the point that we even have a method for efficient array-to-array-copy support using a bare-bone native implementation: System.arraycopy()). They represent contiguous memory spaces.
However, they wrap these bare-bone arrays within an object (which keeps track of the size/length of the array for you).
Java arrays can have their content modified, but like their C counterpart, you will need to allocate more memory when trying to expand them (except you do it indirectly, and will usually reallocate a complete array instead of doing a realloc() like in C).
Strings
Strings in Java are immutable, meaning they cannot be changed, once initialized, and operations on String actually create new String instances. Look up StringBuilder and StringBuffer for efficient string manipulation with an existing instance, and beware of their internal implementation details (especially when it comes to pre-setting the capacity of your buffer efficiently, to avoid frequent re-allocations).
for instance, the following code uses produces a 3rd String instance out of someString and "another string":
String myNewStr = someString + "another string";
In the underlying implementation, the Java String* classes also use an arrays of characters, like their C parent.
This implies that they use more memory than the bare-bone C implementation, as you have the overhead of your instance.
Not only that, they actually use a lot more memory because the Java String class provides Unicode support by default, meaning it allows for multiple code points per character (which is not a trivial thing to do in C, in comparison).
On the other, notice that except if considering performance, you don't need to worry about threading, memory, and implementing functions looking for trailing '\0' characters.
What More?
A lot more could be said and researched. Your question is fairly broad at the moment, but I'll be glad to edit if you add sub-questions in your comments.
Also, maybe this could help:
- Java Language Specification, 3rd Ed., Chap 10 - Arrays
- Details of the JVM compilation for arrays
- Java and C differences, with details for both strings and arrays.
- The C-FAQ contains some details similarities and differences between arrays on pointers.
In C, a string is typically just an array of (or a pointer to) chars, terminated with a NUL (\0) character. You can process a string as you would process any array.
In Java, however, strings are not arrays. Java strings are instances (objects) of the java.lang.String
class. They represent character data, but the internal implementation is not exposed to the programmer. You cannot treat them as arrays, although, if required, you can extract string data as an array of bytes or chars (methods getBytes
and getChars
). Note also that Java chars are 16-bits, always, while chars in C are typically (not always) 8-bit.
Arrays:
The first obvious difference is that Java doesn't use the same declaration syntax for arrays as C. In C, the array subscript is part of the declarator, whereas in Java it's part of the type specification:
int[] arr; // Java, arr is null until array object is instantiated
int arr[]; // C, incomplete declaration
Note that, in Java, arr
exists but is null-valued. In C, arr
doesn't exist until a complete declaration appears.
int[][] 2Darr; // Java, arr is null until array object is instantiated
int 2Darr[][]; // Illegal declaration in C; size must be specified for at least
// the outer dimension
Array objects in Java must be instantiated with a new
operation, and it's there that the array size is specified:
int[] arr = new int [10];
int[][] 2Darr = new int[10][20];
If the array is not of a primitive type, each individual array element must be separately instantiated:
String[] strs = new String[10];
for (int i = 0; i < strs.length; i++)
strs[i] = new String("some value");
Array expressions in Java do not have their types "decay" to pointer types like array expressions in C (which is handy, since Java doesn't have pointer types per se); array types in Java are "first class" objects, meaning they retain all their type characteristics in any context. When you pass an array object to a method, the method receives an array object, not a pointer.
Java arrays know how big they are (given by the .length
attribute).
Strings:
Unlike C, Java supplies a distinct String data type. Do not think of Java strings as 0-terminated arrays of char; they are something different.
Java String objects are immutable; you cannot modify the contents of a String object. You can create a new String object from the modified contents of an existing String object. There are also classes like StringBuilder and StringBuffer that allow you to manipulate character data directly and create new String objects.
Hope that helps.
A string in C is just an array of chars. There is nothing but the convention that when a NUL character (\0) is found, the string ends.
All string support depends on functions in the C standard library, such as strlen(), strcpy() and so on.
To tell the size of a C "string", you have to pass a pointer to a separate function. You could argue that there are no strings in C at all, just conventions for arrays of char.
Java, on the other hand, has strings built in as a part of the language itself. The Java String has methods, which can tell you its size for instance. Java has primitive types, like C: float and int.
But it also has "Objects" and String is a kind of object.
This is so far very much like the difference between C and C++ too.
In C string is indeed array of character ended by '\0'. In Java string is a class. The java string can better be compared with std::string in C++ rather than C character array.
Declaration :- In C - char str[100]; In Java - String str;
In Java in most of the cases you don't need to worry about the string implementation as rich varieties of member functions are provided to work with it. In C also there are many APIs like strlen, strcpy, strcat, which are quite sufficient for normal operations.
The main difference comes in when you have to do some operations involving two strings. e.g. lets say assigning one string to other. In jave it's straight forward.
String str1("This is Stack Overflow.");
String str2;
str2 = str1;
But In C you will have to use a loop to assign each character. Now again that does not mean that Java does it faster, because internally java also does the same thing. Just that the programmer is unaware of that.
In Java some operations can be done using natural operators e.g. comparison.
str1 == str2.
But in C you will have to use strcmp function for this.
strcmp(str1,str2);
In short while working in C you must and must know how to operate on string internally. In Java you must not.
Now in C you will also have to be extra careful when you are creating string on heap area.
char * str1 = malloc(100);
You will have to free this memory using free(str1). In Java the programmer need not aware of heap memory or stack memory so such thing do not come into picture.
String is a object in JAVA its unlike a Character array in C
If you really need to know the difference you need to know the diff between ptr in C and ref in java
when you say in C: char str[10]; ==> you allocate a sequence 10 blocks in memory and every block's size is sizeof(char) and terminated by null so you can deal with strings with normal ptr operations.
java: when you say String str; ==> you create an object java.lang.String which inherit some methods which in the java.lang.String Class like compare(),equals(),contains(),charAt() and more.
C: to perform normal String manipulation you treat with ptrs or you using prepared function from header files which inside it deals with block of memory no more no less.
Ex: comparing 2 strings => strcmp(str1,str2);
java:as I said every thing in java is an object if you want to compare 2 string:
String str1;
String str2;
str1.equals(str2);
C: a String is must be NULL-Terminated to know when you should stop and if you try to read the block after the string no thing bad would happen (it will compile and wouldn't crash probably)
Java: as I said String is an object so you don't need to deal with memory if you try to access an element outside the String it will throw an indexOutOfBoundException and your program would crash unless you handle this Exception.
精彩评论