• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

int i = 1/2

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = 1/2;

Why does this compile and produce 0 as an answer which it does. I expected 1/2 to return 0.5 which is a double and therefore cannot be assigned to an int and therefor would not compile due to loss of precision etc.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think int i = 1/2 produces a compile time constant so what happens is that the compiler or whatever makes the implicit cast to it. I'm not sure but i read something about it in here
[ August 23, 2004: Message edited by: Doyle Matt ]
 
Peter Warde
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt
I'm not sure about this compile time constant. I don't think the code below is a compile time constant as the value of y would only be determined at runtime.

class Test {

int i;

Test(int y) {
i = y/2;
System.out.println(i);
}

public static void main( String args[]) {

Test t = new Test(1);
}
}

Ps does anybody know a good explanation of what is determined at compile time versus runtime
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
both operands y and 2 are integers and if they were (byte/short/int)..they wud be implicitly converted to int as per the operands work. so the int answer is 0. even if u had written
double d=y/2; it would give 0.0 and not 0.5.
Because it performs the division first and then assigns the answer to d.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Warde:
Matt
I'm not sure about this compile time constant. I don't think the code below is a compile time constant as the value of y would only be determined at runtime.

class Test {

int i;

Test(int y) {
i = y/2;
System.out.println(i);
}

public static void main( String args[]) {

Test t = new Test(1);
}
}

Ps does anybody know a good explanation of what is determined at compile time versus runtime




the way it works, with any arithmetic operations, and for that matter almost any statement that you can point out in a line of source, is there is an instruction for the specific operation that gets determined at compile time, based on the number of operands and their types, and then values are given to that instruction at runtime, which determines the result. so based on the types and numbers of operands you have in your source file, the compiler will determine a specific instruction that should be executed for that. in this case, it's an integer division, which truncates.
 
Doyle Matt
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

All this techy terms are getting me confused. Anyway just to simplify things the code int i = 1/2 generates a compile time constant plus both "1" and "2" are integers. so at runtime/or compile time (which ever is correct) will produce and output of 0 (an integer) and not a double 0.5.
Clear enough? Or did I add additional mistakes and confusion?
 
Chris Wash
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Doyle Matt:
Hi All,

All this techy terms are getting me confused. Anyway just to simplify things the code int i = 1/2 generates a compile time constant plus both "1" and "2" are integers. so at runtime/or compile time (which ever is correct) will produce and output of 0 (an integer) and not a double 0.5.
Clear enough? Or did I add additional mistakes and confusion?



that is the way it works. i thought you were looking for an explanation of why it worked like that, so i was just saying the compiler has an instruction set that it works off of. it sees 2 integer operands and decides to use the integer division instruction, which returns an integer. even if you were try to cast this value as a floating point type and store it in a float, it would still be zero. the instruction it uses it does division like you used to do back in 3rd grade with remainders, only the remainders get thrown away or truncated. if you wanted to get a 0.5 you would need to use floating point operands.

chris
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic