Tuesday, November 5, 2013

Introduction to Java variables and data types

Variables

The easiest way to think of a variable is as a container that you can name and store data in. A variable can be anything from a string to a number to a simple 'true or 'false'. Instead of typing out the data stored in the variable every time you need to use it, you can instead just reference the name of the variable to access the information.

For instance, if you wanted to reference someone's name in multiple locations in code you could set their name to a variable.

String myName = "Bob";

"Hi myName how are you doing today? How did you get the name myName. I love it!" would actually read "Hi Bob how are you doing today? How did you get the name Bob. I love it!"

One advantage to using variables instead of 'hard-coding' the information is that if we needed to change the information we would only have to change it in one location - the variable and that change is reflected throughout the code.

Now, lets take a look at variable declaration and what everything means.

 String title = "Java Basics for Android";

1. The first word in a variable declaration is the data type that the variable will hold. In our case, our data      type is defined as 'String' because we are referring to a series of characters.
2. The second word in the declaration is the title or name of our variable. This is the name that we use to reference our information stored in our variable.
3. The equals (=) sign is what is called an operator. There are numerous operators used in Java, but this one refers to assignment. What we are saying is set whatever is on the left side of (=) equal to whatever is on the right side.
4. Following the (=) sign we have a series of words enclosed in quotations (" "). In Java, strings are enclosed in quotation marks.
5. Lastly, we have a semi-colon. A semi-colon is to Java as a period is to English. In other words, the semi-color is used by Java to determine when the end of the line is.

Basic Data Types

We have already talked about one data type offered in Java: strings. There are several others that we need to be familiar with.

int - An integer value, i.e. a whole number (no decimals) that includes 0 and negative numbers.
float - A floating point value that includes as many decimal places as it can hold. Because the decimal place can change, or float, it's important to know that these values may technically be imprecise. When precise decimals are needed, like for currency, we should use the BigDecimal data type. 
boolean - A true or false value that can can only be in one of those states at a time.
char - A single character, such as a letter "H" or a symbol or special character "%". When discussing chars, lowercase and uppercase letters are different. So, "h" is different than "H". 
byte - The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive)
short - An integer value that can range between -32,768 and 32,768 (inclusive). int variables have a much larger range of potential values. 
long - The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1

Some example variable declarations:
int age = 24;
float distance = 11.72011f;
boolean isFull = true;
char reportGrade = 'B';
The previous data types are called primitive data types because they are pretty straight forward and simple. 

String - A series of characters strung together to make text. 

Strings are more complex than primitive data types because they are actually objects in java, denoted by the capital "S" in string. Java naming convention dictates that objects be capitalized. An object is different than a primitive data type because objects have complex properties and methods available to it, whereas primitive data types are limited. For example, String objects have a method called "length()" that returns the number of characters in the string. Much more on methods later. 

I titled the post "Introduction...." because that is exactly what it is. Variables can get much more complex when we are talking about things like scope or constructors, but this should be enough to get you started with understanding how variables are used to store data. 

And so it Begins...

In the 1970's a theory of learning described as "Four Stages for Learning Any New Skill was developed by a psychologist by the name of Thomas Gordon. In his theory he defined four main stages of learning and awareness that each and every person goes through when learning a new skill.

The four stages are as follows:

1. Unconscious incompetence
     - An individual does not know/understand a skill and they may deny its usefulness
2. Conscious incompetence
     -An individual does not know/understand a skill, but they understand the benefit in learning it
3. Conscious competence
     -An individual starts to learn how to use the skill
4. Unconscious competence
     -An individual has so much practice with a skill that it becomes "second nature"

It is this fourth stage of learning that I hope to be able to improve upon by writing this blog. You see, you can read, replicate and do something as many times over as you want, but you will still not fully understand it until you are able to teach it. I hope to be able to regurgitate the knowledge I learn via tutorials and documents in this blog in an interesting, understandable manner to help me fully understand the concepts at hand.

In doing so, this will force me to critically think about the concepts at hand and how to apply them to different situations. In essence - it will force me to review the material instead of just reading it.