If T is a primitive type, then a variable of type "array of T" can hold a null reference or a reference to any array of type "array of T".
If T is a reference type, then a variable of type "array of T" can hold a null reference or a reference to any array of type "array of S" such that type S is a subclass or subinterface of type T.
A variable of type Object[] can hold a reference to an array of any reference type.
A variable of type Object can hold a null reference or a reference to any object, whether it is an instance of a class or an array.
Ilakya Mukunth wrote:
1)are my assignments valid? I compile this code. it works fine. no compile time error
2)The commented lines did not compile. Can anyone explain the reason why?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ilakya Mukunth wrote:1. can we assign a one type of primitive variable to another primitive variables?
public class PrimitivesAssign
{
public static void main(String[] args)
{
PrimitivesAssign a = new PrimitivesAssign();
a.test();
a.test2();
}
void test()
{
byte b=10;
short s =10;
char c =10;
int i =10;
long l =10;
float f =10,ff=10.0f;
double d = 10;
d=f; d=l; d=f; d=i; d=s; d=b; d=c;
f=l; f=i; f=c; f=s; f=b;
l=i; l=c; l=s; l=b;
i=c; i=s; i=b;
s=b;
}
}
are my assignments valid? I compile this code. it works fine. no compile time error
2. can we assign a one type of primitive array reference variable to another primitive array reference variables?
public class PrimitivesAssign
{
public static void main(String[] args)
{
PrimitivesAssign a = new PrimitivesAssign();
a.test();
a.test2();
}
void test2()
{
byte b[] = new byte[4];
short s[]= new short[4];
char c[]=new char[4];
int i[]= new int[4];
long l[]=new long[4];
float f[]= new float[4];
double d[] = new double[4];
//d=f; d=l; d=f; d=i; d=s; d=b; d=c;
//f=l; f=i; f=c; f=s; f=b;
//l=i; l=c; l=s; l=b;
//i=c; i=s; i=b;
//s=b;
}
}
The commented lines did not compile. Can anyone explain the reason why?
Himai Minh wrote:
Ilakya Mukunth wrote:1. can we assign a one type of primitive variable to another primitive variables?
public class PrimitivesAssign
{
public static void main(String[] args)
{
PrimitivesAssign a = new PrimitivesAssign();
a.test();
a.test2();
}
void test()
{
byte b=10;
short s =10;
char c =10;
int i =10;
long l =10;
float f =10,ff=10.0f;
double d = 10;
d=f; d=l; d=f; d=i; d=s; d=b; d=c;
f=l; f=i; f=c; f=s; f=b;
l=i; l=c; l=s; l=b;
i=c; i=s; i=b;
s=b;
}
}
are my assignments valid? I compile this code. it works fine. no compile time error
2. can we assign a one type of primitive array reference variable to another primitive array reference variables?
public class PrimitivesAssign
{
public static void main(String[] args)
{
PrimitivesAssign a = new PrimitivesAssign();
a.test();
a.test2();
}
void test2()
{
byte b[] = new byte[4];
short s[]= new short[4];
char c[]=new char[4];
int i[]= new int[4];
long l[]=new long[4];
float f[]= new float[4];
double d[] = new double[4];
//d=f; d=l; d=f; d=i; d=s; d=b; d=c;
//f=l; f=i; f=c; f=s; f=b;
//l=i; l=c; l=s; l=b;
//i=c; i=s; i=b;
//s=b;
}
}
The commented lines did not compile. Can anyone explain the reason why?
question 1:
assiging a smaller variable to a bigger variable is ok. For instance, assigning an integer to a double is ok. A double can hold 64 bits. An integer is almost 32 bits.
But if you assign a double to an integer without casting, it won't compile. A 32-bit integer cannot hold a 64-bit double.
question 2:
assigning a type to a different type is not ok. double[] is an object type. int[] is another object type.
Just like Double d = new Integer(); It is a type mismatch error.
Himai Minh wrote:
Ilakya Mukunth wrote:1. can we assign a one type of primitive variable to another primitive variables?
public class PrimitivesAssign
{
public static void main(String[] args)
{
PrimitivesAssign a = new PrimitivesAssign();
a.test();
a.test2();
}
void test()
{
byte b=10;
short s =10;
char c =10;
int i =10;
long l =10;
float f =10,ff=10.0f;
double d = 10;
d=f; d=l; d=f; d=i; d=s; d=b; d=c;
f=l; f=i; f=c; f=s; f=b;
l=i; l=c; l=s; l=b;
i=c; i=s; i=b;
s=b;
}
}
are my assignments valid? I compile this code. it works fine. no compile time error
2. can we assign a one type of primitive array reference variable to another primitive array reference variables?
public class PrimitivesAssign
{
public static void main(String[] args)
{
PrimitivesAssign a = new PrimitivesAssign();
a.test();
a.test2();
}
void test2()
{
byte b[] = new byte[4];
short s[]= new short[4];
char c[]=new char[4];
int i[]= new int[4];
long l[]=new long[4];
float f[]= new float[4];
double d[] = new double[4];
//d=f; d=l; d=f; d=i; d=s; d=b; d=c;
//f=l; f=i; f=c; f=s; f=b;
//l=i; l=c; l=s; l=b;
//i=c; i=s; i=b;
//s=b;
}
}
The commented lines did not compile. Can anyone explain the reason why?
question 1:
assiging a smaller variable to a bigger variable is ok. For instance, assigning an integer to a double is ok. A double can hold 64 bits. An integer is almost 32 bits.
But if you assign a double to an integer without casting, it won't compile. A 32-bit integer cannot hold a 64-bit double.
question 2:
assigning a type to a different type is not ok. double[] is an object type. int[] is another object type.
Just like Double d = new Integer(); It is a type mismatch error.
Himai Minh wrote:
Ilakya Mukunth wrote:1. can we assign a one type of primitive variable to another primitive variables?
public class PrimitivesAssign
{
public static void main(String[] args)
{
PrimitivesAssign a = new PrimitivesAssign();
a.test();
a.test2();
}
void test()
{
byte b=10;
short s =10;
char c =10;
int i =10;
long l =10;
float f =10,ff=10.0f;
double d = 10;
d=f; d=l; d=f; d=i; d=s; d=b; d=c;
f=l; f=i; f=c; f=s; f=b;
l=i; l=c; l=s; l=b;
i=c; i=s; i=b;
s=b;
}
}
are my assignments valid? I compile this code. it works fine. no compile time error
2. can we assign a one type of primitive array reference variable to another primitive array reference variables?
public class PrimitivesAssign
{
public static void main(String[] args)
{
PrimitivesAssign a = new PrimitivesAssign();
a.test();
a.test2();
}
void test2()
{
byte b[] = new byte[4];
short s[]= new short[4];
char c[]=new char[4];
int i[]= new int[4];
long l[]=new long[4];
float f[]= new float[4];
double d[] = new double[4];
//d=f; d=l; d=f; d=i; d=s; d=b; d=c;
//f=l; f=i; f=c; f=s; f=b;
//l=i; l=c; l=s; l=b;
//i=c; i=s; i=b;
//s=b;
}
}
The commented lines did not compile. Can anyone explain the reason why?
question 1:
assiging a smaller variable to a bigger variable is ok. For instance, assigning an integer to a double is ok. A double can hold 64 bits. An integer is almost 32 bits.
But if you assign a double to an integer without casting, it won't compile. A 32-bit integer cannot hold a 64-bit double.
question 2:
assigning a type to a different type is not ok. double[] is an object type. int[] is another object type.
Just like Double d = new Integer(); It is a type mismatch error.
Ilakya Mukunth wrote:How can you assign char to an byte? byte is 8 its and char is 16 bits. Can you explain. The line 4 will produce the compile time error
Ilakya Mukunth wrote:
Clarification needed in Question-1
---------------------------------------
I tried to assign final char, final short to byte variables, it compiles just fine. Please have a look at the following program
If I uncomment the ,line 3 and 4, it shows the following errors
Test.java:14: possible loss of precision
found : short
required: byte
byte b=s;
^
Test.java:15: possible loss of precision
found : char
required: byte
b=c;
^
2 errors
Please clarify
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|