This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games and have Dana Pylayeva on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Question from danchisholm

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please have a look at the below code :



It gives a compiler error on line 1 ,At line 1, the invocation of the Math.min method produces a return value of type int. The variable a1 is of type byte; so the assignment expression results in a compile-time error.

But as the result from the invocation of min method is of type int, which is in the range of the byte,it can be assigned to variable byte a1 right?? why compiler error.

Added to this if byte a= 40; doesn't give any compiler error where as byte b=128; does ,because the value is out of range for byte... Then why does the code block provided above raises the error?
Please clear my doubt..
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because compiler checks the types to the both side of the assignments and find loss of precision
Even if u try to compile the following code
public class Test{
public static void main(String [] args){
byte a,b,c;
int i=1;
a=i; //1
b=40;
c=20.0; //2
}
}

U will get compile time error due to 1 and 2
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it might be more accurate to say that Min is a function which the compiler does not evaluate- if it had been 1+2 it would have compiled fine. It attempts to turn things into literals, and if it were smarter here it would have been able to do so.

But the fact is it just does not call functions in the attempt to resolve literals- it saves this for run time. By run time, it isn't sure of the answer anymore and then the compiler says "might be a problem" even though it turns out it isn't.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(From the documentation http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html )

The line


(where x1 and x2 are byte variables) invokes the method above that is marked with a **. The return type of this method is an int.

This is what the compiler looks at. That it returns an int.

So, to the compiler you are telling it to assign an int to a byte. Assigning an int to a byte requires a cast. The compiler doesn't calculate what the value is of that int.

The only exception is the following:



Here, the compiler can see that c = -128 is being assigned a literal. It notes that the literal is within a valid range of a byte so it allows this.

Also in the case where it assigns b = i where i is a final int, this gets resolved at compile time, and the compiler knows what the value is at compile time, so it allows this.

Hmmm... I'm still working towards that 90. Any hope of that happening? -Louie
[ September 04, 2004: Message edited by: Louie van Bommel ]
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic