Variables In Java

  1. Variables are use to store values.
  2. Variables can be use in a math expression.
  3. Variables can be use to show values to user.
  4. Variables can be use to assign value to another variable.
  5. Variable creation is done in 2 steps
    1. First step is variable declaration
    2. Another step is initialization of variable.
  6. Syntax:

Data_Type   Variable_Name = value;

 

Casting in Primitive Data type

Casting is use to convert value from one data type to another.

There are 2 types of casting

  1. Implicit casting

    1. Casting done automatically/internally by java
    2. No needs to write any extra code to do this type of casting.
  2. Explicit Casting

    1. Casting which has to be done by developer.
    2. Need to write code manually to achieve this casting.
    3. You may get an logically incorrect output if it is done in wrong way.

 

 

Different type of values store in Variables

  1. Decimal value

    1. All whole numbers.
    2. Ex : 10, 20 ,123
  2. Binary Value (JDK 1.7 onwards)

    1. Always start with 0B or 0b
    2. It contains only 0 or 1 digits.
    3. Ex : 0B1010, 0b0101
  3. Octal Value

    1. Always start with 0
    2. It contains only 0 to 7 digits.
    3. Ex : 027, 033
  4. Hex-Decimal Value

    1. Always start with 0X or 0x
    2. It contains only 0 to 15 numeric digit (for 10 to 15 use A-E)
    3. Ex : 0XABC, 0x123ABC, 0xdad

 

Example :-

public class VariableValueType
{
	public static void main(String args[])
	{
		int dec = 22;
		int binary = 0B0101;
		int octal = 027;
		int hex = 0XABC;

		System.out.println(dec);
		System.out.println(binary);
		System.out.println(octal);
		System.out.println(hex);
	}
}

 

 

Type of Variable

Type of variable, decided on the bases of the variable declaration location.

  1. Local Variable
    1. Which is created inside method or at method declaration level.
  2. Instance variable
    1. Which is created inside class and outside any method.
  3. Class/Static variable
    1. Which is created inside class and outside any method with static keyword.

 

Example :-

public class VariableTypes
{
	int b = 30; // Instance Variable
	static int c = 40; // Static / Class Variable

	public static void main(String args[])
		{
		int a = 20; // Local Variable
		System.out.println(a);
		System.out.println(c);
		System.out.println(b);
		}
	
}

 

 

Join Telegram : Click Here

 

All Full Stack Java Study Material

 

Java the programing language

 

Job’s For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Them !