Java Variables

This post will discuss Java variables with examples to help you achieve your learning goals.

Variables are the names of reserved areas of memory that have been allocated for a specific purpose. Essentially, it is a memory location name.

A variable is a combination of “vary” and “able“, meaning its value can be changed.

Java Variables

Java Variables

When it comes to Java variables, they serve as containers for storing data values.

Variables in Java can be divided into different types, such as:

  • String – a type of data that stores text, such as “Hello.” Double quotes surround the values of strings.
  • int – holds integers without decimals, such as 123 or -123
  • float – contains floating point numbers with decimals, such as 19.99 or -19.99
  • A char stores characters such as ‘a’ or ‘b.’ The values of chars are surrounded by single quotes.
  • boolean – stores values that are either true or false.

Syntax:

type variable = value;

Where type is a Java variable type (such as int or String), and Variable is the names of variables (such as x or any name the user prefers). Variables are assigned values using the equal sign.



Creating Java Variables

We must create Java variables by specifying their type and assigning a value.

Here is an example of how to create a variable that should store text.

Create a variable called variable of type String and assign it the value “Java“:

Example: 

public class Main { public static void main(String[] args) { String variable = "Java"; System.out.println(variable); } }

Here we create another String type variable and Assign name of Java Founder to it:

Example: 

public class Main{ public static void main(String[] args) { String Java_Founder = "James Gosling"; System.out.println("The name of Java Founder is: "+Java_Founder); } }

Consider the following example to create a variable that stores a number.

Make a variable of type int, name it num_variable, and assign it the value 205:

Example: 

public class Main { public static void main(String[] args) { int num_variable = 205; System.out.println(num_variable); } }

Declare a variable of type int, name it mrx_sum, and assign it the equation 20+10:

Example: 

public class Main { public static void main(String[] args) { int mrx_sum = 20+10; System.out.println("Answer: "+mrx_sum); // As a result it prints the answer as 30 } }

Considering Java variables, you can also declare a variable without assigning a value and assign the value later:

Example: 

public class Main { public static void main(String[] args) { int num_variable2; num_variable2 = 1020; System.out.println( num_variable2); } }

Similarly, we declare a String variable named ample first and assign a value to it in the next line:

Example: 

public class Main { public static void main(String[] args) { String ample; ample="Java"; System.out.println(ample); // As a result this also prints the value Java } }

If you assign a new value to an existing variable, it will overwrite the previous value:

Example: 

public class Main { public static void main(String[] args) { int number = 25; number = 30; // number is now 30 System.out.println(number); } }

Example: 

public class Main { public static void main(String[] args) { String mrx ="Java"; mrx = "Java Programming "; // this modifies the value of mrx from Java to Java Programming System.out.println(mrx); } }


Final Variables

Suppose you don’t want others (or yourself) to overwrite existing values. In that case, you can add the final keyword (this declares the variable as “final” or “constant,” which means unchangeable and read-only):

Example: 

public class Main { public static void main(String[] args) { final int number = 15; number = 30; // modifying the value of the final variable will cause an error System.out.println(number); } }

Consequently, it is impossible to change the value of a String Variable if you have declared the variable with final keyword:

Example: 

public class Main { public static void main(String[] args) { final String ample = "Java"; ample = "Java Language "; // modifying the value of the final variable is not possible as the variable is declared as final System.out.println(ample); } }

Other Types

Here is a demonstration of how to declare variables of other types:

Example: 

int myNumber = 5; float myFloatNumber = 5.99f; char myLetter = 'D'; boolean myBool = false; String myText = "We are learning about Java Variables ";

Example: 

public class Main { public static void main(String[] args) {String mrx = "These are other types of Java Variables "; boolean ample_bool = true; char letter ='P'; int num_mrx = 13; float ample_float =3.45f;System.out.println("String value: "+mrx); System.out.println("Boolean value: "+ample_bool); System.out.println("character value: "+letter); System.out.println("Integer value: "+num_mrx); System.out.println("Float value: "+ample_float); } }

 

In the next chapter, you will learn more about data types.



Display Variables

Java variables are commonly displayed using the println() method.

To combine both text and a variable, use the + character:

Example: 

public class Main { public static void main(String[] args) { String country_name = "Pakistan"; System.out.println("Hello my country name is " + country_name ); } }

Example: 

public class Main { public static void main(String[] args) {int mrx_Age=25; System.out.println("My age is: "+mrx_Age); } }

A variable can also be added to another variable by using the + character:

Example: 

public class Main { public static void main(String[] args) { String first_name = "John "; String last_name = "Charles"; String full_name = first_name + last_name; System.out.println(full_name); } }

Example: 

public class Main { public static void main(String[] args) { String ample_name="James Gosling"; int mrx_Age=67;System.out.println("The Founder of Java Language is: "+ample_name+" and his age was: "+mrx_Age);} }

Numeric values are represented by the + character (notice that we use int (integer) variables):

Example: 

public class Main { public static void main(String[] args) { int number1 = 25; int number2 = 26; System.out.println(number1 + number2); // Print the value of number1 + number2 } }

As you can see from the example above:

  • The Java variable number 1 stores the value 25.
  • And variable number 2 stores the value 26.
  • Using the println() method, we display the value of variable number1 + number2, which is 51.

Example: 

public class Main { public static void main(String[] args) { int ample=12; int mrx=47; System.out.println("Sum: "+(ample+mrx)); } }

Declare Many Variables

Comma-separated lists are used to declare multiple variables of the same type:

Example: 

public class Main { public static void main(String[] args) { int num1 = 25, num2 = 35, num3 = 45; System.out.println(num1 + num2 + num3 ); } }

We can also add as many String values as we can by using the + operator:

Example: 

public class Main { public static void main(String[] args) { String mrx1="Java "; String mrx2="Programming "; String mrx3="Language ";System.out.println("After concatenation: "+mrx1 + mrx2 + mrx3 ); } }

Java Identifiers

There must be a unique name for every Java variable.

A unique name is called an identifier.

Names can be short (such as x and y) or descriptive (such as age, sum, or total volume).

Important: To create maintainable and understandable code, you should use descriptive names:

Example: 

public class Main { public static void main(String[] args) { // Good practice int hoursPerDay = 24; // OK, but not so easy to understand what h actually is int h = 24; System.out.println(hoursPerDay); System.out.println(h); } }

Example: 

public class Main { public static void main(String[] args) { // Good practice float value_PI= 3.142f; // It is not wrong to write the variable name as p but as it does not make any sense hence we should try to avoid it float p = 3.142f; System.out.println(value_PI); System.out.println(p); } }

Names for Java variables (unique identifiers) are constructed according to the following rules:

  1. In naming a variable, you can use letters, digits, underscores, and dollar signs.
  2. There must be a letter at the beginning of a name.
  3. The first letter of a name should be lowercase, and it cannot contain any whitespace.
  4. Names can also begin with $ and  _(but we will not use them in this tutorial).
  5. Variable names are case sensitive (“myVar” and “myvar” are different variables).
  6. There are certain words that cannot be used as names (like Java keywords, for example int or boolean).
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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