//
write a java program to verify the default values of primitive data types
public class CLASSVARIABLES {
static int i;
float f;
char c;
byte b;
short s;
long l;
double d;
boolean bo;
void fun()
{
System.out.println(i);
System.out.println(f);
System.out.println(c);
System.out.println(b);
System.out.println(s);
System.out.println(l);
System.out.println(d);
System.out.println(bo);
}
public static void main(String[] args) {
CLASSVARIABLES cl = new CLASSVARIABLES();
cl.fun();
System.out.println("in side main");
System.out.println(i);
}
}
//write
a java program to convert one data type values into another
public class CONVERSIONS {
public static void main(String[] args) {
int i=20;
System.out.println(i);
float f=i;
System.out.println(f);
//long l=f;
cannot convert from float to long
long l = i;
System.out.println(l);
double d=l;
System.out.println(d);
double d1=i;
System.out.println(d1);
double d2=f;
System.out.println(d2);
// short s=i;
cannot convert int to short
boolean b=false;
System.out.println(b);
// int i1=b; cannot convert from boolean to int
}
}
Note:
Java does not support garbage values. Every
local variable must be initialized. If the data members of a class are not
initialized, JVM assign the default values for the variables. The default
values for Primitive data types are:
byte->0
short->0
int->0
long->0
float->0.0F
double->0.0
char->
" "(blank space)
boolean->false
NOTE
:
Any integer value assigned to a variable is by default treated as ‘int’ and the
floating-point value as ‘double’ by Java. That is why, a float value is
assigned to a variable of ‘float’ datatype using the letter ‘F’ as follows:
float pi = 3.1428F;
//
write a program to initialize the primitive data types
public class
PRIMITIVETYPES {
public
static void main(String[] args) {
int i=10;
float f=20.0f;
char c='r';
byte b=40;
short s=1000;
long l=200000;
double d=2000.34;
boolean bo=true;
System.out.println(i);
System.out.println(f);
System.out.println(c);
System.out.println(b);
System.out.println(s);
System.out.println(l);
System.out.println(d);
System.out.println(bo);
}
}