Header Ads

Variables in Java with examples

Java variable 

Java variable is a named location in memory that can hold a value of a particular data type. Variables are used to store data and manipulate it during program execution.

We have three types of instance, static and local variables.

Types of datatypes in java

  •  Primitive
  •  Non-primitive





Variable

A variable is a name assigned to a reserved section of memory in a computer program. A memory location is a specific physical address in a computer's memory where data is stored. The term "variable" comes from the fact that the value stored in this memory location can vary, or change, as the program executes.

Suppose:



Java variables have a specific scope that defines their visibility and accessibility within the program.

A variable can have a local or global scope, which can defines its accessibility and usability within a program.

Local variables

Local variables are declared inside a method, constructor, or block of code and can only be accessed within that scope.

Global variables

Global variables are declared outside of any method, constructor, or block of code and can be accessed throughout the program.

Static variables 

Java variables can also have different modifiers, such as public, private, static, final, and volatile, which can affect their accessibility and behavior within the program.

Declaring a variable in Java involves specifying the variable's data type, name, and optional initial value. For example:

int data= 10; // declares an integer variable named data with an initial value of 10.

Understand the java variables using code:



public class A  
{  public int data = 500; // global variable 
    static int m=100;//static variable  
     public static void main(String args[])  
    {  
        int data=50;//instance variable    
    } 
    void method()  
    {    
        int n=500;//it is local variable    
    }  
   
}//end  


Lets Do some practice examples


Example of Subtract two numbers in java :



public class SubtractTwoNumbers {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 5;
        int r = num1 - num2;

        System.out.print("Result:"); 
        System.out.print(r);// prints "Result: 5"
        
        
    }
}
Result of Previous code:

Result  : 5


One example with Typecasting

What is Typecasting?


Typecasting, also known as type conversion, is the process of converting a value of one data type to another data type. In Java, there are two types of typecasting: implicit typecasting (also known as widening conversion) and explicit typecasting (also known as narrowing conversion).
 

public class SubtractTwoNumbers {
    public static void main(String[] args) {
      double d = 10.5;
	  int i = (int) d; // explicit typecasting from double to int
	  System.out.println("Double value: " + d);
	  System.out.println("Int value: " + i);
    }
}

Output of that code


Double value: 10.5
Int value: 10

Explain: 
This is because we initialize the double variable d with the value 10.5, and then use explicit typecasting to convert it to an int value. Since int values do not have decimal places, the fractional part of d is truncated and the resulting int value is 10. Finally, we print both the original double value and the resulting int value to the console using the System.out.println() method. 

Scope of Variables in Detail : of (Local, instance , Global Scope)

In Java, variables can have different scopes, which determine where the variable can be accessed within a program. There are three types of scope in Java:

Local scope:

A variable declared inside a method, block, or constructor has local scope and can only be accessed within that method, block, or constructor.


Instance scope: 

A variable declared inside a class but outside any method has instance scope and can be accessed by any method or constructor in the class.

Class scope: 

A variable declared with the "static" keyword has class scope and can be accessed by any method or constructor in the class, as well as from outside the class. 

Examples:


Example of Local Scope and how to write it:





// Local scope
	public class LocalScopeExample {
    
		public void method() {
        
			int x = 5; // x has local scope and can only be accessed within this method
            
			System.out.println(x);
            
		}
	}


Example of Instance Scope and how to write it:


	public class InstanceScopeExample {
		private int x; // x has instance scope and can be accessed by any method or constructor in this class
        
        public void setX(int value) {
   			 x = value;
		}

		public void printX() {
    		System.out.println(x);
		}
	}
        
        


Example of Class Scope and how to write it:


	// Class scope
	public class ClassScopeExample {
		private static int x; // x has class scope and can be accessed by any method or constructor in this class, as well as from outside the class
        public static void setX(int value) {
   			 x = value;
		}

		public static void printX() {
   			 System.out.println(x);
		}
     }
        
        

5
 Next: 

Later ;

Abstract class variables in java

Powered by Blogger.