TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Double Numbers: Double.BYTES and Double.SIZE

Use the double numeric type, accessing Double.BYTES and Double.SIZE.
Double. A double is twice the size of a float. It is 8 bytes (which is equal to 64 bits). Doubles are used to store floating point values.
In Java, we use the Double class to access static methods and constants on a double. We can find useful constants like BYTES but also methods like isNaN to test for special double values.
First example, constants. Here we show some constants on the Double class. We find that the MIN and MAX double values are in a large range.
Java program that shows double constants public class Program { public static void main(String[] args) { // This is the number of bytes. System.out.println(Double.BYTES); // Size is the number of bits. System.out.println(Double.SIZE); // Print min and max. System.out.println("MIN: " + Double.MIN_VALUE); System.out.println("MAX: " + Double.MAX_VALUE); } } Output 8 64 MIN: 4.9E-324 MAX: 1.7976931348623157E308
Locals, casts. Next we try casting some doubles. We can convert ints and floats (and other smaller numeric types) to doubles without a cast. This is a widening cast.FloatCast

Suffix: The lowercase "d" indicates double literal. So if we want a literal of type double, use the "d" as a suffix.

Java program that uses double locals public class Program { public static void main(String[] args) { // Use double local variables. double value1 = 1.0d; double value2 = 1; double value3 = (double) 1; // A double can be assigned to a float value. float test = 1.0f; double value4 = test; System.out.println(value1); System.out.println(value2); System.out.println(value3); System.out.println(value4); } } Output 1.0 1.0 1.0 1.0
Double tests. The Double class has several static methods. These include isNaN, isFinite and isInfinite which tell us things about the double argument we provide.

Tip: Special values on a double like NaN are stored within the double's bytes as a special code.

Java program that tests doubles public class Program { static void test(double v) { // Use methods to test the double. if (Double.isNaN(v)) { System.out.println("Not a number"); } if (Double.isFinite(v)) { System.out.println("Finite"); } if (Double.isInfinite(v)) { System.out.println("Infinite"); } } public static void main(String[] args) { // An array of doubles. double[] values = { Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 100 }; // Test all doubles in array. for (double v : values) { System.out.println(":: TEST ::"); System.out.println(v); test(v); } } } Output :: TEST :: NaN Not a number :: TEST :: -Infinity Infinite :: TEST :: Infinity Infinite :: TEST :: 100.0 Finite
Truncate. To truncate a double, we can cast it to an int. But this will not always work. We can use a method that combines Math.floor and Math.ceil for correct results.Truncate Number
A review. In my experience double is best reserved for special situations. If floating point is necessary, a double is a simpler choice than float, as floats can be easily cast to doubles.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf