Skip to content

Commit 42e379e

Browse files
author
JavaProgramTo.com
committed
java float vs double
1 parent 6227ab1 commit 42e379e

File tree

9 files changed

+179
-15
lines changed

9 files changed

+179
-15
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.javaprogramto.datatypes.diff;
2+
3+
public class FloatVsDoubleDataLoss {
4+
5+
public static void main(String[] args) {
6+
7+
// double examples
8+
double d = 1.2345678912345678;
9+
10+
float f = (float) d;
11+
12+
System.out.println("float value after double to float conversion (data loss) - " + f);
13+
14+
// float examples
15+
float f1 = 1.1111111f;
16+
17+
double d1 = f1;
18+
19+
System.out.println("\n" + "double value after float to double conversion - " + d1);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.javaprogramto.datatypes.diff;
2+
3+
public class FloatVsDoubleDefault {
4+
5+
static float floatDefaultvalue;
6+
static double doubleDefaultvalue;
7+
8+
public static void main(String[] args) {
9+
10+
// default types
11+
double d = 12345.6789; // no error. so it is considered as double type.
12+
13+
//float f = 12345.6789; // error. Type mismatch: cannot convert from double to float. float type is not
14+
// detected
15+
16+
// default values
17+
18+
System.out.println("Float type default value - " + floatDefaultvalue);
19+
System.out.println("Double type default value - " + doubleDefaultvalue);
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.javaprogramto.datatypes.diff;
2+
3+
public class FloatVsDoubleMemory {
4+
5+
public static void main(String[] args) {
6+
7+
System.out.println("Double size");
8+
int doubleSizeBits = Double.SIZE;
9+
int doubleSizeInBytes = doubleSizeBits / 8;
10+
11+
System.out.println("Double size in bytes - " + doubleSizeInBytes);
12+
System.out.println("Double size in bits - " + doubleSizeBits);
13+
14+
System.out.println("\n" + "Float size");
15+
int floatSizeBits = Float.SIZE;
16+
int floatSizeInBytes = doubleSizeBits / 8;
17+
18+
System.out.println("Float size in bytes - " + floatSizeInBytes);
19+
System.out.println("Float size in bits - " + floatSizeBits);
20+
21+
}
22+
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.javaprogramto.datatypes.diff;
2+
3+
public class FloatVsDoublePrecision {
4+
5+
public static void main(String[] args) {
6+
7+
// double examples
8+
double d = 1.2345678912345678;
9+
10+
System.out.println("Double type precision value 1 - " + d);
11+
12+
double d2 = 1.1020304050d;
13+
System.out.println("Double type precision value 2 with suffix d - " + d2);
14+
15+
// float examples
16+
17+
float f = 1.23456789123456789f;
18+
19+
System.out.println("\n" + "Float type precision value 1 - " + f);
20+
21+
float f2 = (float) 2.10203040;
22+
System.out.println("Float type precision value 2 with suffix d - " + f2);
23+
24+
}
25+
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.javaprogramto.datatypes.diff;
2+
3+
public class FloatVsDoubleSuffix {
4+
5+
public static void main(String[] args) {
6+
7+
// double examples
8+
double d = 10.9876;
9+
10+
System.out.println("Double value no explicit usage of suffix - " + d);
11+
12+
double d2 = 1.23456789d;
13+
System.out.println("Double value optionnal suffix d - " + d2);
14+
15+
// float examples
16+
17+
// Compile time error
18+
// float f3 = 123.456; // Type mismatch: cannot convert from double to float
19+
20+
float f = 10.9876f;
21+
22+
System.out.println("\n" + "Float value with explicit usage of suffix f - " + f);
23+
24+
float f2 = (float) 1.23456789;
25+
System.out.println("float value with type casting - " + f2);
26+
27+
}
28+
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.javaprogramto.datatypes.diff;
2+
3+
public class FloatVsDoubleWrapper {
4+
5+
public static void main(String[] args) {
6+
7+
// Double wrapper
8+
Double d1 = new Double(123.456);
9+
System.out.println("Double d1 value - " + d1);
10+
11+
Double d2 = new Double("7.890");
12+
System.out.println("Double d2 value - " + d2);
13+
14+
Double d3 = Double.valueOf(12.456);
15+
System.out.println("Double d3 value - " + d3);
16+
17+
// Float wrapper
18+
Float f1 = new Float(123.456);
19+
System.out.println("\n" + "Float f1 value - " + f1);
20+
21+
Float f2 = new Float("7.890");
22+
System.out.println("Float f2 value - " + f2);
23+
24+
Float f3 = Float.valueOf(12.456f);
25+
System.out.println("Float f3 value - " + f3);
26+
27+
}
28+
}

src/main/java/com/javaprogramto/keywords/extend/ClasesExtendsExamples.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ public int getNoOfWheels() {
2222
return this.noOfWheels;
2323
}
2424
}
25-
25+
/*
2626
class HybridCar extends Car , HondaCar{
2727
28+
}
29+
*/
30+
31+
class SkodaCar extends Car {
32+
2833
}
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
package com.javaprogramto.keywords.extend;
22

3-
public class ClasesExtendsExamples {
3+
import java.util.Calendar;
44

5-
public static void main(String[] args) {
5+
public class FinalClasesExtendsExamples {
66

7-
HondaCar hondaCar = new HondaCar();
8-
int noOfWheels = hondaCar.getNoOfWheels();
9-
System.out.println("Honda car wheels : " + noOfWheels);
7+
public static void main(String[] args) {
108

119
}
1210
}
1311

14-
class Car {
12+
final class Calculator {
1513

16-
int noOfWheels = 4;
17-
}
14+
public int sum(int a, int b) {
15+
return a + b;
16+
}
1817

19-
class HondaCar extends Car {
18+
public int substract(int a, int b) {
19+
return a - b;
20+
}
2021

21-
public int getNoOfWheels() {
22-
return this.noOfWheels;
22+
public int multiply(int a, int b) {
23+
return a * b;
2324
}
24-
}
2525

26-
class HybridCar extends Car , HondaCar{
26+
public int divide(int a, int b) {
27+
return a / b;
28+
}
29+
30+
public int reminder(int a, int b) {
31+
return a % b;
32+
}
33+
34+
}
2735

36+
class MyCalculator extends Calendar{
37+
38+
2839
}

src/main/java/com/javaprogramto/keywords/extend/InterfaceExtendsExamples.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ interface CreditCardPayment extends Payment{
2323
interface UPIPayment extends Payment{
2424

2525
void validateUPIAddress();
26-
}
26+
}

0 commit comments

Comments
 (0)