Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Variable and Constant in Java:-





Variable or Data Member:-  It is a special identifier whose value can be changed at compile-time or runtime.

Variable will be declared by

 access specifier +modifier + datatype + identifier

int x =10 ;    //default instance int x=10 dynamic memory allocation using object

static int y=20 //  default static int y=20 one-time memory allocated at compile time


Type of variable:-

1 instance or Dynamic:-

It will be declared without any modifier and it will be allocated memory at the time of execution by Object.

a variable which will be declared underclass or under instance method will be instance variable

the variable is also called data members in OOP'S Programming.


class A
{
    int x=10; //global instance 
    void fun()
    {
       int y=20; //local instance variable
    }

}

A obj = new A();  //object


Complete Explanation by Program:-

class Add
{
   int a=100,b=200,c;
   public static void main(String args[])
   {
      Add obj = new Add();
      obj.c=obj.a+obj.b;
      System.out.println(obj.c);
      Add obj1 = new Add();
      obj1.a=10;
      obj1.b=20;
      obj1.c=obj1.a+obj1.b;
      System.out.println(obj1.c);
   } 


}



2 Static:-

This type of variable will allocate memory after compile-time, static has constant memory but not the constant value, value can be changed in the same memory multiple times.

static datatype identifier=value

static int x=100;
x=10;
x=12;


we can define static variable underclass and instance method by static keyword. if we define a static variable under static method then no need to declare a static keyword.


class A
{
    static int a=100;
    void fun()
    {
        static int b=20;
    }

   public static void main()
   {
      int c=30;
   }

}

Program of Static With Complete explanation?

class Add
{
   static int a=100,b=200,c;
   public static void main(String args[])
   {
       c=a+b;
      System.out.println(c);
      a=10;
      b=20;
      c=a+b;
      System.out.println(c);
   } 


}


.........................................................................................................................................
Constant:-
....................................................................................................................................................

If we can not change the value of the identifier at compile-time or run-time then it is called constant.

java provides the final keyword to a constant declaration.

final static datatype variablename=value;

constant memory always will be static by default.


class ConstDemo
{
   final static int A=100;
     
   public static void main(String args[])
   {
       A=20;
       System.out.println(A);

   }


}





Assignment:-

1)WAP to calculate electricity bill where unit price and total consumption will be assigned by the users?


2)  WAP to calculate salary where basic, ta, da, comm, pf,hra,noofleave will be assigned by the users?

3)  WAP to calculate the multiplication of complex numbers?

The solution to this program:-
class Complex
{

    public static void main(String args[])
    {
       int a,b,c,d,r,i;
       a=2;
       b=3;
       c=4;
       d=5;
       r = a*c-b*d;
       i = a*d+b*c;
       System.out.println(r + "+" + i + "i");
  
    }


}


Program to perform the addition of complex numbers?

class Complex
{

    public static void main(String args[])
    {
       int a,b,c,d,r,i;
       a=2;
       b=3;
       c=4;
       d=5;
       r = a+c;
       i = b+d;
       System.out.println(a + "+" + b + "i");
       System.out.println(c + "+" + d + "i");
       System.out.println(r + "+" + i + "i");
  
    }


}

4)  WAP to find the maximum number in any three-digit number?
int a= 457 ;  //7


5)  WAP to find middle numbers using three-digit numbers?


6)  WAP to calculate the area of a circle where pi will be constant and radius will be variable?


7)  WAP to calculate simple interest using instance type variable?


8)  WAP to convert temperature from Celsius to Fahrenheit using static data members globally?


c/5 = f-32/9














4 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. class Electro
    {
    public static void main(String args[])
    {
    int up=10,c=300;
    int eb=up*c;
    System.out.println(eb);
    }
    }

    ReplyDelete
  2. class Maxt
    {
    public static void main(String args[])
    {
    int a=247;
    int d1,d2,d3,max;
    d3=a%10;
    a=a/10;
    d2=a%10;
    a=a/10;
    d1=a;
    max=d1;
    if(max<d2)
    {
    max=d2;
    }
    if(max<d3)
    {
    max=d3;
    }
    System.out.println("Highest Digit is: "+max);
    }
    }

    ReplyDelete
  3. class Middle
    {
    public static void main(String args[])
    {
    int a=247;
    int m;
    a=a/10;
    m=a%10;
    System.out.println("Middle Digit is: "+m);
    }
    }

    ReplyDelete
  4. class Area
    {
    public static void main(String args[])
    {
    double r=14;
    double area= Math.PI*r*r;
    System.out.println("Area of circle with radius "+r+" is: "+area);
    }
    }

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post