wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes primitive & primitive ref var : assignments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "primitive & primitive ref var : assignments" Watch "primitive & primitive ref var : assignments" New topic
Author

primitive & primitive ref var : assignments

Ilakya Mukunth
Ranch Hand

Joined: Mar 13, 2012
Posts: 57
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?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 722
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

Joined: Mar 13, 2012
Posts: 57
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

Joined: Mar 13, 2012
Posts: 57
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

Joined: Mar 13, 2012
Posts: 57
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

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.

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

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: primitive & primitive ref var : assignments
 
Similar Threads
Question on conversion and autoboxing
wrappers
Question about primitive assignments
primitive assignment problem
primitive widening doubt