wellnwill Marathahalli Bangalore

Variable and DataType Program


Examples :

class Test
{
public static void main(String args[])
{
int for=10;
System.out.println(for);
}
}
class Test
{
public static void main(String args[])
{
int if=10;
System.out.println(if);
}
}         
class Test
{
public static void main(String args[])
{
int 1id=10;
System.out.println(1id);
}
} 


        
class Test3{
  boolean bo;
  byte by;
  short sh;
  int in;
  long lo;
  float fl;
  double d;
  String st;  // st is reference variable of type String
  Test3 t;   // t is reference variable of type Test3
  void display() {
  System.out.println(bo);
  System.out.println(by); 
  System.out.println(sh);
  System.out.println(in);
  System.out.println(lo);
  System.out.println(fl);
  System.out.println(d);
  System.out.println(st);
  System.out.println(t);
 }
  public static void main(String args[]){                                                                 
    Test3 t1=new Test3();
    t1.display();
  }
}     
// O/P - false,0,0,0,0,0.0,0.0,null,null
Note-  Printing the default values. 
class Test1
{
   public static void main(String[] args)
   {
       System.out.println("Welcome to java");
   }
}

// O/P -  Welcome to java
class Test2
{
  char c;
  void display()
  {
     System.out.println(c==0); //true
     System.out.println(c==' '); //false
  }
  public static void main(String a[])
  {
     Test2 t=new Test2();
     t.display();
  }
}

// O/P - true,false 
// Note - Default value of char data type is either 
          ascii-0 or \u0000. It is not a space.
          
              
class Test3
{
  boolean bo;
  byte by;
  short sh;
  int in;
  long lo;
  float fl;
  double d;
  String st;  // st is reference variable of type String
  Test3 t;   // t is reference variable of type Test3
  void display() {
  System.out.println(bo);
  System.out.println(by); 
  System.out.println(sh);
  System.out.println(in);
  System.out.println(lo);
  System.out.println(fl);
  System.out.println(d);
  System.out.println(st);
  System.out.println(t);
 }
  public static void main(String args[]) {                                                                 
    Test3 t1=new Test3();
    t1.display();
  }
}     
// O/P - false,0,0,0,0,0.0,0.0,null,null
Note-  Printing the default values. 
class Test4 
{
   public static void main(String args[])
   {
       int i;  // local variable
      System.out.println(i);
   }
}
// O/P - CompileTimeError 
      variable i might not have been initialized
Note - We must initialize local variable before using.

class Test5
{
   short i;
   public static void main(String args[])
   {
      System.out.println(i);
   }
} 
 
// O/P - CompileTimeError 
         non-static variable i cannot be referenced from a static context.
Note - We can not use instance variable from static method directly.
class Test6
{
  static short i;
  public static void main(String args[])
  {
     System.out.println(i);
  }
}
// O/P - 0//default value of short i.




class Test7 
{
   public static void main(String argss[])
   {
     byte b=0, short sh=0;
     System.out.println(b);
     System.out.println(sh);
   }
}

// O/P - Compile time error.
Note - We can not declare multiple data types in single line. 
class Test
{
  static short i;
  public static void main(String args[])
  {
     System.out.println(i);
  }
}
// O/P - 0//default value of short i.




class Test
{
public static void main(String args[])
{
int id1=10;
System.out.println(id1);
}
}





class Test
{
  static short i;
  public static void main(String args[])
  {
     System.out.println(i);
  }
}
// O/P - 0//default value of short i.




class Test
{
public static void main(String args[])
{
int $id=10;
int _age=20;
int $_salary=30;
System.out.println($id);
System.out.println(_age);
System.out.println($_salary);

}
}
class Test
{
public static void main(String args[])
{
int String=10;
System.out.println(String);
}
}

class Test
{
public static void main(String args[])
{
int System=20;
java.lang.System.out.println(System);
}
}

class Test
{
public static void main(String args[])
{
int String=10;
System.out.println(String);
}
}

class Test
{
public static void main(String args[])
{
int Test=20;
System.out.println(Test);
}
}

class Test
{
public static void main(String args[])
{
{
int A12=10
}
System.out.println(A12);
}
}

Literals

A constant value which can be assigned to a variable is known as literal.

Footer with Map

Top