Variable Default Values – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #JAVA#3867    siteicon   siteicon  

Variable Default Values

Java class variables have default values. Let us experiment with that.

Learning Objectives

  • Understanding the default values of java class variables.

Source Code

TC++ #3867

Source Code

class VarDefaultValues
{
static byte b;
static short s;
static int i;
static long l;
static char c;
static double d;
static float f;
static boolean bool;
public static void main(String[] args)
{
System.out.println("Default value of byte "+b);
System.out.println("Default value of short "+s);
System.out.println("Default value of int "+i);
System.out.println("Default value of long "+l);
System.out.println("Default value of char "+c);
System.out.println("Default value of double "+d);
System.out.println("Default value of float "+f);
System.out.println("Default value of boolean "+bool);
}
}

Test it !

Run Output

1
2
3

Code Understanding

class VarDefaultValues
{
static byte b; static short s; static int i; static long l;
static char c; static double d; static float f; static boolean bool;
All the 8 types of primitive data types have been defined as class/static variables without any initialisation

public static void main(String[] args)
{
System.out.println(“Default value of byte “+b);
System.out.println(“Default value of short “+s);
System.out.println(“Default value of int “+i);
System.out.println(“Default value of long “+l);
Default values of integer types byte,short,int and long is 0.

System.out.println(“Default value of char “+c);
Of character type it is ‘u0000’ which is basically the null character of ascii code. This would be unprintable.

System.out.println(“Default value of double “+d);
System.out.println(“Default value of float “+f);
For decimal types float and double it is 0.0.

System.out.println(“Default value of boolean “+bool);
And for boolean type  it is false.

}
}

Notes


Suggested Filename(s): VarKinds.java



Share

sunmitra| Created: 20-Mar-2018 | Updated: 20-Mar-2018|






Back