• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

primitive & primitive ref var : assignments

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from the JLS section 4.12.2 http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.2


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.

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?


1) Wouldn't you think the fact that it compiled and ran be a pretty good indication that they are valid? Do you have some reason to think they aren't?

2) What was the compiler error? That tells you EXACTLY why it didn't compile. It prints out that stuff for a reason. You should learn to use it. If you need help interpreting it, then post it here so we can see what you are seeing.
 
Bartender
Posts: 2445
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.




Thank you all for your contribution. Thanks @ Himai for your time
 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



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
public class Test
{
public static void main(String[] a)
{
final short s1 = 1;
final char c1 = 1;
byte b1 = s1; //1
b1=c1; //2

short s=10;
char c=10;
byte b=s; //3
b=c; //4
}
}

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
 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



class MWC118 {
public static void main(String[] args) {
byte[] b = {'a', 'b', 'c'}; //1
byte b2 = 'a'; //2
String s1 = new String(b); // 3
String s6 = new String(b2); // 4
}}
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
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Java has special handling for compile time constants. Literals (in this example, char literals) are contants whose value can be determined at compile time. When the compiler assigns a compile time constant to a variable, and determines that the assignment will not lose any bits (called loss of precision), it will do the assignment. In other words, if the value being assigned will fit, it will be allowed.

Henry
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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




Same reason as previous post -- just need to have a deeper understanding of what is a compile time constant. See this topic for more information about compile time constants.

https://coderanch.com/t/454384/java/java/compile-time-constant

Henry
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic