• 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

Not Getting Compilation Error?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Attaching the code below.



I am getting the compilation error at the last line. This is fine as I am returning long type from a method which should return int.

But why I am not getting compilation error from the line just above the last line. There I am returning int but the method return type is byte?

Any help is appreciated.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because static final int i is returning to byte

so no casting is required

this is rule...remember ..no casting is required if we put value which is static final even if value is greater then the type we put in ..
they are compile time constants..

so no casting is required for compile time constants
also remember no casting is required when we use
operator like += assingment operator..
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Since you can't convert int to long but you can do in case of byte to int

-Mohan
 
Samir Dash
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for responses.
As you told Amit


no casting is required if we put value which is static final


But if I am casting the long to int it compiles fine.
Just writing the last line alone.


Mohan I am not agree with you.
As you can assign all int values to long type. This will work as here we are widening the conversion.

Please correct me if I am wrong.
 
Mohan Vinukonda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Samir,
I am agree with you,
But I have written int to long instead of long to int

thanks for your catch
-Mohan
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

I just don't understand this scenario.can u plz check out the declaration of long variable 'l' ?.it is also static & final like int variable 'i'.Itz value(=10) also fits in the int range.Then why itz value can't be returned from the method retLong()?.Kindly reply back if u got any answers..

Thanks,
Priya.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a doubt in the rule you said:

no need to cast if we assign static final to a datatype smaller than the static final data type.

As per you said, if assigning static final int to byte is correct then assigning static final long to int must also work without any compilation error.

Please explain
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my understanding, long is a single precision number which can not fit into int, since int is a non precision number.

Here is the rule:
- compiler requires explicit type casting to make sure programmer is aware of loss of precision. Yes, incase of using final also.
- Non precision numbers can be returned with in their capability with out any casting, incase of using final only.
- Comment out the variable declarations and see the compile time errors to be clear on what is said here.

Does it answer for this thread?
[ July 28, 2005: Message edited by: Arulkumar Gopalan ]
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arul,

Thanks a lot!.now it is clear..!!

Thanks,
Priya.
 
Alangudi Balaji Navaneethan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arul,

So far I have known that precision can only be applied to float and double data types. You said long is a single precision. According to me, int as well as long are non precision numbers.

I referred complete reference by herbert Schlidt yesterday and came to know that the java compiler will convert byte, short and int to int data types before using them. That is why when assigning int to byte or short won't give any errors. But in case of returning long to int there may be a loss of precision (aah!!! why I am using the word precision).

Am I correct in my defend.
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So far I have known that precision can only be applied to float and double data types. You said long is a single precision. According to me, int as well as long are non precision numbers.


single precision number: long
double precision number: double
byte, short, int, float are not precision numbers.

I hope it is not changed in latest versions of Java, since i read java 5 years back. Let us know if there is any change in this.


the java compiler will convert byte, short and int to int data types before using them


Yes incase of performing any arithmetic operations.


That is why when assigning int to byte or short won't give any errors.


Yes


But in case of returning long to int there may be a loss of precision (aah!!! why I am using the word precision).


Yes, that is the reason am saying, we need explicit cast.



rule:
If the source type is greater than the destination type, explicit caste is required by programmer.

Does it help?
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arulkumar Gopalan:

single precision number: long
double precision number: double
byte, short, int, float are not precision numbers.



Hmm... and what about float?

From JLS3:


[ July 29, 2005: Message edited by: George Bolyuba ]
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS3:

4.2.3 Floating-Point Types, Formats, and Values

The floating-point types are float and double, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York).
 
Alangudi Balaji Navaneethan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,

I accept with george and also thanks to arul for his clear explanation.

float : SINGLE PRECISION
double : DOUBLE PRECISION
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The compilation error happen because it didn't met the first 2 out of the 3 condition for implicit narrowing primitive conversion. It is written in the book of Khalid Mughal, entitle "Programmers Guide to Java Certification 2nd ed.", which are as follows:

1. The source is a constant expression of either a byte, short, char, or int type;
2. The destination type is either byte, short, or char type; and
3. The value of the source is determined to be in the range of the destination type at compile time.

I hope that satisfy your question.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Werner Capuno, Welcome to Javaranch!

I suppose this book is cool, but may be you can show a part of JLS which speaks about our situation?
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I'll do it.

JLS3:

5.2 Assignment Conversion

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 representable in the type of the variable.
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Concur with above! Thanks george for your pointer.
[ July 29, 2005: Message edited by: Arulkumar Gopalan ]
 
Alangudi Balaji Navaneethan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just checked out JLS about conversions. OOOps.... there are plenty of such conversions. Is it really necessary to know each and every of such conversions?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Testing the example that originated this thread

static final int i = 128;
will cause complile error in the line

static byte retInt(){return i;}
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your example variables are effectively class constants and as long as it's within byte range the compiler will have no problem converting it. Seems a little inconsistent until you realize you've declared a manifest constant.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic