Variable Default Values – CSKC
Sign In

Search

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

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test Java

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

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