Java Variables

In this tutorial, You'll learn about java variables. Also learn final variable, rules for identifiers in java

java variables

 

Types of Java Variables

Variables are a box or container in a memory of device to store some data values in it.

In java, we have different types of java variables for different types of data are follows:
  1. int - This stores integers without decimal points.
  2. float - This stores floating point numbers.
  3. char - This stores single character in it and characters are surrounded with single qoutes like 'A' or 'b'.
  4. string - This stores sequence of character or we can say text and it surrounded with double qoutes like "codeamy" or "divya".
  5. boolean - This stores only two values are true or false.

Declaring of Variables

To create a java variables, we have to follow a syntax in which we have to specify the type of container and assign it data or value.
 

Syntax for variables

type variable_name  = value; 

where type is one of the type as we learn above like int, float, char, etc. variable_name is a unique name of container such as x or divya. so, we can uniquely identify it. The equal sign(=) is used to assign the value to variable_name.
 
int divya = 10;
 
We can also declare variable without assigning value, and assigning later.
 
int x;
x = 10;
System.out.println(x);  // output : 10
 
We can also overwrite the assigned value like
 
int x = 10;
x = 20;
System.out.println(x); // output : 20

Display Variables

To print or display the value of java variables. Java language provide us the method println().
for examples,
 
int divya = 27;
System.out.println("roll no." + divya);
 
To combine the string with variables, we use + operator.
 
String lastName = "Soni";
String firstName = "Divya";
String Name = firstName + lastName;
System.out.println(Name);

we also use + to combine one or more variables.
but in case of numeric it will add the variables. let us see in below example.
 
int a = 10;
int b = 20;
System.out.println(a+b);  // output will be 30 not 10 20 

Declare many variables

We can declare many variables in single line but with same type with use of comma(,) in between.
for example,

int x = 10, y = 20, z = 30;
System.out.println(x + y + z);

final in Java

final is a keyword or reserved word in java. It is also denote as constant. If you don't want to change your assign value to a variableName then put final keyword in front of declaration.

final int x = 01;
x = 02; // output : It will generates error that you can't reassign the final variable.

Java Identifiers

All variables are identified with unique name. These unique names are known as Identifiers. There certain rules for defining a valid identifiers otherwise it generates compile-time error.

Rules for identifiers in Java

  1. The first character must be Letter(A-Z or a-z), digits, dollar sign($) or Underscore( _ ).
  2. Every Java variable must start with a letter or underscore i.e. it can’t start with digits( 0-9 ).
  3. Reserved words or keywords are not used as variables name. For example, String int = "veer"; is not valid because int is a reserved word.
  4. Variable names are case sensitive both upper and lower cases treated as different. 
  5. Blankspace or whitespace in variable names is not allowed.  
In above article, we learn abut Java Variables and also learn about Java Identifiers, I think the whole article clear your doubt. In upcoming tutorials, I will share details of Java Data Types.

No comments:

Post a Comment

If you have any doubts, Please let me know