How do strings work? In C we must declare the size of a char array to represent a "string".
But in java we do not allocate memory for the size of a string. So how does the string work?
Is there a pre deterimed size assigned to strings from the compiler or what?

The String class handles all that. There must be a similar class in C++

The String class handles all that. There must be a similar class in C++

How do strings work? In C we must declare the size of a char array to represent a "string".
But in java we do not allocate memory for the size of a string. So how does the string work?
Is there a pre deterimed size assigned to strings from the compiler or what?

The class handles all those details. Remember that Strings are immutable. They know how much space they need when they are created.

In java String is class

You use command :

javap java.lang.String on command prompt

Here you can see various predefined methods

Don't confuse string variables with Strings. In C terms, a String variable is a pointer, and its initial value is null. You can only create a string via new String(some parameters) or as a String literal "some string". In both cases the size of the string is known when it is created, and a suitable amount of space is allocated dynamically on the heap when the String is created.

Generally speaking, anywhere in C you would see a call to malloc(), in Java (or C++) you'll find the word new instead. This also applies for strings, but strings are a little bit special in that there is a unique syntax for creating them (using "...") that doesn't use new. In either case, the result is the same: the standard library reserves the space to store the String when the constructor is called. Java doesn't have destructors (as such), so the space is only freed when the garbage collector gets around to it.

In C, I'd like to add, you do not need to declare the size of the char array to store a string when you initialize it with a string literal; since the size is known at compile time, the compiler can do it for you.

char str[] = "foo";

How do strings work? In C we must declare the size of a char array to represent a "string".
But in java we do not allocate memory for the size of a string. So how does the string work?
Is there a pre deterimed size assigned to strings from the compiler or what?

When it comes to Java, String type is a bit special. Even though it is a "proper" reference type like other classes out there, compiler offers additional support when it comes to handling strings.

For e.g. Java doesn't have operator overloading but permits "+" operator for String object concatenation (which decomposes to StringBuilder operations under the hood but still). It's also possible to create String objects without explicitly calling any static method or constructor i.e. compiler has built-in support for "string literals".

// "String" instance creation without new at compiler level
final String first = "something";
// concatenation; important: strings are immutable and hence a new "copy" is
// created for the result string instead of modifying the parameters to +
final String newString = first + " to be proud of";

Regarding the question about capacity, almost all regular methods for creating strings (apart from the ones mentioned above) automatically account for capacity, which is inferred from the passed in arguments. Let's assume you want to create a UTF-8 String instance from a sequence of bytes read from a Socket. The code would look like:

final byte[] bytes = readBytesFromSocket();
final String newUtf8String = new String(bytes, "UTF-8");

But you would rarely need to "instantiate" string and new String() should be rather thought of an exception rather than the norm. Now, drawing parallel with C like languages where you need to allocate an appropriate buffer which needs to be passed to a routine as follows:

char line[] = new char[256];
getline(line, 256);
printf("%s", line);

Such code is not required in Java given that the "string" creation is internally handled by almost all API methods which deal with strings. A Java example for reading lines from a plain text file would be:

Scanner in = new Scanner(new File("a.txt"));
while (in.hasNextLine()) {
    String s = in.nextLine();
    System.out.printf("Line read: %s", s);
}

There are quite a few other details related to "encoding" when it comes to "how" a sequence of bytes/chars is interpreted by the String class but that would needlessly complicate the answer to your original question.

You might also be interested in gathering a bit more background on Strings in Java by reading the following articles:
http://www.xyzws.com/scjp/SGS11/3/3
http://docs.oracle.com/javase/tutorial/java/data/strings.html

How do strings work? In C we must declare the size of a char array to represent a "string".
But in java we do not allocate memory for the size of a string. So how does the string work?
Is there a pre deterimed size assigned to strings from the compiler or what?

Perhaps you will find this tutorial useful. It's about how to declare/use strings and the methods that the String classs has(with examples). Good luck with learning Java!

Perhaps you will find this tutorial useful. It's about how to declare/use strings and the methods that the String classs has(with examples). Good luck with learning Java!

this 'tutorial' doesn't even show all the basic ways to initialize a String, it just shows a few of the basic functionalities that every Java developer will use on a regular base

this 'tutorial' doesn't even show all the basic ways to initialize a String, it just shows a few of the basic functionalities that every Java developer will use on a regular base

No offence, but the guy asked for String basics, not for an exhaustive approach toward Java strings. Instead of criticizing, perhaps you should come with a better alternative.

No offence, but the guy asked for String basics, not for an exhaustive approach toward Java strings. Instead of criticizing, perhaps you should come with a better alternative.

he asked how Strings work, not what you can do with a String.
since a better alternative was already provided, I didn't felt compelled to try and outdo the ones already posted earlier.

he asked how Strings work, not what you can do with a String.
since a better alternative was already provided, I didn't felt compelled to try and outdo the ones already posted earlier.

My mistake, didn't saw the links above.

commented: no harm by making a mistake, if you are prepared to be corrected :) +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.