Thursday, May 12, 2016

Understanding Java Variables and Data Types

Java variables enable programmers to store single data points, bits of information, for later use. For efficiency in later use, Java variables have types. Those types are referred to as data types because they allow us to store different types of data respectively for convenience and predictability. It's necessary for any Java programmer to understand the basics of variables and data types in Java before moving on to more advanced topics.

To illustrate how Java variables work, let's imagine a photo sharing application. The app would store a whole bunch of information about the state of our application and the photos its users share: the number of users, the number of photos shared, and the total number of comments shared. That data has to be stored in order for us to manipulate and display them to our users when necessary. Enter Java variables.

Java Variables

Variables can hold data and that data can be changed over the lifetime of our program. A variable must have a type, a name, and be provided some sort of data to hold. The most widely used type of data in Java is the character string, represented by Java's String class. A string, such as "SitePoint" is simply an instance of the String class.

[author_more]

Naming Variables

There are a few rules you must follow and a few you should. Java's variable names are case senstive and can be an unlimited number of letters and numbers. However, variable names must start with a letter, underscore character _ , or a dollar sign $.

When creating variables in Java, it is best to follow the convention of using numbers and full words that are descriptive of the purpose of the variable while avoiding use of the underscore character and dollar sign. Finally, variables should be in lower camel case, a popular programming convention that dictates that the first letter of the first word should be in lower case and the following words should be capitalized.

Using Variables

Let's create the skeleton of our application's main class to see how we can store each of the aforementioned data points about our application in String variables:

public class SitePointGram {
    public static void main(String[] args) {
        String nameOfApp = "SitePointGram";
        String numberOfUsers = "";
        String numberOfPhotos;
        String numberOfComments = null;
        //...
    }
}

So what's going on there? Let's skip to the third line of that block of Java code. On each line, we create a new variable of type String to store a single point of information about our application. Observe that to create a variable in Java, we start by stating the type of the data to be stored in the variable, followed by the name of the variable in lower camel case, then the assignment operator =, and finally the data to be stored in the variable.

In the first line of our main method, we store the name of our application in the nameOfApp String variable and the data stored in it is "SitePointGram". The next line has a String variable that will store the number of users on our application. Notice that it stores an empty string "". Hold that thought as we move to the next two lines.

Every variable in Java has a default value; the default value for String variables is null, "nothing". If we don't know the value of a variable at the time of declaration, we can omit explicitly initializing it with a value and allow Java to implicitly provide it an appropriate default value. That is exactly what we do with the numberOfPhotos variable. Similarly, on the fourth line, we initialize the numberOfComments String variable to null explicitly, although we do not have to. It's important to understand that an empty string is an actual character string, albeit an empty one, while null means the variable does not yet have valid data. Let's continue.

SitePointGram is becoming popular and people are flocking to it. Let's represent the growth of our application in Java:

public static void main(String[] args) {
    //...
    numberOfUsers = "500";
    numberOfPhotos = "1600";
    numberOfComments = "2430";
    //..
}

After initializing our String variables, it is now apparent that our application has 500 users, 1600 shared photos, and a whopping 2430 total comments across those photos. We're doing well, so it's time we learn how to use data types in Java.

Continue reading %Understanding Java Variables and Data Types%


by Lincoln Daniel via SitePoint

No comments:

Post a Comment