• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Plz explain me the answer of this code........  RSS feed

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q. what will happen when following code is executed ??

class MyClass
{
public static void main(String []args)
{
int i = 100;
byte b = i;
System.out.println(b);
}
}


ans.- compilation error.
(as 100 falls in the range of byte)
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well type checks happened on the variable type, and not on the values. So if you try to assign any int variable value to byte variable, it won�t compile.

You need to change your code as below to compile the code

byte b = (bype)i;.
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

check code above you will get your answer.
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot gowher.....i got it.
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks chetan..
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The i's value must be within the range of byte (-128 to 127), otherwise it is generating a compiler error.
[ August 16, 2006: Message edited by: wise owen ]
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unbelievable!!!

Compiles fine:



Doesn't compile: (!!!)




Doesn't compile: (!!!)



Why???
 
Ivan Rebrov
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any ideas?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting, I would expect a compile error: possible loss of precision for the first block of code.

The code compiles when we have:

final short i = 100;
byte b = i;

AND

final int i = 100;
byte b = i;

Could this be because the byte(8 bit), short(16 bit) and int (32 bit) all fall in the range of a 32-bit register and the long primative type has a 64 bit size and therefore loses 32 bits when assigned to a byte or an int?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Java Language Specification:

In addition, if the expression is a constant expression (�15.28) of type byte,
short, char or int
:
� A narrowing primitive conversion may be used if the type of the variable is
byte, short, or char, and the value of the constant expression is represent-
able in the type of the variable.
FT

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas on why long is treated differently here? (I find it helpful to know "why" when learning language rules, and this one seems a little arbitrary on first glance.)
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found This post, which gives a reasonable justification for treating long differently.
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per my view towards the implicit narrowing is that.... the reason for allowing the implicit narrowing for:

// code:
final int i=100;
byte b=i;

is that literals can never be byte,short or char type so whenever there is any need to asign a constant value to a variable of type byte,short or char ,java tells why should we bother about converting the int literal (in above case, 100) explicitly to byte before assigning it to variable b.... because in any case its not possible to assign 100 as a byte to variable b.
But in case of following codes:

//code:
final long l=100;
byte b=i;

// code:
final long l=100;
int i=l;

....java compiler tells that we should cast it explicitly to remove ambiguity or any confusion.
 
Paper jam tastes about as you would expect. Try some on this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!