aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Not Getting Compilation Error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Not Getting Compilation Error?" Watch "Not Getting Compilation Error?" New topic
Author

Not Getting Compilation Error?

Samir Dash
Greenhorn

Joined: Jun 08, 2005
Posts: 8
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.


Regards,<br />Samir (SCJP 1.4)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
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..


Thanks and Regards, Amit Taneja
Mohan Vinukonda
Ranch Hand

Joined: Jul 28, 2005
Posts: 32
Hi,
Since you can't convert int to long but you can do in case of byte to int

-Mohan
Samir Dash
Greenhorn

Joined: Jun 08, 2005
Posts: 8
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

Joined: Jul 28, 2005
Posts: 32
Hi Samir,
I am agree with you,
But I have written int to long instead of long to int

thanks for your catch
-Mohan
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
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.
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42
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


if you think you can you r right<br />if you think you can not you r double right
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
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 ]

Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi Arul,

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

Thanks,
Priya.
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42
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

Joined: Oct 13, 2003
Posts: 104

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?
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
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 ]

SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
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

Joined: Apr 28, 2004
Posts: 42
yes,

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

float : SINGLE PRECISION
double : DOUBLE PRECISION
Werner Capuno
Greenhorn

Joined: Jul 29, 2005
Posts: 14


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

Joined: Feb 18, 2005
Posts: 162
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

Joined: Feb 18, 2005
Posts: 162
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

Joined: Oct 13, 2003
Posts: 104
Concur with above! Thanks george for your pointer.
[ July 29, 2005: Message edited by: Arulkumar Gopalan ]
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42
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?
enediel Gonza
Greenhorn

Joined: Jun 11, 2005
Posts: 4
Testing the example that originated this thread

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

static byte retInt(){return i;}
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not Getting Compilation Error?