Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6230621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:46:55+00:00 2026-05-24T09:46:55+00:00

I’m a beginner and there’s something that’s not making much sense to me. Please

  • 0

I’m a beginner and there’s something that’s not making much sense to me. Please could be so kind as to explain where I’m going wrong. I’m sorry if this has been asked before.

Here the presence of the decimal point mean these get evaluated using floating point division.

System.out.println(1/3.0); // this prints: 0.3333333333333333
System.out.println(1.0/3); // this prints: 0.3333333333333333
System.out.println(1.0/3.0); // this prints: 0.3333333333333333

Apparently, this below is an example of “truncating-integer division.” It seems a bit weird to me but ok.

System.out.println(1/3); // this prints: 0

Is it ok to say: “in the line below, the (double) cast is evaluated 1st. It effectively says: “treat 1/3 as a double – don’t use truncating integer division. instead use floating point division.””

System.out.println((double)1/3); // this prints: 0.3333333333333333

Below, however we get 0.0 – how did that happen?

System.out.println((double)(1/3)); // this prints: 0.0

ok so maybe the extra parentheses mean the (1/3) gets evaluated 1st. It is evaluated using truncating integer division yielding 0. And then the double is applied giving us 0.0 Ok that makes sense

Ok so we maybe can propose a couple of general rules here:

Rule 1: (double) expression means apply the (double) cast first then evaluate the expression.

Rule 2: (double) (expression) means evaluate the expression then apply the cast.
Great!

So in the next line below we have: (int) expression, so I guess we can apply rule 1). The (int) cast is evaluated 1st. It effectively says: “treat 1.0/3 as a int – don’t use as much memory as you would with a double. Don’t use floating point division, instead apply truncating integer division.”” So we have 0 right? No.

System.out.println((int)1/3.0); // this prints: 0.3333333333333333

Ok so we have 0.33333 so the (int) cast is not evaluated 1st. It is as if it wasn’t there. Let’s propose a 3rd rule:

Rule 3: (int) expression means ignore the (int) cast altogether just evaluate the expression like the (int) isn’t even there.

Ok applying rule 3 to the line below, we have (int) but we are just going to ignore it. 1.0/3.0 is evaluated with floating point division and we get 0.3333333. Success!

System.out.println((int)1.0/3.0); // this prints: 0.3333333333333333

And in the last line below, again we have (int) but we are just going to ignore it. (1.0/3) is evaluated using floating point division yielding 0.3333333333 right? No.

System.out.println((int)(1.0/3)); // this prints: 0

Ok now I’m confused. Please could you help me get my head round this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:46:56+00:00Added an answer on May 24, 2026 at 9:46 am

    Java’s language parsing rules are basically implying a lot of parentheses everywhere in your code based on its rules of order of operations. Understanding where Java thinks the parentheses are will help you understand this behavior. When you say:

    (double) 1 / 3.0
    

    … this is equivalent to saying:

    ((double) 1) / 3.0
    

    This means that 1 gets converted to a double, and 3.0 is automatically a double, so you’ll end up doing a floating point division rather than integer division. The same thing would happen with (double) 1 / 3, because the divisor is a double, so even though the dividend is an int, the system realizes that you want to do floating-point division. The compiler doesn’t want you to lose precision unless you specifically ask to, so any time either the dividend or the divisor is a double, it will do double division.

    On the other hand:

    (int) 1 / 3.0
    

    … is the same as saying:

    ((int) 1) / 3.0
    

    In this case, you’re telling the compiler what it already knew: that the divisor (1) is an int. Then you’re asking it to divide by a double value (3.0). Since the dividend is a double, it will perform double division.

    As you noted, 1/3 will produce zero, because that’s how integer division works and both numbers are integers. The same will happen with 1/(int)3.0 because you’re telling the compiler to make the 3.0 into an int before the division will occur (as in 1/((int)3.0)).

    The final point to keep in mind is that (int) 0.33 will also get converted to 0, because an integer can’t hold decimal values in it. So when you say (int)(1.0/3), you’re doing double division, but then you’re converting the double into an int afterwards, producing 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.