Basic Java Language: Difference between revisions

wikademia>MetaBohemian
m →Primitive Data Types: -redundant & erroneous line: Strings aren't primitive
wikademia>MetaBohemian
m →Assignment Operator '=': clarified char literals, reworded float/double
Line 139: Line 139:
int a = 6;
int a = 6;
</pre>
</pre>
However, you can't say <code>char myChar = y;</code> to set a character to 'y'. You have to use ' ' for characters. That is: <code>char myChar = 'y';</code>. For Strings, use " ". That is: <code>String myString = "This is an example of a string"</code>. Note how characters are just 1 and strings can consist of entire words.
However, you can't say <code>char myChar = y;</code> to set a character to 'y'. That would set the character variable <code>myChar</code> to the value stored in the character variable <code>y</code>. To set <code>myChar</code> equal to the character <i>y</i>, use a '''character literal''' — a technical term for "a character in single-quotes": <code>char myChar = 'y';</code>. Note that a <code>char</code> primitive can only hold a single character. <code>char myChar = 'Yo';</code> is illegal.


Finally, what is the difference between double/float and int/long. Well, doubles and floats can have decimals: <code>float myFloat = 5.1;</code>
Finally, what's so special about <code>float</code>s and <code>double</code>s? Doubles and floats can have decimals: <code>float myFloat = 5.1;</code>. (The term "float" is short for "floating-point number," referring to the decimal point.)


Now, why would you want to set a variable? For further use, ie, to access at a later time. For example, you can do calculations with other operators...
Now, why would you want to set a variable? For further use, ie, to access at a later time. For example, you can do calculations with other operators...