Size and type of variable values are specified by a datatype. For the corresponding variable, it is important to use correct data type and also it is used to avoid errors, save time and memory as well as make the code more maintainable and readable. Most common data types are:
|int||4 bytes||Stores whole numbers from -2, 147, 483,648 to 2,147,483,647|
|Long||8 bytes||Stores whole numbers from -9,223,372,036,854,775,808 to 9,223, 372,036,854,775,808|
|Float||4 bytes||Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits|
|Double||8 bytes||Stores fractional numbers. Sufficient for storing 15 decimal digits|
|Bool||1 bit||Stores true or false values|
|Char||2 bytes||Stores a single character/letter, surrounded by single quotes|
|String||2 bytes per character||Stores a sequence of characters, surrounded by double quotes|
Number types are divided into two groups:
Integer types- it stores whole numbers, either positive or negative (such as 456 or -789) and it should be without decimals. int and long are its valid types. Which type should be used depends upon the numeric value.
Floating point types – it represents numbers with a fractional part, containing one or more decimals. float and double are its valid types.
Int- The int data type can store whole numbers from -2147483648 to 2147483647. In general, when we create variables with a numeric value, preferred data type is int.
int myNum = 10000;
Long- The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store value. We should end the value with “L”:
long myNum = 160000000000000L;
Floating Point Types
Floating point type used be used whenever we need a number with a decimal, such as 5.16 or 3.14512.
Float- The float data type can store fractional numbers from 3.4e-038 to 3.4e+038. We should end the value with “F”:
float myNum = 6.75F;
Double- The double data type can store fractional numbers from 1.7e-308 to 1.7e+308. We can end the value with a “D”(it may not be required):
double myNum = 18.88D; Console.WriteLine(myNum);
Which is better to use either float or double?
The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.
A floating point number can also be a scientific number with an “e” to indicate the power of 10:
float f1 = 35e3F;
double d1 = 12E4D;
A Boolean data type is declared with the bool keyword and can take the values true or false:
bool isCFun = true;
bool isFishTashy = false;
Console.WriteLine(isCFun); // Outputs True
Console.WriteLine(isFishTashy); // Outputs False
The char data type is used to store a single character. The character must be surrounded by single quotes, like ‘A’ or ‘b’:
char myGrade = ‘A’;
The string data type is used to store a sequence of characters(text). String values must be surrounded by double quotes:
string greeting = “Hello World!”;