Java Variables

 

Variables are very important for us to understand because computer programs are filled with them. We need to know what they are and what they do.

For beginners a variable stores a value. A more specific definition of variable is a name given to a memory location which is used to store a value in a computer program. Another definition that I like is that variables act as containers for storing numbers and words which are used in a computer program.

String firstName = "Folau";
System.out.println(firstName);// this prints out Folau
firstName = "Lisa";
System.out.println(firstName);// this prints out Lisa

firstName is the variable. It holds “Folau” or “List” and you can use it any where you want to use firstName.

Here is how you create a variable:
type variable = value;
A variable must have 3 things:
type
name
value

Example: here is an example of a variable that stores a word or string.

String firstName = "Folau";
System.out.println(firstName);// this prints out John

The equal sign you see between the variable name(firstName) and the value(“Folau”) is used to assign the value to the variable.

Naming a variable has a set of rules
a. Variable names can container letters, digits, underscore, and doller signs

// you can do this
int age = 25;
// you cannot do this. double is a reserved word.
int double = 50;

b. Variable names must not start with digits but can start with and should start with letters

// you cannnot do this. 10IsMyNumber starts with a digit.
String 10IsMyNumber = 10;

c. Letters are more of the norm for java variables. In other languages such as PHP and Javascript, dollar signs and underscore are used regularly.

d. Variable names are case sensitive so firstName and firstName are different variables.

String firstName = "Folau";// N in firstName is uppercase
String firstname = "Lisa";// n in firstname is lowercase

e. Variable names should start with a lowercase letter and cannot contain whitespace(pretty much no space).

// you cannot do this. There is a space in between first and name. 
String first name = "Folau";
// solution. Remove or replace the space with the underscore sign
String first_name = ""

f. Variable names cannot be java reserved words(words that are reserved just for the language only like String you see in the above example)

// you cannot do this. String is a keyword and cannot be used as a variable name.
String String = "Folau";

// you can do this. string is all in lower case.
String string = "Folau";

Variable Naming conversions
1. camelCase vs snake_case.

String firstName = "Folau"; vs String first_name = "Folau";
int numberOfPersons = 100; vs int number_of_persons = 100;

Most java programs I have worked on and seen use the camelCase pattern. Most java developers I know use the camelCase pattern. I think it’s java convention to use camelCase but it’s up to you.

2. Make variable names comprehensive and easy to understand. Great programmers use comprehensive variable names just FYI. Use words, even if they are long, rather than abbreviations and initials

int numberOfPersons = 100; // great
int nop = 100; // not clear
int p = 100; // not good at all

3. Naming style
Sometimes developers get lazy and do this.

// NUMBER_OF_PERSONS and Number_Of_Rocks are not consistent. Name both of them the same style.
public static final int NUMBER_OF_PERSONS = 100;
public static final int Number_Of_Rocks = 50;

My opinion on variable naming convention is that when I name variables I make sure they are clear and easy to understand within the context. Remember code is writtern for humans and reading code is like reading a story. It should flow and make sense.

Make sure to take time to plan and think about your variable names because they are so important. Especially if you are in a team where your team members will read your code. They should not have any issue reading or understanding what your variables represent.

Development time can be saved if your variable relay the message it needs to relay. Bad variable names can cause confusion and waste a lot of development time. Plan and think first!

Your variable names kind of represent how well you understand the program you are developing. Not only that it also represents how good of a developer you are to a certain degree.

 

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *