More about Variable in Java

Now before going to discuss what a variable is in Java in the context of programming language. We should know what the variable word means in the context of Programming language.

Variable

Variable in General Programming language

You can say a Variable is a particular container with a datatype and name, and a value that can be stored in it is called a variable. We can do multiple operations on it depending on their datatypes. Now, what is a data type? We shall discuss this in the next post. Now consider a Variable as a container or a thing that can store values means it can be of any datatype, i.e., it can be alphabets, it can be a string, it can be an integer, it can be a floating point number or complex number okay look here below the syntax of variable in general programming languages:

Syntax of Variables in General Programming language

I hope you are aware of the word Syntax. If not so, look, Syntax is a structure or way of writing particular code in a particular programming language. So for more confirmation and knowledge, you can check our detailed post on its Syntax in C#, C++, JAVA, and Python. Now here below is the Syntax of a Variable okay:

[varaible data type] + [varaible name] = [value of variable]

I also make visual representation of a variable hope you like it.

Variable

For example : making integers variable age and assigning it value 18 okay using C# syntax.

int age =18;

Variable in Java Programming language

Now see how ca we declare a variable in Java it is same like as in C# . First we have to create or choose type of variable or you can say datatype of a variable then it’s name and it’s value.

Syntax of Variable in Java Programming language

[varaible data type] + [varaible name] = [value of variable]

I also make visual representation of a variable hope you like it.

Variable

For example : making integers variable age and assigning it value 18 okay using Java syntax.

int age =18;

Other Types of Variables

We can create other types of variables on behalf on their datatypes so look below how can we create other datatypes variables we will also see in next post what are different datatypes are in java.

Here below you can see demonstration of other datatypes variables:

package my package

class myclass
{
public static void main String args[]
{
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";

System.out.prinln(myNum);
System.out.prinln(myFloatNum);
System.out.prinln(myLetter);
System.out.prinln(myBool);
System.out.prinln(myText);
}

}

Output:

5
5.99
D
true
Hello

How to create many or multiple Variables in Java

We can declare more than one variable of the same type, there are two ways see below:

First way:

public class Main {
  public static void main(String[] args) {
    int x = 1;
    int y = 6;
    int z = 50;
    System.out.println(x + y + z);
  }
}

Output:

57

Second way(it is recommended):

public class Main {
  public static void main(String[] args) {
    int x = 1, y = 6, z = 50;
    System.out.println(x + y + z);
  }
}

Output:

57

How to assign one value to multiple variables in Java

We can also assign the same value to multiple variables in one line so this reduce our line of code:

First way:

public class Main {
  public static void main(String[] args) {
    int x, y, z;
    x = 10;
    y = 10;
    z = 10
    System.out.println(x + y + z);
  }
}

Output:

30

Doing instead of this we can also do that:

Second way(it is recommended):

public class Main {
  public static void main(String[] args) {
    int x, y, z;
    x = y = z = 10;
    System.out.println(x + y + z);
  }
}

Output:

30

Identifiers in Java

All Java variables can be of any datatype and must be identified with unique names. These impressive names are called identifiers. They can be short names (like a and b) or more descriptive names (age, sum, total volume).

Note: It is recommended to avoid short names; instead of this, use descriptive names/variable names to create understandable and maintainable code:

public class Main {
  public static void main(String[] args) {
    // Good
    int secondsPerminute = 60;

    // OK, but not so easy to understand what s actually is
    int s = 60;
    
    System.out.println(minutesPerHour);
    System.out.println(m);
  }
}

Output:

60
60

Rules for naming Variables:

The general rules for naming a variables in Java are:

1-Names can contain letters(a,b,c etc), digits(1,2,3), underscores(_), and dollar signs($ etc)
2-Names must begin with a letter.
3-Names should start with a lowercase letter and it cannot contain whitespace
4-Names can also begin with $ and _ (but we will not use it in this blog)
5-Names are case sensitive (“codelikechamp” and “codelikeChamp” are different variables)
6-Reserved words (like Java keywords, such as int or boolean) cannot be used as names

Link: https://Codelikechamp.com

Medium Link: Follow me on Medium

Linkedin Link: Follow me on Linkedin

🤞 Don’t miss any latest posts!

Please subscribe by joining our community for free and stay updated!!!

IF YOU HAVE ALREADY SUBSCRIBED JUST CLOSE THIS FORM !

Leave a Comment

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

Scroll to Top