What is String in java? And why is it use in java?

Ezzaral commented: You are just a font of useless, lazy questions, aren't you? -3

Recommended Answers

All 14 Replies

String is a datatype.
for more information, I would suggest you take a look at this and go over your textbook/course notes again. it's a very basic datatype, used in every application I've ever seen and most likely in every application/code I have yet to see, so studying up a bit is not a bad idea, but the question (how) you asked, is way to 'open' to answer in a simple, single post.

commented: +1 for link +8

A String is effectively an array of chars and can be easily swapped between the two using the .toString() and .toCharArray() methods. Chars are single characters and a string is a collection of characters. ie 'f' and '2' are chars, but 'brown' is a string, or array of chars.

a String may look like an array of chars, but it isn't. don't get fooled by looks.

Isn't Char a primitive type and String a class construct of chars?

Char doesn't exist.
char is a primitive type, Character is it's wrapper class.
if a String was an array of chars, do you really think that the String class would provide this method?

To be fair, the Java 7 source code for String begins

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];
    ...

so you could reasonably say that a String is an array of chars wrapped in a class that provides many useful methods to work with the chars (but none that can modify them). Hence the source code for toCharArray simply returns a copy of the original char[] so as to protect it from modification

public char[] toCharArray() {
    // Cannot use Arrays.copyOf because of class initialization order issues
    char result[] = new char[value.length];
    System.arraycopy(value, 0, result, 0, value.length);
    return result;
}

Conceptually, a string is an object that holds a piece of written text. The name 'string' is historical, and refers to the concept of 'string rewriting', a mathematical technique for analyzing sentences in an abstract grammar; the intended mental image is of pieces of paper held together by a (physical) string, which was an early way of manipulating character strings before computers.

String is class. You can create object of that class.In c and C++ for use string you need to create pointer array of char datatype. But in Java there is no concept like pointer so for allowing string they give you class of String.

That's quite misleading. Java has reference variables that are just like pointers that you can't mess with. Java also has arrays of chars, eg
char[] refToCharArray = new char[120]; // creates a char array and a ref to it
The reason for the String class is to provide a load of useful methods operating on that simple array, and incidentally to protect it from modification thus allowing many important runtime optimisations.

char is a primitive type, Character is it's wrapper class.
It cant exist

ehm ... what can't exist?

Why have I received negative votes when what I said was true in esscence?

You confused char and Char (which doesn't exist), but apart from that I see no real fault in your post. However, anyone can vote on any post, for whatever reason they see fit, and votes without any comment are anonymous by design. I just gave you an up-vote to counter at least one of the down votes, but really... I wouldn't worry too much about it.
JC

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.