Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java mistakes with numbers

Tags:

java

I am trying to fix the following Java code,

I cannot figure out why the printout is still 5.

public class simpleMath
{
    public static void main(String[] args) 
    {
        int number=5;
        half(number);

        System.out.println("5 divided by 2 is: " + number);

    }

    private static double half(int number) {
         number =  number/2;
         return number;
    }
}
like image 775
Yui2 Avatar asked May 31 '12 10:05

Yui2


People also ask

Do you make common mistakes in Java programming?

In this article, we’re going to present some of the most common mistakes which every Java programmer eventually makes, the reasons why they happen and solutions to avoid them. Note that making mistakes are not bad. It is good for helping programmers realize and understand the problems better as well as improve their programming skills.

How do you find logical errors in Java?

The Java system has no idea what your program is supposed to do, so it provides no additional information to help you find the error. Logical errors are also called Semantic Errors. These errors are caused due to an incorrect idea or concept used by a programmer while coding.

What is a syntax error in Java?

Usually, the compiler indicates the exact line where the error is, or sometimes the line just before it, however, if the problem is with incorrectly nested braces, the actual error may be at the beginning of the block. In effect, syntax errors represent grammatical errors in the use of the programming language. Compilation Error in java code:

How do you handle run time errors in Java?

To handle the error during the run time we can put our error code inside the try block and catch the error inside the catch block. For example: if the user inputs a data of string format when the computer is expecting an integer, there will be a runtime error. RunTime Error in java code:


4 Answers

Because you're not re-assigning the returned value.

int number = 5;
number = half(number);
like image 50
adarshr Avatar answered Oct 13 '22 13:10

adarshr


When you call the function, you're discarding its return value:

half(number);

You probably meant to write:

number = half(number);

Also, in Java, arguments are passed by value. This means that, even though you change number inside the function, the change does not propagate back to the caller.

There are several further problems:

Problem 1: The suggested change will store the result in number, which is an integer variable. Thus, the result of half() -- which is of type double -- will be truncated to an integer. To avoid the loss of precision, you either have to change number to be a floating-point variable, or store the result in a different variable of the appropriate type.

Problem 2: The following uses integer division:

number =  number/2;

The result is truncated to an integer, i.e. 5 / 2 is 2. The latter is then converted to a double (2.0), which is what the function returns.

To fix, change the function like so:

private static double half(int number) {
     return number / 2.0;
}

P.S. Floating-point numbers have a lot of properties that can be unintuitive. I recommend having a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic.

like image 21
NPE Avatar answered Oct 13 '22 12:10

NPE


You passing the primitive data type which is done by value. You need to give SOP in method half() EDIT: Need to use the result returned by method half() by either assigning it to number or calling this method in SOP itself.

like image 28
ejb_guy Avatar answered Oct 13 '22 14:10

ejb_guy


Why half(number) doesn't modify number declared in main() function? It is because you will pass the value of number to half() function to evaluate, i.e. you give a copy of value in number to half() function. Therefore, whatever half() function does to number will not get reflected back to number variable declared in main(). You need to assign the return value of half() to number in main() if you want to update its value.

There are other cases, such as variable shadowing, that I'm not going to talk in details, since it may confuses you.

like image 20
nhahtdh Avatar answered Oct 13 '22 12:10

nhahtdh