• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

can someone expalin the output ???

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I
final int i = 100;
byte b = i;
System.out.println(b);

II
int i = 100;
byte b = i;
System.out.println(b);
the first one compiles and runs fine WHY ???
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whenever you use the final modifier infront of a variable you must initialize it. What this basically does is, create a constant value for the variable that can never be modified.\
In your example, you are assigning variable i a CONSTANT value of 100. What this effectively does is that it makes the variable independant of a particular type.
so when we say:
byte b=i
it is the same as saying
byte b = 100; if you try this it will also compile fine, AS LONG AS IT IS IN THE RANGE OF A BYTE, and 100 is in range.

In the second case since final is not used, the variable i is identified with a particular type, an int, and assignment of an int type cannot be made to a byte (even if the value may be in range) without explicit casting.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sarim,
Why this is NOT compiling
final long ll = 10;
int ii = ll;
Thank
Saradindu Sarkar
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per JLS,
narrowing primitive conversion may be used if all of the following conditions are satisfied:
(1) The expression is a constant expression of type byte, short, char or int.
(2) The type of the variable is byte, short, or char.
(3) The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.
Also, If the type of the expression cannot be converted to the type of the variable by a conversion permitted in an assignment context, then a compile-time error occurs.
This is what dictates the results of the code
HTH
Sandeep Nachane

------------------
www.tipsmart.com:
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic