• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assignments

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question from Javabeat

public class Test8{
public static void main(String a[]){
byte b = 1;
char c = 2;
short s = 3;
int i = 4;

c = b; // 1
s = b; // 2
i = b; //3
s = c * b; //4
}
}
I always wrong in answering the questions like above, i thought c=b is correct but it is giving the compiler error..can any one explain me in detail about the narrowing the widening.
Thank you.
 
NDP Prasad
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i change the b to
final byte b = 1; It did't shows any compiler error. why is so?
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NDP,
Also when you write
final byte b = 1
it becomes a compile time constant and compiler is assured that its value will never change.All numeric primitive types which size is big enough to store value of b,can be used to assign to.
[ January 03, 2007: Message edited by: Sanjeev Kumar Singh ]
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand the question. The code you have written needs several <b> casts </b> in order to compile.

If this helps: "Remember aritmetic operations always return an <b> int </b> as a result"

byte a = 3;
byte b = 4;

byte c = (byte)(a + b);//Cast is necesary because the result is an <b> int </b>

If you add more information I can try to answer.

Maybe other ranchers understand the question better than me ...
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Sanjeev Kumar Singh said is true, but you still need a cast in your //4.
 
NDP Prasad
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to know why

s=b is right while c=b is not correct and also the expression s = c * b;
 
NDP Prasad
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why dint i need any cast for getting this //here
char c = 2;
short s = 3;
double d=3.9;

d=s*c; //here

I didt understand this concept, pls explain me.
Sanjeev Iam not getting your .gif.
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to undertand which data type is "bigger" or could fit an "smaller" data type:

They come in this order:

byte
char
short
int
long
float
double



if you try to assign a short to an int it can handle:

short s = 10;
int i = s;

but

int i = 10;
short s = i; //Short can't handle ints. Doesn't compile.


But what if you declare "int i" as final???

final int i = 10;
short s = i; //It works!!!


Why it works???

because if you define "final int i" prior to execution (compiling). The compiler will check this before an error happen since i will never change again.


if you not declare it as "final" the compiler will tell you "Hey im just checking your code but I'm not executing it. I don't know if in the future you will assing a 100000 to the int, so i will tell you " possible lose of ..."
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



hi,
dear Prasad,

it's true, when you will make b final then it will compile because c, s and i at line//1, //2, and //3 will be assigned with a byte value and there is no need of explicit casting.

if you did not declare b as final then the right side of assignment at line //1 , //2, //3 is a integer value(when you are trying to reassign c,s and i using b)which require explicit casting because int is default numeric literal value.


---hope this help.
 
Anuragk kushwaha
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


one more thing as Sanjeev told, final makes var compile time constant.

and yes you can do it without using final. you have to explicit cast at //1 and //4...as


always keep in mind one thing.... conversion between char and byte, short need explicit casting

---please correct me if i am wrong
 
NDP Prasad
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

always keep in mind one thing.... conversion between char and byte, short need explicit casting



1.I dont think above quote is correct because
s=b; //is ok. no casting

2.char is 16 bits, byte is only 8 bit. but it was't accomadate byte.
c = b; //we got compilation files

why?
 
Anuragk kushwaha
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


dear NDP, read as...

"all conversions between char and the two integer types byte and short are considered narrowing conversions: the reason being that the conversions between the unsigned type char and the signed types byte or short can result in loss of information."

means.... one side of assignment operator is char and other is either byte or short..



2. the reason being that the conversions between the unsigned type char and the signed types byte or short can result in loss of information."


hope this help...
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by NDP Prasad:


1.I dont think above quote is correct because
s=b; //is ok. no casting

2.char is 16 bits, byte is only 8 bit. but it was't accomadate byte.
c = b; //we got compilation files

why?




When you say s=b then you are assigning a byte (8 bits) to a short (16 bits). And remember both short and byte are "SIGNED" types.
However character although is of 16 bits but remember that it is UNSIGNED. that is character is always positive ( >0 ). Due to this the compiler will not allow assigning to a character.

Please correct me if i am wrong.
 
NDP Prasad
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thks all I got it.

byte b=-4;
char c='f';
c=(char)b;
System.out.println(c);

Ans is ?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though the topic seems closed but just to clarify...

every byte value is assignable to a short as both are signed int types.

every byte value is not assignable to a char type.

final byte b = 1;
char c = b; //OK

final byte b = -1;
char c = b; // compiler complains!!!
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whenever there is expression evaluation using int or short or byte the result is always a int so
byte b=3;
byte c= 9;
byte a= b+c;
will not complile because afte adding b+c thje result is integer 12 and we are assigning it to byte so it requires explicit casting
so byte a=(byte)(b+c)
 
Anuragk kushwaha
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, Sweety is talking about Numeric Promotions when evaluating expressions....Numeric operators only allow operands of certain types.it is implicitly applied on the operands to convert them to permissible types.

unary numeric promotion as.....


If the single operand of the operator has a type narrower than int, it is converted to int by an implicit widening primitive conversion; otherwise, it is not converted.



binary numeric promotions.....



Binary numeric promotion implicitly applies appropriate widening primitive conversions so that a pair of operands have the broadest numeric type of the two, which is always at least int.


 
This is my favorite show. And this is my favorite tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic