Android Course - How to use Java Variables

Android Tutorial – Java Variables

Articles & Tutorials

What are the Java Variables? How to use them? what is Java Data Type …

Here is the video where I’ve talked about this and you’ll found all the details in this post :

 

Java variables are a part of memory that can contain a data value and a variable has a Data Type.

Variables are used to save information which your Java program needs to do for his job. It can be any type of information like texts, codes (e.g. currency codes, country codes, etc.) to numbers, …

In the example below, the main() the method contains the declaration of a single integer variable named age. The value of the integer variable is first set to 38.

public class VariablesExample{

    public static void main(String[] args) {

        int age = 38;

        System.out.println("My age is: " + age);
    }

}

 

Types of Java Variables :

There exist 4 types of Java Variables :

  • Non-static fields
  • Local variables
  • Parameters
  • Static fields

A non-static field is a variable associated with the object. They are called also instance variables because they belong to instances of a class.

A static field is a variable associated with a class and has the same value for all objects that access it. They are also called class variables.

A local variable is declared inside a method. Local variables are only accessible inside the method that declared them.

Parameters are variables that are passed to a method when the method is called. Parameters are also only accessible inside the method that declares them.

 

Declaration of Java Variables:

For the 4 types, the declaration has the same syntax like this :

dataType nameOfVariable;

In this line we have only two things :

dataType: which type is your variable?

nameOfvariable : the name that you will choose for your variable

 

Java DataType :

They are regrouped in 2 categories :

 

Here is an example, how to declare java variable with 2 categories :

// Primitive Data Type
int     age;
byte    myByte;
short   age;
char    surname;
long    id;
float   average;
double  average;

// Object references
String  name;
Integer age;
Byte    myByte;
Short   age;
Long    id;
Float   average;
Double  average;

Maybe you have made some difference between them, for Object Data types, its start with Uppercase character.

 

Assignment of Java Variable :

It respects this syntax :

nameOfVariable = value


dataType nameOfVariable = value;

Here is some example of how to assign the java variable:

age = 38;
int age = 25;
String name = "fay├žal";

 

Reading of Java Variable :

It’s very easy to read Java variable, you just use the name of the variable in your part of code

For example VariablesExample.java, I just write the variable “age” in method System.out.println

If you want for example to create a new variable with an existing one, just do it like this :

int newAge = age;

 

Naming Conventions for Java Variables :

You must respect some rules for naming Java variables :

1) The Java variables names are case sensitive, it means the variable age isn’t same with AGE.

2) Java variables names must start with a letter, or _ character or the $ character.

3) The name can also contain numbers (in addition to letters, or _ character or the $ character) after the first character in a Java variable name.

4) The java variables names cannot be named like reserved keyword in Java, for example, you can’t use these names for your java variables: int, for, …

 

There are also other conventions but it’s not necessary to follow them, such as :

5) Writing the variable in lowercase: address, city, firstname.

6) If the variable contains more words, the first one will be in lowercase and next ones, the first character will be in upperCase, for example: firstName, myFullAddress.

7) Do not begin the name with $ or _ … yeah i know it’s allowed but not recommended

8) If the variable is static then it will be named in only with uppercase separated by ‘_’ character. For example DAYS_IN_WEEK.

 

Related Post: Your First Java Program

 

 

Leave a Reply

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