Stack
of integer values
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Integer
user input
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
java.util.Locale
Lecture notes |
Pdf slides |
|
Locale
properties
Lecture notes |
Pdf slides |
|
NumberFormat
instance
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
float
precision
Lecture notes |
Pdf slides |
|
double
precision
Lecture notes |
Pdf slides |
|
BigDecimal
Lecture notes |
Pdf slides |
|
BigDecimal
operations
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
BigDecimal
features
Lecture notes |
Pdf slides |
|
static
double random()
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
|
10 1 3 |
Supports searching of objects based on:
Objects only, no primitive types!
|
java.lang.Integer java.lang.Integer java.lang.Integer |
|
|
|
Enter an integer:-34 You entered -34 Enter an integer:five Sorry, but 'five' is not an integer. |
Value: 13 |
Exception in thread "main" java.lang.NumberFormatException: For input string: "201" ... at de.hdm_stuttgart.sd1... |
A Locale object represents a specific geographical, political, or cultural region.
An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user.
For example, displaying a number is a locale-sensitive operation: Numbers should be formatted according to the customs and conventions of the user's native country, region, or culture.
Language
Encoding
Country
Extensible
final NumberFormat standard = new DecimalFormat(); System.out.println(standard.format(1234.5678)); final NumberFormat de = DecimalFormat.getInstance(Locale.GERMANY); System.out.println(de.format(1234.5678)); |
1234.5678 1.234,568 |
final DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(Locale.getDefault()); unusualSymbols.setDecimalSeparator('|'); unusualSymbols.setGroupingSeparator('^'); final String strange = "#,##0.###"; final DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols); weirdFormatter.setGroupingSize(4); System.out.println(weirdFormatter.format(12345.678));
1^2345|678
final NumberFormat de = NumberFormat.getInstance(Locale.GERMANY),
us = NumberFormat.getInstance(Locale.US);
try {
final Number[] values = {
de.parse("103.234"), de.parse("400.000,234"),
us.parse("103.234"), us.parse("400.000,234"),};
for (final Number n: values) {
System.out.println(n + "(" + n.getClass().getTypeName() + ")");
} catch (ParseException e) { ... }
103234(java.lang.Long) 400000.234(java.lang.Double) 103.234(java.lang.Double) 400(java.lang.Long)
final float result = 0.99f - 0.1f -0.1f -0.1f;
System.out.println(result);
0.68999994
final double result = 0.99 - 0.1 -0.1 -0.1;
System.out.println(result);
0.6900000000000001
final BigDecimal zero_dot_99 = new BigDecimal("0.99");
final BigDecimal zero_dot_1 = new BigDecimal("0.1");
BigDecimal
result = zero_dot_99.subtract(zero_dot_1); // Subtracting 0.1
result = result.subtract(zero_dot_1); // Subtracting 0.1
result = result.subtract(zero_dot_1); // Subtracting 0.1
System.out.println(result);
0.69
final BigDecimal zero_dot_99 = new BigDecimal("0.99");
final BigDecimal zero_dot_1 = new BigDecimal("0.1");
BigDecimal result = zero_dot_99.
subtract(zero_dot_1).
subtract(zero_dot_1).
subtract(zero_dot_1);
System.out.println(result);
0.69
Higher memory allocation hosting higher precision.
Immutable instances
Calculation performance penalty.
Clumsy interface.
for (int i = 0; i < 10; i++) {
System.out.println(Math.random());
}
0.4754286988826202 0.0060114391743414375 ... 0.9047785351372987 0.2516070321935864
try(final Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter an integer seed:");
final long seed = scanner.nextLong();
Random generator = new Random(seed);
for (int i = 0; i < 10; i++) {
System.out.print(generator.nextBoolean() + " ");
}
}
Enter an integer seed:4237549835735 false true true true false false false true false true