File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Ranch Office and the fly likes Round-up question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Ranch Office
Bookmark "Round-up question" Watch "Round-up question" New topic
Author

Round-up question

Larry LeFever
Greenhorn

Joined: Jun 13, 2000
Posts: 18
In one of the questions (I didn't note which), there is something like the following:
byte a = 2; a++;
The given correct answer is "compiles and runs". It points out that no explicit cast is required in "a++", though one would be required in "a + 1", because the byte "a" would then be cast to an int, so the arithmetic result would be an int that would then be intended to be stored in a byte, which is smaller than an int, and downcasting must be explicit in such cases, because of the possibility of loss of information. (I've elaborated this answer quite a bit.)
But isn't the literal "2" an int, by default, requiring an explicit cast to byte in order for it to be stored in byte "a" -- to force the programmer to acknowledge the potential loss of information?
The compiler just checks the relationship between types, right? Or does it also check the magnitude of a given literal relative to the magnitude-limit of the relevant type?


Sun-Certified Java Programmer
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20629
    ∞

This is one that originally stumped me too, so I ran it through the compiler and shore'nuff - it works! The compiler must see that it will fit into the byte and so it is okay with it.

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Yes, there's lots of cases where literals are treated fairly flexibly. You can assign a literal int to any of the shorter primitives (byte, char, short) without complaint, provided it is a valid value.
Sensible enough really. You wouldn't want to have to keep writing
byte b = (byte)5;


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi,
the answer lies here at jls
15.13.2 Postfix Increment Operator ++
"If necessary, the sum is narrowed by a narrowing primitive conversion to the type of the variable before it is stored. The value of the postfix increment expression is the value of the variable before the new value is stored."
same is the case for postfix decrement operator -- and Prefix Increment Operator ++ and the Prefix Decrement Operator --
thus if
byte b=127;
b++;//works and does not raise a compile time error but it prints -128
however this raizes and error saying cast required to convert to byte
byte b=120;
b=b+5;//compiler screams even though the sum fits into a byte. thus explicit cast needed here.
Hope this helps.
regds.
rahul.


[This message has been edited by rahul_mkar (edited June 14, 2000).]
Larry LeFever
Greenhorn

Joined: Jun 13, 2000
Posts: 18
I'm a bit confused now. Eventually, I'll find the time to work with the compiler to see how it actually works. In the meanwhile, though, I'd appreciate some further clarification.
Is the appending of "f or F" or "l or L" to a numerical literal ever necessary?
I realize the default is: "if no decimal, then int; else double". But, if the other rule is "no complaint, if the value fits, even if the literal's type is larger than that of the lvalue, when should one, or need one, use the "F" or "L"?
(I'm leaving out of the picture, for the time being, arithmetic expressions, which, I realize, may involve implicit casts, which, in turn, seem to trigger the need for explicit ones.)
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20629
    ∞

It seems there are many times when Java requires that L or F.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi larry ,
i guess one does have to work things out on the system in the end. it has taken me a lot of time to learn these things. coming to u r doubt of f or F. the demo class raises an error "explicit cast needed to convert double to float". the basic thing is that any floating point number is a double by default. thus
byte b=20; // valid 20 integer is narrowed back to byte
float f=20.3;// invalid double has to be cast to float

public class demo
{
public static void main(String args[]){
float b=120.08;
float c=120.08f; // valid
float d=120.08F;// valid
long x=120;//valid
long xa=120l;//valid
long xb=120L;// valid
System.out.println(b);
}
}
The l or L, d or D, f or F, are needed many a times. mostly to specify that the literal is of the specified type. f or F is used mostly to convert doubles to floats through assignment.
if u r confused with any of my statements the forum is open for discussion.
Regds,
Rahul.

[This message has been edited by rahul_mkar (edited June 14, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Round-up question