Friday, November 16, 2012

My Java Tutorial 3: What is your type?


In almost every program, you will use variables. Variables store information in memory and make available to the programmer. It can be in term for number, string, character or other data types. I will explain about variable, syntax, data types, and assignment statement. This will be quite a reading, but believe me, to be a programmer is actually to read. The thousand-line’s codes... I mean books and JavaDoc.



First, let’s look at this simple program. It does nothing much other than declaring and initializing the variables with variable name describing the data type.

A variable is a symbol that represents something. It usually have descriptive variable name to describe what it store. Right now, it is small program so you know and don’t have to look at the definition. But when the code are thousands lines, you may start taking Panadol.  Therefore, it is a good practice to name variables close to what it really does. It also useful to include a comment with each variable declaration.

 Variable name have some rules in it. Although you can name your son Johnny English and doubt the filmmaker going to file a report on you, Java compiler will, because there is a space in between.
1.    It must begin with a letter or underscore. (Underscore refers to character ‘_’)
2.    It can’t start with number.
3.    It must be consist of entirely letters, digits and underscores.
4.    No space or special characters(E.g.:!@#%^&*<>?/\|[]{}~`) are allowed in identifiers (variable name). Except @, and in JAVA: dollar sign $.
5.    Java is case-sensitive. So helloWorld and HELLOWOrld and HELLOWORLD are all distinct name.
6.    Can’t be a reserved word: abstract, assert, Boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch. tldr; Just don’t use it when your IDE make it colorful in, dangerous way, like red or purple. Unless you customize your IDE coloring.

Some of allowed variable are: a, A, xXx, rate, x1, i_am_a_very_long_long_name, HelloWorld, agent007, hadahsneiu2i3_4fs. Yes, the last name is allowed to, though I doubt you want that kind of name in your adorable program.

Do you know camel, that cute thing in the desert? There is a convention for naming the variable referred to as camel case. In this program whatIsYourType, aSingleLetter is a camel case variable name. When the name is made up of several words, capitalize each word except the first.
Another convention is to use all capital letters for constant, underscores to separate words.

There are 3 musketeers, I mean, datatypes: primitive, reference and constant data types.
1.     You mostly deal with primitive data types in this beginning program. It is simple data types also known as built-in types. Four common integer type: byte, short, int, long. There store similar value, but within different range, but usually we use int. There are times the numbers are way big you will be going to another data type. For decimal values, two of them are float and double where double is usually the choice of programmer. Then there are boolean that only have 2 values: true and false. Like a lamp, turn on and off. Last but not least the single char-arcter that hold single Unicode character. (Java can hold Unicode character, but for the sake of illiterate others, don’t use Chinese/Arabic/Japanese/Koreans/whatever as your variable name)
2.    Reference data types refer to as object. Java is an object-oriented programming language, so you will be using this in future. It can be class, array or interface. E.g: String. Different from data types above that is similar in C++ programming, String is an object, a special data types that store text. No matter how long you write it as long as your computer memory can hold it or the maximum value.
3.    Constant data types is declared using keyword final. When declared in a class, it will use keyword static.

Declaration is to make a variable of certain data type. So, to make an integer called number, I would do this: int number; Initalization / assignment statement is to set initial value to the number. To assign number a value of 3, do something like this: number = 3; In the previous program, I declare a new variable AND give it initial value of 3. Assignment statements take the form variable = expression; where expression refers to or computes a data value.

No comments:

Post a Comment