• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

implicit and explicit casting

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys...

i have one question and confusion that

if while declaring

byte b=2; // here 2 literal is implicitly int and converted
// automatically to byte as written in K&B

but if
we declare
int i=3;
byte b2=i;

here the automatic casting will not applied..

is am rt ?
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first case:
byte b = 2;
the compiler is smart enough to know that the literal 'fits' within a byte. That is only because it is a literal.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
FYI, one more variation to the code , the following code will NOT cause compiler error. Note that the int variable declared is final. Hence the value assigned to byte b is compile time constant.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amit ...

Let's consider all the case again step-by-step :
==============================================================

==============================================================

In

Line 1:value '2' is by default an int but because it's value can fit on a byte an explicit cast took place .

Line 2:value '2' is by default an int so no problem.

Line 3:the mean reason (I think so)that there were no explicit cast is because 'i' is a variable and excluding that you assigned it to value '2' in line 2 but it may take any value and the big problem happens if its value is bigger than a byte one so by restricting on such case only CONSTSNTS are allowed to be automatically casted because there value won't change by anymean .

Hope I could help you understand such point somehow .
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jay Pawar:
Amit,
FYI, one more variation to the code , the following code will NOT cause compiler error. Note that the int variable declared is final. Hence the value assigned to byte b is compile time constant.




final static variables are constants
so they are assinged value when class is compiled and before runtime...?

are you sure that it will work fine even if we remove static ...and allow only final modifier ???

will that enought to make it constant..?

pls correct me if i m wrong...

amit
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit, you will learn better if you test these things yourself.

Except in the movies, no program you write will cause your computer to explode.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Note that the int variable declared is final. Hence the value assigned to byte b is compile time constant.




final static variables are constants



These two statements are common fallacy.
A constant is always final, but a final is not necessarily constant.
JLS 15.28 is your point of reference.

As for the original question, the answer lies in the fact that you are using a constant (not necessarily a final). Consider the following:



What now?
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not necessary to add static at the time of defining constants . But generally we add static because all the instance variable having same value for that variable so we make it commaon ( static , class level ) .

Am I right ?
[ February 08, 2005: Message edited by: rathi ji ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something along those lines, yes.
To declare a non-static constant field is considered poor form (though I know of only one code analyser that will pick you up on it).
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:


What now?





sir,,,

can we declare a block static ?

like above
static
{
int a=2;
byte b=4;
}

is that legal ? what does that mean...is that mean that all the variables in static block are static ? or what ? pls tell me and correct me >>>


on more thing is that you wrote that

every constant is final but not every final is constant...

is it true if only talk only about final variables... ?
i didn't get this point ...

:roll:
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:


What now?





sir,,,

can we declare a block static ?

like above
static
{
int a=2;
byte b=4;
}

is that legal ? what does that mean...is that mean that all the variables in static block are static ? or what ? pls tell me and correct me >>>


on more thing is that you wrote that

every constant is final but not every final is constant...

is it true if only talk only about final variables... ?
i didn't get this point ...

:roll:
 
I knew that guy would be trouble! Thanks tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic