Learnitweb

Category: Java tutorial

  • Stream takeWhile method in Java

    1. Introduction The takewhile method was added in Stream interface in Java 9. takewhile is a default method in Stream interface. Following is the signature of takewhile: This method returns the stream of elements that matches the given predicate. If this stream is ordered, it returns a stream comprising the longest sequence of elements from…

  • Indify String Concatenation

    1. Introduction This enhancement changed the static String-concatenation bytecode sequence generated by javac to use invokedynamic calls to JDK library functions. This will enable future optimizations of String concatenation without requiring further changes to the bytecode created by javac. The invokedynamic instruction simplifies and potentially improves implementations of compilers and runtime systems for dynamic languages…

  • Java 9 SafeVarargs Annotation Enhancements

    1. Introduction In this article, we’ll discuss the SafeVarargs annotation enhancement in Java 9. Before discussing that, we’ll discuss briefly about the @SafeVarargs annotation. From Java 9 onwards, you can also use @SafeVarargs annotation for private instance methods also. 2. @SafeVarargs Annotation If you use var-arg methods with generic type then at runtime if one…

  • Java 9 Diamond operator enhancement for anonymous class

    1. Introduction From Java 9, Diamond operator can be used for anonymous classes. Until Java 8, you could not apply diamond operator for anonymous generic classes. To understand what this statement means, we’ll first discuss the usage of diamond operator before Java 9. And then the enhancement in Java 9. 2. Java 7 diamond operator…

  • Java 9 try-with-resources enhancement

    1. Introduction From JDK 9 onwards, you can use the resource reference variables created outside of try block directly in try block resources’ list. That is, the resource reference variables need not be local to try block. Before Java 9, the resource reference variables created outside of try block can not be used as a…

  • Private methods in Java 9 interfaces

    1. Introduction From Java 9, private methods can be added to Java interfaces. In this tutorial, we’ll discuss the purpose of adding private methods in Java and how to define these methods in an interface. 2. Why private methods in Java interface? Java 8 allowed interface to have methods with implementation logic. Such methods are…

  • Spliterator in Java

    1. Introduction In this tutorial, we’ll discuss Spliterator interface. This is one of the items added in Java 8 for parallel processing. The Spliterator interface was added in Java 8. Its name stands for “splitable iterator”. Spliterators, like Iterators, are for traversing the elements of a source. Spliterators can traverse the elements of a source…

  • Compact Strings in Java 9

    1. Introduction JEP 254 introduced a more space-efficient internal representation of strings. This was introduced in Java 9. A String in Java is internally represented as char[] array. Since Java uses UTF-16, each character of char[] takes up space of 2 bytes (sixteen bits). In most of the applications, strings are prominently used and consume…

  • Convenience Factory Methods for Collections

    1. Introduction JEP 269 introduced this enhancement which is the one-liner concise way of creating compact, unmodifiable collection instances with small number of elements. This enhancement of Java 9 defines library APIs to make it convenient to create instances of collections and maps with small number of elements. The purpose of these factory methods for…

  • Metaspace – Class Metadata in Java 8

    1. Introduction Java classes are not saved as it is and have internal representation in JVM. This internal representation is referred to as class metadata. Before Java 8, the class metadata was allocated in space called as “permanent generation“. This permanent generation space is commonly known as Permgen space. Starting with JDK 8, the permanent…