Difference between "char" and "String" in Java

JavaStringChar

Java Problem Overview


I am reading a book for Java that I am trying to learn, and I have a question. I can't understand what is the difference between the variable type char and String. For example, there is a difference between int and short, the bytes at the memory and the area of numbers that they have.

But what is the difference between char and String? except that char use (') and "String" (").

PS: It is my first "real" programming language. (At school I learned a fake-language for the purpose of the programming lesson.)

Java Solutions


Solution 1 - Java

char is one character. String is zero or more characters.

char is a primitive type. String is a class.

char c = 'a';
String s = "Hi!";

Note the single quotes for char, and double quotes for String.

Solution 2 - Java

char means single character. In java it is UTF-16 character. String can be thought as an array of chars.

So, imagine "Android" string. It consists of 'A', 'n', 'd', 'r', 'o', 'i' and again 'd' characters.

char is a primitive type in java and String is a class, which encapsulates array of chars.

Solution 3 - Java

In layman's term, char is a letter, while String is a collection of letter (or a word). The distinction of ' and " is important, as 'Test' is illegal in Java.

char is a primitive type, String is a class

Solution 4 - Java

char is a primitive type, and it can hold a single character.

String is instead a reference type, thus a full-blown object. It can hold any number of characters (internally, String objects save them in a char array).

Primitive types in Java have advantages in term of speed and memory footprint. But they are not real objects, so there are some possibilities you lose using them. They cannot be used as Generic type parameters, they could not have methods or fields, and so on.

However, every Java primitive type has a corresponding full-blown object, and the conversion between them is done automagically by the compiler (this is called autoboxing).

You can for example do:

int i=12;
Integer l=i;

The compiler takes care of converting the int to a Integer.

Solution 5 - Java

char have any but one character (letters, numbers,...)

char example = 'x';

string can have zero characters or as many as you want

String example = "Here you can have anything";

Solution 6 - Java

A char holds a single character, while a string holds lots of characters.

Solution 7 - Java

Char is a single alphabet where as String is a sequence of characters. Char is primitive datatype where as String is a class.

Solution 8 - Java

char is a primitive type, and it can hold a single character. String is instead a reference type, thus a full-blown object.

Solution 9 - Java

Well, char (or its wrapper class Character) means a single character, i.e. you can't write 'ab' whereas String is a text consisting of a number of characters and you can think of a string a an array of characters (in fact the String class has a member char[] value).

You could work with plain char arrays but that's quite tedious and thus the String class is there to provide a convenient way for working with texts.

Solution 10 - Java

A char simply contains a single alphabet and a string has a full word or number of words woth having a escape sequence inserted in the end automatically to tell the compiler that string has been ended here.(0)

Solution 11 - Java

A char consists of a single character and should be specified in single quotes. It can contain an alphabetic, numerical or even special character. below are a few examples:

char a = '4';
char b = '$';
char c = 'B';

A String defines a line can be used which is specified in double quotes. Below are a few examples:

String a = "Hello World";
String b = "1234";
String c = "%%";

Solution 12 - Java

In string we can store multiple char. e.g. char ch='a';

String s="a";

String s1="aaaa";

Solution 13 - Java

In terms of ASCII values you can say char is a single ASCII value ranging from 0-255. Whereas String is a collection of ASCII values. Try this code to learn better.

        char c='a';
		String s="a b c d e f g hijkl";
		int i=c;
		System.out.println(i);
		for(int count=0;count<s.length();count++){
			int temp=s.charAt(count);
		    System.out.print(temp+" ");
		}

The output will be:

97

97 32 98 32 99 32 100 32 101 32 102 32 103 32 104 105 106 107 108

Since 97 is the ASCII value for small 'a'. 32 is the ASCII value for space. Hope this helps in-depth understanding of the concept.

Solution 14 - Java

A character is anything that you can type such as letters,digits,punctuations and spaces. Strings appears in variables.i.e they are text items in perls. A character consist of 16bits. While the lenght of a string is unlimited.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndroid GirlView Question on Stackoverflow
Solution 1 - Javauser207421View Answer on Stackoverflow
Solution 2 - JavaVladimir IvanovView Answer on Stackoverflow
Solution 3 - JavaAndreas WongView Answer on Stackoverflow
Solution 4 - JavaAndrea ParodiView Answer on Stackoverflow
Solution 5 - JavaMartin54View Answer on Stackoverflow
Solution 6 - JavaMatthiasView Answer on Stackoverflow
Solution 7 - JavaTanuj VermaView Answer on Stackoverflow
Solution 8 - JavaSandeep KhotView Answer on Stackoverflow
Solution 9 - JavaThomasView Answer on Stackoverflow
Solution 10 - Javauser2757367View Answer on Stackoverflow
Solution 11 - JavaAshwini KapoorView Answer on Stackoverflow
Solution 12 - JavaPratiti MehtaView Answer on Stackoverflow
Solution 13 - JavaSabarish Kumaran.VView Answer on Stackoverflow
Solution 14 - JavaMiss Mark Loveth.View Answer on Stackoverflow