Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaration of characters and Strings

Tags:

java

Declaration of a character:

char ch = '';

When I do this i am getting the error 'empty character literal'.

Declaration of a String:

String str = "";

I see no error in doing that to a String.

The question is, why doesn't a similar error show up for the declaration of a String, or why declaration of empty character generating such error where empty string is getting passed

like image 786
Samreen Fatima Avatar asked Sep 01 '15 17:09

Samreen Fatima


People also ask

How do you declare a string and character?

Declaring a string is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string. char str_name[size]; In the above syntax str_name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store.

What are strings and characters?

The main difference between Character and String is that Character refers to a single letter, number, space, punctuation mark or a symbol that can be represented using a computer while String refers to a set of characters. In brief, String is a collection of characters.

How do you declare a character in C?

Syntax of Declaring Character Variable in Cchar variable_name; Here char is used for declaring Character data type and variable_name is the name of variable (you can use any name of your choice for example: a, b, c, alpha, etc.) and ; is used for line terminator (end of line).


1 Answers

String is a set of chars and String str=""; contains no chars(read: empty string) but if you want to have Char variable it must have some value. '' means no value.

like image 177
Jure Potocnik Avatar answered Sep 30 '22 15:09

Jure Potocnik