1. Assignment operator always returns the value which is assigned. Here I am giving some examples.
public class Test
{
public static void main(String[] args)
{
boolean a = true;
float b = 0.0f;
double c = 0.0;
int d = 0;
if(a = false)
{
System.out.print("\nThis is will not executed");
}//if
else
{
System.out.print("\nThis is false : " + (a = false));
}//else
if(a = true)
{
System.out.print("\nThis is will executed now because (a = true) = " + (a = true));
}//if
else
{
System.out.print("\nThis is will not executed");
}//else
/* In java if(condition) statement will always need boolean value
* so following all code will fail to compile
* if(b = 2.3)
* if(c = 4.3)
* if(d = 3)
* Lets see what will be return value for these
*/
System.out.print("\n(b = 2.3) = " + (b = 2.3f));
System.out.print("\n(c = 4.3) = " + (c = 4.3));
System.out.print("\n(d = 3) = " + (d = 3));
}//PSVM
}//class Test
2. For down cast you will need explicit casting.
public class Test
{
public static void main(String [] args)
{
byte b = 10;
short s = 20;
int i = 30;
float f = 40f;
double d = 50;
// Here are few valid assignments
d = f;
f = i;
i = s;
s = b;
//Here are few invalid assignments
b = s;
s = i;
i = f;
f = d;
//To make this working you have to cast explicitly
b = (byte)s;
s = (short)i;
i = (int)f;
f = (float)d;
}//PSVM
}//class Test
3. Only byte, short, int, char, String and enum is allowed in switch case. Here I am giving example.
public class Test
{
public static void main(String args[])
{
String f ="0";
switch (f)
{
}
int f1 = 0;
switch (f1/2)
{
}
byte f2 = 0;
switch (f2 + 3)
{
}
short f3 = 0;
switch (f3)
{
}
char f4 = '0';
switch (f4)
{
}
/*
It will fail for float, double, long
float f4 = 2.3f;
switch(f4)
{
}
*/
}
}
public class Test
{
public static void main(String[] args)
{
boolean a = true;
float b = 0.0f;
double c = 0.0;
int d = 0;
if(a = false)
{
System.out.print("\nThis is will not executed");
}//if
else
{
System.out.print("\nThis is false : " + (a = false));
}//else
if(a = true)
{
System.out.print("\nThis is will executed now because (a = true) = " + (a = true));
}//if
else
{
System.out.print("\nThis is will not executed");
}//else
/* In java if(condition) statement will always need boolean value
* so following all code will fail to compile
* if(b = 2.3)
* if(c = 4.3)
* if(d = 3)
* Lets see what will be return value for these
*/
System.out.print("\n(b = 2.3) = " + (b = 2.3f));
System.out.print("\n(c = 4.3) = " + (c = 4.3));
System.out.print("\n(d = 3) = " + (d = 3));
}//PSVM
}//class Test
2. For down cast you will need explicit casting.
public class Test
{
public static void main(String [] args)
{
byte b = 10;
short s = 20;
int i = 30;
float f = 40f;
double d = 50;
// Here are few valid assignments
d = f;
f = i;
i = s;
s = b;
//Here are few invalid assignments
b = s;
s = i;
i = f;
f = d;
//To make this working you have to cast explicitly
b = (byte)s;
s = (short)i;
i = (int)f;
f = (float)d;
}//PSVM
}//class Test
3. Only byte, short, int, char, String and enum is allowed in switch case. Here I am giving example.
public class Test
{
public static void main(String args[])
{
String f ="0";
switch (f)
{
}
int f1 = 0;
switch (f1/2)
{
}
byte f2 = 0;
switch (f2 + 3)
{
}
short f3 = 0;
switch (f3)
{
}
char f4 = '0';
switch (f4)
{
}
/*
It will fail for float, double, long
float f4 = 2.3f;
switch(f4)
{
}
*/
}
}