Practice Questions — Variables and Data Types in Java
← Back to NotesTopic-Specific Questions
Question 1
Easy
What is the output?
int x = 10;
System.out.println(x);Simple variable declaration and print.
10Question 2
Easy
What is the output?
double price = 99.99;
System.out.println(price);double stores decimal numbers.
99.99Question 3
Easy
What is the output?
char ch = 'Z';
System.out.println(ch);char stores a single character in single quotes.
ZQuestion 4
Easy
What is the output?
boolean isJavaFun = true;
System.out.println(isJavaFun);boolean can only be true or false.
trueQuestion 5
Easy
What is the output?
int a = 10;
double b = a;
System.out.println(b);int to double is widening (implicit) casting.
10.0Question 6
Medium
What is the output?
double x = 9.78;
int y = (int) x;
System.out.println(y);Narrowing cast truncates, does NOT round.
9Question 7
Medium
What is the output?
int a = Integer.MAX_VALUE;
System.out.println(a);
System.out.println(a + 1);What happens when you exceed the maximum value of int?
2147483647-2147483648Question 8
Medium
What is the output?
char ch = 'A';
System.out.println(ch + 1);
System.out.println((char)(ch + 1));char is a numeric type. 'A' is 65 in Unicode.
66BQuestion 9
Medium
What is the output?
byte b = 127;
b = (byte)(b + 1);
System.out.println(b);byte range is -128 to 127. What happens at 128?
-128Question 10
Medium
What is the difference between int and Integer in Java?
One is a primitive, the other is a wrapper class.
int is a primitive type that stores a 32-bit integer value directly. Integer is a wrapper class that wraps an int in an object. int cannot be null, cannot be used in collections, and has no methods. Integer can be null, can be stored in collections (like ArrayList), and provides utility methods like parseInt(), toBinaryString(), and constants like MAX_VALUE.Question 11
Hard
What is the output?
float f = 1.0f / 3;
System.out.println(f);
double d = 1.0 / 3;
System.out.println(d);float has ~7 digits precision, double has ~15.
0.333333340.3333333333333333Question 12
Hard
What is the output?
int x = 5;
int y = 2;
System.out.println(x / y);
System.out.println((double) x / y);
System.out.println(x / (double) y);Integer division truncates. Casting one operand to double forces floating-point division.
22.52.5Question 13
Hard
What is the output?
byte a = 50;
byte b = 60;
// byte c = a + b; // This would cause an error!
int c = a + b;
System.out.println(c);In Java, byte + byte is promoted to int.
110Question 14
Hard
Why can you write
byte b = 50; but not byte b = 50 + 50;?The compiler can verify 50 fits in a byte at compile time, but 50 + 50 is computed at runtime.
byte b = 50; works because the compiler can verify at compile time that 50 fits within byte's range (-128 to 127). byte b = 50 + 50; fails because 50 + 50 is evaluated as an int expression (100), and assigning an int to a byte requires explicit casting even though 100 fits in a byte. The compiler does not evaluate arithmetic expressions to check if the result fits.Question 15
Hard
What is the output?
Integer a = 127;
Integer b = 127;
Integer c = 128;
Integer d = 128;
System.out.println(a == b);
System.out.println(c == d);Java caches Integer objects for values -128 to 127.
truefalseQuestion 16
Medium
What is the difference between
var x = 10; and int x = 10; in Java?var is type inference, not dynamic typing.
Both produce the same result: a variable
x of type int with value 10. With int x = 10, you explicitly state the type. With var x = 10, the compiler infers the type as int from the assigned value. After declaration, both are identically int. var is not dynamic typing; you cannot later assign a String to x.Question 17
Easy
What is the output?
final int MAX = 100;
System.out.println(MAX);
// MAX = 200; // Would this work?final makes a variable constant.
100The commented line
MAX = 200 would cause a compiler error: cannot assign a value to final variable MAX.Mixed & Application Questions
Question 1
Easy
Declare variables for a student's name (String), age (int), CGPA (double), grade (char), and placement status (boolean). Initialize them with sample values and print each.
Use appropriate types for each value.
public class StudentInfo {
public static void main(String[] args) {
String name = "Rohan";
int age = 21;
double cgpa = 8.45;
char grade = 'A';
boolean isPlaced = true;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("CGPA: " + cgpa);
System.out.println("Grade: " + grade);
System.out.println("Placed: " + isPlaced);
}
}Question 2
Medium
What is the output?
int a = 5, b = 2;
System.out.println(a / b);
System.out.println(a % b);
System.out.println((double) a / b);Integer division truncates. Modulus gives remainder. Casting forces float division.
212.5Question 3
Medium
Write a program that demonstrates widening casting by storing a byte value into short, int, long, float, and double variables, printing each.
byte -> short -> int -> long -> float -> double is the widening chain.
public class WideningDemo {
public static void main(String[] args) {
byte b = 42;
short s = b;
int i = s;
long l = i;
float f = l;
double d = f;
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("int: " + i);
System.out.println("long: " + l);
System.out.println("float: " + f);
System.out.println("double: " + d);
}
}Question 4
Medium
What is the output?
String s1 = "Hello";
String s2 = s1;
s1 = "World";
System.out.println(s1);
System.out.println(s2);Strings are immutable. Reassigning s1 does not change s2.
WorldHelloQuestion 5
Hard
What is the output?
int i = 10;
float f = i;
int j = (int) f;
System.out.println(i == j);
System.out.println(f);
int big = 16777217;
float bigF = big;
int bigBack = (int) bigF;
System.out.println(big == bigBack);float has limited precision (~7 digits). Large int values may lose precision when converted to float.
true10.0falseQuestion 6
Hard
Explain why
byte + byte gives an int in Java. What design decision does this reflect?Think about integer arithmetic and overflow.
In Java, all integer arithmetic on types smaller than int (byte, short, char) automatically promotes operands to
int before the operation. This is called integer promotion. The result is always at least an int. This prevents overflow during intermediate calculations: byte a = 100; byte b = 100; -- their sum (200) exceeds byte range but fits in int.Question 7
Hard
What is the output?
Integer x = null;
System.out.println(x);
// int y = x; // What would happen?
System.out.println("Done");println(null) works, but unboxing null to int throws an exception.
nullDoneIf the commented line were uncommented, it would throw
NullPointerException at runtime because unboxing null to int is illegal.Question 8
Hard
Write a program that takes an ASCII value (int) and prints the corresponding character, and vice versa. Demonstrate with values 65, 97, and characters 'Z', 'z'.
Cast int to char and char to int.
public class AsciiConverter {
public static void main(String[] args) {
System.out.println("Int -> Char:");
System.out.println("65 -> " + (char) 65);
System.out.println("97 -> " + (char) 97);
System.out.println("\nChar -> Int:");
System.out.println("'Z' -> " + (int) 'Z');
System.out.println("'z' -> " + (int) 'z');
}
}Question 9
Medium
What is the output?
var name = "Aarav";
var age = 20;
var gpa = 8.5;
System.out.println(name.getClass().getSimpleName());
System.out.println(((Object) age).getClass().getSimpleName());
System.out.println(((Object) gpa).getClass().getSimpleName());var infers the type from the value. getClass() shows the runtime type.
StringIntegerDoubleQuestion 10
Medium
What are the default values for class-level fields of type int, double, boolean, char, and String?
Only class fields get defaults. Local variables do not.
Default values for class-level fields:
int = 0, double = 0.0, boolean = false, char = '\u0000' (null character), String = null. Local variables inside methods do NOT get default values and must be initialized before use.Question 11
Easy
What is the output?
int x = 10;
int y = 3;
System.out.println(x / y);
System.out.println(x % y);Integer division and modulus.
31Multiple Choice Questions
MCQ 1
How many primitive data types does Java have?
Answer: C
C is correct. Java has exactly 8 primitive types: byte, short, int, long, float, double, char, boolean.
C is correct. Java has exactly 8 primitive types: byte, short, int, long, float, double, char, boolean.
MCQ 2
What is the default value of a boolean instance variable?
Answer: B
B is correct. The default value of a boolean instance variable is
B is correct. The default value of a boolean instance variable is
false. Unlike C/C++, Java's boolean is not numeric, so 0 (C) is incorrect. null (D) is for reference types.MCQ 3
Which data type is used to store a single character in Java?
Answer: B
B is correct.
B is correct.
char stores a single Unicode character (e.g., char g = 'A';). String (A) stores multiple characters. byte (C) stores a number. Character (D) is the wrapper class for char.MCQ 4
What suffix is required for a long literal in Java?
Answer: A
A is correct. Long literals require the
A is correct. Long literals require the
L suffix (uppercase recommended to avoid confusion with digit 1). d/D is for double. f/F is for float.MCQ 5
What is the size of an int in Java?
Answer: B
B is correct. An int is always 4 bytes (32 bits) in Java, regardless of platform. Unlike C/C++ where int size is platform-dependent, Java guarantees fixed sizes for all primitive types.
B is correct. An int is always 4 bytes (32 bits) in Java, regardless of platform. Unlike C/C++ where int size is platform-dependent, Java guarantees fixed sizes for all primitive types.
MCQ 6
What is the output of: System.out.println((int) 7.9)?
Answer: A
A is correct. Narrowing cast from double to int truncates the decimal part.
A is correct. Narrowing cast from double to int truncates the decimal part.
(int) 7.9 gives 7, not 8. Casting does NOT round; it simply removes the decimal portion.MCQ 7
Which of these is a valid float literal?
Answer: D
D is correct. Both
D is correct. Both
3.14f and 3.14F are valid float literals. 3.14 (A) is a double literal. The f/F suffix is required to denote a float literal.MCQ 8
What is the result of: Integer.MAX_VALUE + 1?
Answer: C
C is correct. Integer overflow wraps around. MAX_VALUE (2147483647) + 1 wraps to MIN_VALUE (-2147483648). Java does not throw an error for integer overflow; it uses two's complement wraparound.
C is correct. Integer overflow wraps around. MAX_VALUE (2147483647) + 1 wraps to MIN_VALUE (-2147483648). Java does not throw an error for integer overflow; it uses two's complement wraparound.
MCQ 9
What is the wrapper class for the char primitive type?
Answer: B
B is correct. The wrapper class for
B is correct. The wrapper class for
char is Character. Note that it is not Char (A) -- the full word is used. String (D) is for text, not a wrapper for char.MCQ 10
Which line will cause a compiler error?
Line 1: int x = 10;
Line 2: long y = x;
Line 3: byte z = x;
Line 1: int x = 10;
Line 2: long y = x;
Line 3: byte z = x;
Answer: C
C is correct. Line 3 attempts to assign an int to a byte without explicit casting (narrowing conversion). This requires:
C is correct. Line 3 attempts to assign an int to a byte without explicit casting (narrowing conversion). This requires:
byte z = (byte) x;. Line 2 is fine because int to long is widening (automatic).MCQ 11
What is the default value of a String instance variable?
Answer: C
C is correct. String is a reference type. The default value of all reference type instance variables is
C is correct. String is a reference type. The default value of all reference type instance variables is
null. An empty string "" (A) is an actual String object; null means no object at all.MCQ 12
What is the output of: System.out.println((byte) 130)?
Answer: B
B is correct. byte range is -128 to 127. Casting 130 to byte: 130 - 256 = -126. In binary, 130 is 10000010, which in a signed byte (two's complement) is -126. This overflow behavior is a common interview question.
B is correct. byte range is -128 to 127. Casting 130 to byte: 130 - 256 = -126. In binary, 130 is 10000010, which in a signed byte (two's complement) is -126. This overflow behavior is a common interview question.
MCQ 13
Which statement about Java's var keyword is FALSE?
Answer: B
B is the FALSE statement.
B is the FALSE statement.
var does NOT make Java dynamically typed. It is compile-time type inference -- the compiler determines the type from the initializer, and the variable is statically typed thereafter. You cannot assign a different type later.MCQ 14
What happens when you unbox a null Integer to int?
Answer: D
D is correct. Unboxing null throws
D is correct. Unboxing null throws
NullPointerException at runtime. The compiler does not catch this error. Integer x = null; int y = x; compiles fine but throws NPE when executed. This is a common source of production bugs.MCQ 15
Why does
Integer a = 127; Integer b = 127; System.out.println(a == b); print true, but with 128 it prints false?Answer: B
B is correct. Java maintains an Integer cache for values between -128 and 127. For these values, autoboxing returns the same cached object, so
B is correct. Java maintains an Integer cache for values between -128 and 127. For these values, autoboxing returns the same cached object, so
== (which compares references) returns true. For values outside this range, new objects are created, so == returns false. Use .equals() to compare Integer values reliably.MCQ 16
What is the output of:
char c = 65; System.out.println(c);?Answer: B
B is correct. The int value 65 is assigned to a char variable. Since 65 is within char's range and is the Unicode value for 'A',
B is correct. The int value 65 is assigned to a char variable. Since 65 is within char's range and is the Unicode value for 'A',
c stores 'A'. When printed as a char, it shows A, not 65. To print the number, cast to int: (int) c.MCQ 17
Which of the following is NOT a valid variable name in Java?
Answer: C
C is correct. Variable names in Java cannot start with a digit.
C is correct. Variable names in Java cannot start with a digit.
2ndPlace starts with '2', which is illegal. Names can start with a letter, underscore (_), or dollar sign ($). So _count, $price, and firstName are all valid.MCQ 18
What is the output of:
System.out.println(5 / 2);?Answer: B
B is correct. Both 5 and 2 are int literals. Integer division in Java truncates the decimal part.
B is correct. Both 5 and 2 are int literals. Integer division in Java truncates the decimal part.
5 / 2 = 2 (not 2.5). To get 2.5, cast one operand: (double) 5 / 2.MCQ 19
What keyword makes a variable's value unchangeable in Java?
Answer: C
C is correct. The
C is correct. The
final keyword prevents reassignment. const (A) is a reserved word in Java but not used. static (B) means class-level, not constant. immutable (D) is not a Java keyword.MCQ 20
In the widening chain byte->short->int->long->float->double, which conversion can lose precision?
Answer: B
B is correct. Long has 64-bit precision for integers, but float has only ~23 bits of mantissa (~7 decimal digits). Converting a large long to float can lose precision. For example,
B is correct. Long has 64-bit precision for integers, but float has only ~23 bits of mantissa (~7 decimal digits). Converting a large long to float can lose precision. For example,
(float) 16777217L gives 16777216.0f. Similarly, int to float can lose precision for large values. All other conversions in the options preserve full precision.Coding Challenges
Challenge 1: Data Type Explorer
EasyWrite a program that declares one variable of each of the 8 primitive types, initializes them, and prints each with its type name. Also print the size in bytes using the wrapper class SIZE constant.
Sample Input
(No input required)
Sample Output
byte value: 42 (size: 1 byte)
short value: 2026 (size: 2 bytes)
int value: 100000 (size: 4 bytes)
long value: 9876543210 (size: 8 bytes)
float value: 3.14 (size: 4 bytes)
double value: 2.718281828 (size: 8 bytes)
char value: A (size: 2 bytes)
boolean value: true
Use Byte.SIZE, Short.SIZE, etc. (these give size in bits, divide by 8 for bytes).
public class DataTypeExplorer {
public static void main(String[] args) {
byte b = 42;
short s = 2026;
int i = 100000;
long l = 9876543210L;
float f = 3.14f;
double d = 2.718281828;
char c = 'A';
boolean bool = true;
System.out.println("byte value: " + b + " (size: " + Byte.SIZE/8 + " byte)");
System.out.println("short value: " + s + " (size: " + Short.SIZE/8 + " bytes)");
System.out.println("int value: " + i + " (size: " + Integer.SIZE/8 + " bytes)");
System.out.println("long value: " + l + " (size: " + Long.SIZE/8 + " bytes)");
System.out.println("float value: " + f + " (size: " + Float.SIZE/8 + " bytes)");
System.out.println("double value: " + d + " (size: " + Double.SIZE/8 + " bytes)");
System.out.println("char value: " + c + " (size: " + Character.SIZE/8 + " bytes)");
System.out.println("boolean value: " + bool);
}
}Challenge 2: Temperature Converter
EasyWrite a program that converts a temperature from Celsius to Fahrenheit and Kelvin. Use double for precision. Formula: F = C * 9/5 + 32, K = C + 273.15. Test with 37.5 degrees Celsius.
Sample Input
(No input required)
Sample Output
Celsius: 37.50
Fahrenheit: 99.50
Kelvin: 310.65
Use double for all variables. Print with 2 decimal places using printf.
public class TempConverter {
public static void main(String[] args) {
double celsius = 37.5;
double fahrenheit = celsius * 9.0 / 5 + 32;
double kelvin = celsius + 273.15;
System.out.printf("Celsius: %.2f%n", celsius);
System.out.printf("Fahrenheit: %.2f%n", fahrenheit);
System.out.printf("Kelvin: %.2f%n", kelvin);
}
}Challenge 3: Casting Chain Demonstrator
MediumWrite a program that starts with a double value of 99.99 and demonstrates the effect of casting it to float, long, int, short, and byte. Print each result to show how data is lost at each step.
Sample Input
(No input required)
Sample Output
Original double: 99.99
As float: 99.99
As long: 99
As int: 99
As short: 99
As byte: 99
Use explicit narrowing casts. Show comments explaining what happens at each step.
public class CastingChain {
public static void main(String[] args) {
double d = 99.99;
float f = (float) d;
long l = (long) d;
int i = (int) d;
short s = (short) d;
byte b = (byte) d;
System.out.println("Original double: " + d);
System.out.println("As float: " + f);
System.out.println("As long: " + l + " (decimal truncated)");
System.out.println("As int: " + i + " (decimal truncated)");
System.out.println("As short: " + s);
System.out.println("As byte: " + b);
System.out.println("\nNow with 999.99:");
d = 999.99;
System.out.println("double: " + d);
System.out.println("As byte: " + (byte) d + " (overflow!)");
}
}Challenge 4: Student Grade Calculator
MediumAarav has marks in 5 subjects: 85, 92, 78, 95, 88. Write a program that calculates total marks, percentage (double), average (double), and assigns a grade character: A+ (>=90), A (>=80), B+ (>=70), B (>=60), C (<60). Use type casting where needed.
Sample Input
(No input required)
Sample Output
Marks: 85 92 78 95 88
Total: 438
Percentage: 87.60%
Average: 87.60
Grade: A
Use int for marks and total. Use double for percentage and average. Use char for grade. Use explicit casting for division.
public class GradeCalculator {
public static void main(String[] args) {
int m1 = 85, m2 = 92, m3 = 78, m4 = 95, m5 = 88;
int total = m1 + m2 + m3 + m4 + m5;
double percentage = (double) total / 5;
double average = percentage;
char grade;
if (percentage >= 90) grade = 'O';
else if (percentage >= 80) grade = 'A';
else if (percentage >= 70) grade = 'B';
else if (percentage >= 60) grade = 'C';
else grade = 'F';
System.out.println("Marks: " + m1 + " " + m2 + " " + m3 + " " + m4 + " " + m5);
System.out.println("Total: " + total);
System.out.printf("Percentage: %.2f%%%n", percentage);
System.out.printf("Average: %.2f%n", average);
System.out.println("Grade: " + grade);
}
}Challenge 5: ASCII Table Generator
MediumWrite a program that prints the ASCII table for characters A-Z and a-z, showing the character and its integer value. Use char-to-int casting.
Sample Input
(No input required)
Sample Output
=== Uppercase Letters ===
A=65 B=66 C=67 ... Z=90
=== Lowercase Letters ===
a=97 b=98 c=99 ... z=122
Use a for loop with char variable. Cast char to int for the numeric value.
public class AsciiTable {
public static void main(String[] args) {
System.out.println("=== Uppercase Letters ===");
for (char c = 'A'; c <= 'Z'; c++) {
System.out.print(c + "=" + (int) c + " ");
if ((c - 'A' + 1) % 10 == 0) System.out.println();
}
System.out.println("\n\n=== Lowercase Letters ===");
for (char c = 'a'; c <= 'z'; c++) {
System.out.print(c + "=" + (int) c + " ");
if ((c - 'a' + 1) % 10 == 0) System.out.println();
}
System.out.println();
}
}Challenge 6: Integer Overflow Detector
HardWrite a program that demonstrates integer overflow for byte, short, and int by adding 1 to their MAX_VALUE. Also show what happens when you multiply two large ints (100000 * 100000) and compare with the long result.
Sample Input
(No input required)
Sample Output
byte MAX + 1: 127 + 1 = -128
short MAX + 1: 32767 + 1 = -32768
int MAX + 1: 2147483647 + 1 = -2147483648
int multiplication: 100000 * 100000 = 1410065408 (WRONG - overflow!)
long multiplication: 100000 * 100000 = 10000000000 (correct)
Use wrapper class MAX_VALUE constants. Show both the overflowed and correct results.
public class OverflowDetector {
public static void main(String[] args) {
byte maxByte = Byte.MAX_VALUE;
short maxShort = Short.MAX_VALUE;
int maxInt = Integer.MAX_VALUE;
System.out.println("byte MAX + 1: " + maxByte + " + 1 = " + (byte)(maxByte + 1));
System.out.println("short MAX + 1: " + maxShort + " + 1 = " + (short)(maxShort + 1));
System.out.println("int MAX + 1: " + maxInt + " + 1 = " + (maxInt + 1));
System.out.println();
int intResult = 100000 * 100000;
long longResult = 100000L * 100000;
System.out.println("int multiplication: 100000 * 100000 = " + intResult + " (WRONG - overflow!)");
System.out.println("long multiplication: 100000 * 100000 = " + longResult + " (correct)");
}
}Challenge 7: Wrapper Class Utility Showcase
HardWrite a program that demonstrates at least 8 useful methods/constants from wrapper classes: Integer.parseInt, Double.parseDouble, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.toBinaryString, Integer.toHexString, Character.isLetter, Character.isDigit, Character.toUpperCase.
Sample Input
(No input required)
Sample Output
Parsing: "42" -> 42, "3.14" -> 3.14
Ranges: int [-2147483648, 2147483647]
Conversions: 255 -> binary: 11111111, hex: ff
Char checks: 'A' isLetter: true, '5' isDigit: true
Char conversion: 'a' toUpperCase: 'A'
Demonstrate each method clearly with labeled output.
public class WrapperUtility {
public static void main(String[] args) {
System.out.println("=== Parsing ===");
int parsed = Integer.parseInt("42");
double parsedD = Double.parseDouble("3.14");
System.out.println("\"42\" -> " + parsed);
System.out.println("\"3.14\" -> " + parsedD);
System.out.println("\n=== Ranges ===");
System.out.println("int: [" + Integer.MIN_VALUE + ", " + Integer.MAX_VALUE + "]");
System.out.println("long: [" + Long.MIN_VALUE + ", " + Long.MAX_VALUE + "]");
System.out.println("\n=== Number Conversions ===");
System.out.println("255 binary: " + Integer.toBinaryString(255));
System.out.println("255 hex: " + Integer.toHexString(255));
System.out.println("255 octal: " + Integer.toOctalString(255));
System.out.println("\n=== Character Utilities ===");
System.out.println("'A' isLetter: " + Character.isLetter('A'));
System.out.println("'5' isDigit: " + Character.isDigit('5'));
System.out.println("'a' toUpperCase: " + Character.toUpperCase('a'));
System.out.println("'Z' toLowerCase: " + Character.toLowerCase('Z'));
System.out.println("' ' isWhitespace: " + Character.isWhitespace(' '));
}
}Challenge 8: Precision Comparison
HardWrite a program that demonstrates the precision difference between float and double by computing 1.0/3.0 and 2.0/3.0 in both types. Also show that 0.1 + 0.2 does not equal 0.3 in floating-point arithmetic. Explain why in comments.
Sample Input
(No input required)
Sample Output
float 1/3: 0.33333334
double 1/3: 0.3333333333333333
float 2/3: 0.6666667
double 2/3: 0.6666666666666666
0.1 + 0.2 = 0.30000000000000004
0.1 + 0.2 == 0.3? false
Show both float and double results. Demonstrate the 0.1 + 0.2 problem. Include comments explaining why.
public class PrecisionDemo {
public static void main(String[] args) {
// float has ~7 decimal digits precision
float f1 = 1.0f / 3;
float f2 = 2.0f / 3;
// double has ~15 decimal digits precision
double d1 = 1.0 / 3;
double d2 = 2.0 / 3;
System.out.println("float 1/3: " + f1);
System.out.println("double 1/3: " + d1);
System.out.println("float 2/3: " + f2);
System.out.println("double 2/3: " + d2);
// The 0.1 + 0.2 problem
// 0.1 and 0.2 cannot be represented exactly in binary
// floating-point, leading to tiny rounding errors
System.out.println("\n0.1 + 0.2 = " + (0.1 + 0.2));
System.out.println("0.1 + 0.2 == 0.3? " + (0.1 + 0.2 == 0.3));
System.out.println("\nFor financial calculations, use BigDecimal.");
}
}Need to Review the Concepts?
Go back to the detailed notes for this chapter.
Read Chapter NotesWant to learn Java with a live mentor?
Explore our Java Masterclass