java variables

By | April 6, 2019

Java Variable-: A variable is a set of characters that can be associated with a value. It has a type. The set of values that can be assigned to it are restricted to a certain interval group of values or must follow a certain form defined by that type.In Java, there are three types of variables.

  • fields are variables defined in class bodies, outside of method bodies and that do not have the keyword static in front of them
  • local variables are variables declared inside method bodies, they are relevant only in that context
  • static variables are variables declared inside class bodies with the have the keyword static in front of them. If they are declared as public they are accessible globally.

 

Declare a variable in Java-: To declare a variable in Java, we first have to specify what kind of variable we are going to be using. Variables come in a number of different types. The content with using whole numbers, that is, numbers that do not have a specified decimal place and are not  fractions. In this case it is appropriate to use one of Java’s primitive types. These are essentially as base level as we can get with information in the Java programming language, just about everything else we work with in Java is built of the primitive types.

To declare a variable of the integer primitive type whole numbers, we use the int keyword, all lowercase. Once we do this, we need to give our variable a name. This is a unique identifier that we’ll use to access this piece of information in future. Each variable in our local program should have its own name.

 

Java variable example -:

package variables; 
 
public class Variables { 
 
    public static void main(String[] args) { 
        System.out.println(1+1); 
    } 
     
} 

Output of this Programme

2

 

Leave a Reply

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