Java Workshop Enhancements in Packages java.lang and

Werbung
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Java Workshop
Enhancements in Packages
java.lang and java.util
Michael Kelnhofer
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Enhancements in java.lang and java.util
 Unsigned Arithmetic Support
 Standard Encoding and Decoding Base64
 Parallel Array Sorting
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Unsigned Arithmetic Support
Before Java 8:
 Unsigned Integer doesn't exist.
 All Integer stored in two's complement form.
 Problem: Integer.MaxValue + 1; equals: -2147483648
 Over- and Underflow was not easy to detect.
 Similar problems with bit manipulations.
Now Java 8 has introduce new unsigned integer and long
methods, which interprets integer as unsigned.
There are no new data types.
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Unsigned Arithmetic Support
Unsigned integer methods
Description
compareUnsigned(int x, int y)
Compare x and y as unsigned values.
divideUnsigned(int dividend, int divisor)
Return the unsigned quotient of dividing
dividend by divisor.
parseUnsignedInt(String s)
Parse s as an unsigned decimal integer.
parseUnsignedInt(String s, int radix)
Parse s as an unsigned integer
according to radix.
remainderUnsigned(int dividend, int divisor)
Return the unsigned remainder of
dividing dividend by divisor.
toUnsignedLong(int x)
Convert x to a long by an unsigned
conversion.
toUnsignedString(int i)
Return a string representation of i as an
unsigned decimal value.
toUnsignedString(int i, int radix)
Return a string representation of i as an
unsigned integer in the specified radix.
Long likewise offers similar methods.
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Unsigned Arithmetic Support
Infos and Examples:
http://www.informit.com/articles/article.aspx?p=2216988&seqNum=2
API:
http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html
http://docs.oracle.com/javase/8/docs/api/java/lang/Long.html
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Integer Arithmetic
Overflow/Underflow Detection API
Methods
Description
addExact(int x, int y)
addExact(long x, long y)
Returns the sum of its arguments.
decrementExact(int a)
decrementExact(long a)
Returns the argument decremented by one.
incrementExact(int a)
incrementExact(long a)
Returns the argument incremented by one.
multiplyExact(int x, int y)
multiplyExact(long x, long y)
Returns the product of the arguments.
negateExact(int a)
negateExact(long a)
Returns the negation of the argument.
subtractExact(int x, int y)
subtractExact(long x, long y)
Returns the difference of the arguments.
toIntExact(long value)
Returns the value of the long argument.
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Integer Arithmetic
Overflow/Underflow Detection API
Infos and Examples:
http://www.informit.com/articles/article.aspx?p=2216988&seqNum=3
API:
http://docs.oracle.com/javase/8/docs/api/index.html?java/lang/Math.html
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Standard Encoding and Decoding Base64
 Base64 are algorithms to the coding of binary data in
strings for transmission.
 In Java 8 there are 3 methods for en- and decoding:
 Basic: The encoder does not add any line separator
character. The decoder rejects data that contains
characters outside the base64 alphabet.
 URL and Filename safe:
Similarly as basic with small exception with the alphabet.
 MIME: The encoded output must be represented in lines
of no more than 76 characters each and uses line
separators. All line separators or other characters
outside the base64 alphabet are ignored in decoding.
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Standard Encoding and Decoding Base64
final String textOrg = "Polyfon zwitschernd aßen
Mäxchens Vögel: Rüben, Joghurt und Quark";
byte[] code =
Base64.getEncoder().encode(textOrg.getBytes());
byte[] text = Base64.getDecoder().decode(code);
code =
Base64.getMimeEncoder().encode(textOrg.getBytes());
text = Base64.getMimeDecoder().decode(code);
code =
Base64.getUrlEncoder().encode(textOrg.getBytes());
text = Base64.getDecoder().decode(code);
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Standard Encoding and Decoding Base64
Infos and Examples:
http://docs.oracle.com/javase/8/docs/technotes/guides/lang/enhancem
API:
http://docs.oracle.com/javase/8/docs/api/java/util/Base64.html
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Parallel Array Sorting
private static final int DATA_SIZE = 10_000_000;
public static void main(String[] args) {
int[] data = new int[DATA_SIZE];
int[] dataWork = new int[DATA_SIZE];
LocalDateTime start;
Arrays.setAll(data, v -> ThreadLocalRandom.current()
.nextInt(Integer.MAX_VALUE));
System.arraycopy(data, 0, dataWork, 0, DATA_SIZE);
start = LocalDateTime.now();
Arrays.sort(dataWork);
System.out.println("Sequential finished in: " +
Duration.between(start, LocalDateTime.now()).toMillis());
System.arraycopy(data, 0, dataWork, 0, DATA_SIZE);
start = LocalDateTime.now();
Arrays.parallelSort(dataWork);
System.out.println("Parallel finished in: " +
Duration.between(start, LocalDateTime.now()).toMillis());
}
}
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Parallel Array Sorting
Infos and Examples:
https://vaskoz.wordpress.com/2013/07/21/java-8-parallel-array-sorting/
API:
http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html
GSI Helmholtzzentrum für Schwerionenforschung GmbH
Herunterladen