• 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

A question

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why byte b=j does not require typecast while byte b2=(byte)k
requires typecast though both are constants.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe the problem lies in the fact that tho' j gets its value in compile time(since it's final) and the compiler can see what j holds, i does not and gets its assignment only when an instance of the class conversion is created.
so when the compiler sees that b = j; it knows that j=20 and within the range of byte. but when it sees
b1 = k and final k = i, it still doesn't know what i is and safeguards itself against possible problems.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friend,
the reason what I can forsee is this
"i has been assigned to k after i has been declared earlier". Am I right.
Whereas j has been assigned directly as 20"
When it compiles, j is certain that 20 will be assigned to it and finding itself that it is in its range, it works. Wheares in the case of k=i, the compiler is not certain that i would remain constant as it is susceptible to change.
This is my first post, I hope it serves the purpose
regards
venkat
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandy,
I think your problem comes about because you don't understand what the compiler sees:
int i -> -20 (default value specified,but changeable)
int k -> i (k can never reference anything else but i)
int j -> 20 (j can never be changed from 20)
byte b -> j (from above we know this same as b -> 20 literal integer)
byte b1 -> i (byte pointing to changeable integer)
byte b2 -> k (byte pointing to integer reference pointing to changeable integer)
From the above, we can see that just because k points to i doesn't mean that the value pointed to by i (integer) can always fit into a byte. You have given a default value of -20 for i but that can be changed by any constructor. Since the compiler is dealing with changeable values it just uses Java default rules: Narrowing of primitives is automatic but Widening must use cast!
Enjoy,
Manfred.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Manfred:
In this case, the real value of k can be changed:
Assuming the following:
int i = -20;
final int k = i;
i +=20;
System.out.println(k);
It should return 0???
BTW, I did not run this code.

Originally posted by Manfred Leonhardt:
Sandy,
I think your problem comes about because you don't understand what the compiler sees:
int i -> -20 (default value specified,but changeable)
int k -> i (k can never reference anything else but i)
int j -> 20 (j can never be changed from 20)
byte b -> j (from above we know this same as b -> 20 literal integer)
byte b1 -> i (byte pointing to changeable integer)
byte b2 -> k (byte pointing to integer reference pointing to changeable integer)
From the above, we can see that just because k points to i doesn't mean that the value pointed to by i (integer) can always fit into a byte. You have given a default value of -20 for i but that can be changed by any constructor. Since the compiler is dealing with changeable values it just uses Java default rules: Narrowing of primitives is automatic but Widening must use cast!
Enjoy,
Manfred.


 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Weigang,
Manfred is correct. Your example returns -20.

-Peter
[This message has been edited by Peter Tran (edited January 22, 2001).]
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandy,
You claim that k and j are both constant. Well, what matters in our case is whether they are *compile-time* constants. j is one, because it is final and assigned a compile-time constant (20). k is *not* a compile-time constant; it may be final, but the value getting assigned to it (i) is *not* a compile-time constant.
Hope this helps,
Panagiotis.

<pre>
=================
public class conversion{
int i = -20;
final int k=i;
final int j=20;
byte b=j;
byte b1=(byte)i;
byte b2=(byte)k;
}
======================
</pre>
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did a lot of trial coding and yea, i was right!9a smug me!) but i hadn't realized the full implications...
this is what happens... k gets initialized to a final value only when an instance of the class is created, via i. but once assigned this value, k sticks to it.
if i declare k as static and call it from the main method, it still gives an error of calling non-staitc i from a static method!
hence the value of k is made known to the compiler only at the time of the first instantiation of the class and hence the error.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic