The calculation is done as an integer calculation.

If you replace 2 by 2.0 you will get 50.5. I recommend adding a comment to that line to explain this to future readers. Alternatively you can explicitly cast to a double:

((double) (number1 + number2)) / 2
Answer from CompuChip on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Double.html
Double (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double. In addition, this class provides several methods for converting a double to a String and a String to a double, ...
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Double.html
Double (Java SE 17 & JDK 17)
January 20, 2026 - Compares two Double objects numerically. This method imposes a total order on Double objects with two differences compared to the incomplete order defined by the Java language numerical comparison operators (<, <=, ==, >=, >) on double values.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java double class overview
Java Double Class Overview
September 1, 2008 - The Java Double class wraps a value of primitive type double in an object.
🌐
Javatpoint
javatpoint.com › java-double
Java Double class - Javatpoint
Java Double.compareTo() Method The compareTo() method of Double class compares two double values numerically. Syntax public int compareTo(Double anotherDouble) Parameters anotherDouble - the Double value to be compared Return value This method returns the value- Zero, if anotherDouble has same ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Double.html
Double (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double. In addition, this class provides several methods for converting a double to a String and a String to a double, ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › double-parsedouble-method-in-java-with-examples
Double parseDouble() method in Java with examples - GeeksforGeeks
July 11, 2025 - // Java Code to implement // parseDouble() method of Double class class GFG { // Driver method public static void main(String[] args) { try { String str = ""; // returns the double value // represented by the string argument double val = ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-double-class-java
Java.Lang.Double Class in Java - GeeksforGeeks
July 23, 2025 - Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa.
Find elsewhere
🌐
University of Washington
courses.cs.washington.edu › courses › cse341 › 98au › java › jdk1.2beta4 › docs › api › java › lang › Double.html
Class java.lang.Double
true if the value represented by this object is positive infinity or negative infinity; false otherwise. ... Returns a String representation of this Double object. The primitive double value represented by this object is converted to a string exactly as if by the method toString of one argument.
🌐
GeeksforGeeks
geeksforgeeks.org › java › double-doublevalue-method-in-java-with-examples
Double doubleValue() method in Java with examples - GeeksforGeeks
July 11, 2025 - // Java code to demonstrate // Double doubleValue() method class GFG { public static void main(String[] args) { // Double value Double a = 17.47; // wrapping the Double value // in the wrapper class Double Double b = new Double(a); // doubleValue ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.double
Double Class (Java.Lang) | Microsoft Learn
[<Android.Runtime.Register("java/lang/Double", DoNotGenerateAcw=true)>] type Double = class inherit Number interface IConvertible interface IComparable interface IJavaObject interface IDisposable interface IJavaPeerable ... The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double. In addition, this class provides several methods for converting a double to a String and a String to a double, as well as other constants and methods useful when dealing with a double.
🌐
Duke
www2.cs.duke.edu › csed › ap › subset › doc › ap › java › lang › Double.html
Double (Advanced Placement Computer Science Java Subset Specification)
These methods, parseDouble and toString are shown in use in the following code. String s = Double.toString(3.14159); // s is "3.14159" double d = Double.parseDouble("123.45") // d is 123.45
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › › java.base › java › lang › Double.html
Double (Java SE 21 & JDK 21)
January 20, 2026 - Compares two Double objects numerically. This method imposes a total order on Double objects with two differences compared to the incomplete order defined by the Java language numerical comparison operators (<, <=, ==, >=, >) on double values.
🌐
Team Treehouse
teamtreehouse.com › community › how-do-i-add-a-double-method-please-answer
How do I add a double method??? Please answer!!!!! (Example) | Treehouse Community
March 12, 2015 - So, you'll need to update the visibility (See the question), the variable type (Which should be either int or double (See the question to know which one to use)) and prefix with m for a member variable. Also, you don't need to set a value for the variable so, just end it with a ;. Also, be sure to remove them brackets from your class declaration! We don't use brackets next to the class name - we use constructors for this instead but, you don't need to worry about this for the Code Challenge.
🌐
LabEx
labex.io › tutorials › java-how-to-use-double-methods-safely-419123
How to use Double methods safely | LabEx
In Java, the Double class is a wrapper class that represents 64-bit floating-point numbers. It provides a way to use double-precision decimal numbers as objects and offers various utility methods for numeric operations.
🌐
MIT
web.mit.edu › java_v1.0.2 › www › javadoc › java.lang.Double.html
Class java.lang.Double
Constructs a Double object initialized to the value specified by the String parameter. ... Returns the double value of this Double. ... Compares this object against the specified object. ... Returns the float value of this Double.
🌐
W3Schools
w3schools.com › java › ref_keyword_double.asp
Java double Keyword
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
🌐
DataCamp
datacamp.com › doc › java › double
double Keyword in Java: Usage & Examples
Use Double Class: For utility functions and methods involving doubles, consider using the Double class. Double doubleObject = Double.valueOf(a); // Using Double class · double positiveInfinity = Double.POSITIVE_INFINITY; double negativeInfinity = Double.NEGATIVE_INFINITY; double notANumber = Double.NaN; These constants can be used to handle special cases in floating-point arithmetic, such as results of undefined operations. Build your Java skills from the ground up and master programming concepts.
🌐
Studytonight
studytonight.com › java › double-class.php
Java Double Class | Studytonight
We can use it to get a double value from string type value. It takes a single String type argument. public static double parseDouble(String val) throws NumberFormatException · Lets take an example in which we have a string type variable and getting its double value using the parseDouble() method.