Java 15 – New Features ”; Previous Next Java 15 is a major feature release and it has brought many JVM specific changes and language specific changes to JAVA. It followed the Java release cadence introduced Java 10 onwards and it was released on Sep 2020, just six months after Java 14 release. Java 15 is a non-LTS release. New Features in Java 15 Following are the major new features which are introduced in Java 15. JEP 360 – Sealed Classes − To provide fine grained control over the inheritance. JEP 368 – Text Blocks − A second preview feature to handle multiline strings like JSON, XML easily. JEP 375 – Pattern matching Type Check − enhancements to existing pattern matching preview feature of Java 14. JEP 371 – Hidden Classes − To allow runtime creation of non-discoverable classes. JEP 384 – Records − A preview feature enhancing a new type record introduced in Java 14. JEP 383 – Foreign Memory Access API − Enhancement to incubating feature of java 14. JEP 377, 379 – Garbage Collectors − ZDC and Shenandoah garbage collectors are now part of standard API. JEP 339 – Edwards-Curve Digital Signature Algorithm (EdDSA) − Cryptographic signatures are now implemented using EdDSA. JEP 373 – Reimplement the Legacy DatagramSocket API − legacy implementations of the java.net.DatagramSocket and java.net.MulticastSocket APIs are replaced with simpler and more modern implementations which are easy to maintain and debug. Print Page Previous Next Advertisements ”;
Category: java Virtual Machine
Java – Characters
Java – Character Class ”; Previous Next Normally, when we work with characters, we use primitive data types char. Example char ch = ”a”; // Unicode for uppercase Greek omega character char uniChar = ”u039A”; // an array of chars char[] charArray ={ ”a”, ”b”, ”c”, ”d”, ”e” }; Use of Character Class in Java However in development, we come across situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides wrapper class Character for primitive data type char. Java Character Class The Character class offers a number of useful class (i.e., static) methods for manipulating characters. You can create a Character object with the Character constructor − Character ch = new Character(”a”); The Java compiler will also create a Character object for you under some circumstances. For example, if you pass a primitive char into a method that expects an object, the compiler automatically converts the char to a Character for you. This feature is called autoboxing or unboxing, if the conversion goes the other way. Example of Java Character Class // Here following primitive char ”a” // is boxed into the Character object ch Character ch = ”a”; // Here primitive ”x” is boxed for method test, // return is unboxed to char ”c” char c = test(”x”); Escape Sequences A character preceded by a backslash () is an escape sequence and has a special meaning to the compiler. The newline character (n) has been used frequently in this tutorial in System.out.println() statements to advance to the next line after the string is printed. Following table shows the Java escape sequences − Escape Sequence Description t Inserts a tab in the text at this point. b Inserts a backspace in the text at this point. n Inserts a newline in the text at this point. r Inserts a carriage return in the text at this point. f Inserts a form feed in the text at this point. ” Inserts a single quote character in the text at this point. “ Inserts a double quote character in the text at this point. \ Inserts a backslash character in the text at this point. When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. Example: Escape Sequences If you want to put quotes within quotes, you must use the escape sequence, “, on the interior quotes − public class Test { public static void main(String args[]) { System.out.println(“She said “Hello!” to me.”); } } Output She said “Hello!” to me. Character Class Declaration Following is the declaration for java.lang.Character class − public final class Character extends Object implements Serializable, Comparable<Character> Field Following are the fields for java.lang.Character class − static byte COMBINING_SPACING_MARK − This is the General category “Mc” in the Unicode specification. static byte CONNECTOR_PUNCTUATION − This is the General category “Pc” in the Unicode specification. static byte CONTROL − This is the General category “Cc” in the Unicode specification. static byte CURRENCY_SYMBOL − This is the General category “Sc” in the Unicode specification. static byte DASH_PUNCTUATION − This is the General category “Pd” in the Unicode specification. static byte DECIMAL_DIGIT_NUMBER − This is the General category “Nd” in the Unicode specification. static byte DIRECTIONALITY_ARABIC_NUMBER − This is the Weak bidirectional character type “AN” in the Unicode specification. static byte DIRECTIONALITY_BOUNDARY_NEUTRAL − This is the Weak bidirectional character type “BN” in the Unicode specification. static byte DIRECTIONALITY_COMMON_NUMBER_SEPARATOR − This is the Weak bidirectional character type “CS” in the Unicode specification. static byte DIRECTIONALITY_EUROPEAN_NUMBER − This is the Weak bidirectional character type “EN” in the Unicode specification. static byte DIRECTIONALITY_EUROPEAN_NUMBER_SEPARATOR − This is the Weak bidirectional character type “ES” in the Unicode specification. static byte DIRECTIONALITY_EUROPEAN_NUMBER_TERMINATOR − This is the Weak bidirectional character type “ET” in the Unicode specification. static byte DIRECTIONALITY_LEFT_TO_RIGHT − This is the Strong bidirectional character type “L” in the Unicode specification. static byte DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING − This is the Strong bidirectional character type “LRE” in the Unicode specification. static byte DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE − This is the Strong bidirectional character type “LRO” in the Unicode specification. static byte DIRECTIONALITY_NONSPACING_MARK − This is the Weak bidirectional character type “NSM” in the Unicode specification. static byte DIRECTIONALITY_OTHER_NEUTRALS − This is the Neutral bidirectional character type “ON” in the Unicode specification. static byte DIRECTIONALITY_PARAGRAPH_SEPARATOR − This is the Neutral bidirectional character type “B” in the Unicode specification. static byte DIRECTIONALITY_POP_DIRECTIONAL_FORMAT − This is the Weak bidirectional character type “PDF” in the Unicode specification. static byte DIRECTIONALITY_RIGHT_TO_LEFT − This is the Strong bidirectional character type “R” in the Unicode specification. static byte DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC − This is the Strong bidirectional character type “AL” in the Unicode specification. static byte DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING − This is the Strong bidirectional character type “RLE” in the Unicode specification. static byte DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE − This is the Strong bidirectional character type “RLO” in the Unicode specification. static byte DIRECTIONALITY_SEGMENT_SEPARATOR − This is the Neutral bidirectional character type “S” in the Unicode specification. static byte DIRECTIONALITY_UNDEFINED − This is the Undefined bidirectional character type. static byte DIRECTIONALITY_WHITESPACE − This is the Neutral bidirectional character type “WS” in the Unicode specification. static byte ENCLOSING_MARK − This is the General category “Me” in the Unicode specification. static byte END_PUNCTUATION − This is the General category “Pe” in the Unicode specification. static byte FINAL_QUOTE_PUNCTUATION − This
Java – Map Interface
Java – Map Interface ”; Previous Next Map Interface The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date. Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key. Several methods throw a NoSuchElementException when no items exist in the invoking map. A ClassCastException is thrown when an object is incompatible with the elements in a map. A NullPointerException is thrown if an attempt is made to use a null object and null is not allowed in the map. An UnsupportedOperationException is thrown when an attempt is made to change an unmodifiable map. Map Interface Methods Sr.No. Method & Description 1 void clear( ) Removes all key/value pairs from the invoking map. 2 boolean containsKey(Object k) Returns true if the invoking map contains k as a key. Otherwise, returns false. 3 boolean containsValue(Object v) Returns true if the map contains v as a value. Otherwise, returns false. 4 Set entrySet( ) Returns a Set that contains the entries in the map. The set contains objects of type Map.Entry. This method provides a set-view of the invoking map. 5 boolean equals(Object obj) Returns true if obj is a Map and contains the same entries. Otherwise, returns false. 6 Object get(Object k) Returns the value associated with the key k. 7 int hashCode( ) Returns the hash code for the invoking map. 8 boolean isEmpty( ) Returns true if the invoking map is empty. Otherwise, returns false. 9 Set keySet( ) Returns a Set that contains the keys in the invoking map. This method provides a set-view of the keys in the invoking map. 10 Object put(Object k, Object v) Puts an entry in the invoking map, overwriting any previous value associated with the key. The key and value are k and v, respectively. Returns null if the key did not already exist. Otherwise, the previous value linked to the key is returned. 11 void putAll(Map m) Puts all the entries from m into this map. 12 Object remove(Object k) Removes the entry whose key equals k. 13 int size( ) Returns the number of key/value pairs in the map. 14 Collection values( ) Returns a collection containing the values in the map. This method provides a collection-view of the values in the map. Classes that Implement Map The following are the classes that implement a Map to use the functionalities of a Map – HashMap EnumMap LinkedHashMap WeakHashMap TreeMap Interfaces that Extend Map The following are the interfaces that extend the Map interface – SortedMap NavigableMap ConcurrentMap Examples of Map Interface Example 1 Map has its implementation in various classes like HashMap. Following is an example to explain map functionality − import java.util.HashMap; import java.util.Map; public class CollectionsDemo { public static void main(String[] args) { Map<String, String> m1 = new HashMap<>(); m1.put(“Zara”, “8”); m1.put(“Mahnaz”, “31”); m1.put(“Ayan”, “12”); m1.put(“Daisy”, “14”); System.out.println(); System.out.println(” Map Elements”); System.out.print(“t” + m1); } } Output Map Elements {Daisy = 14, Ayan = 12, Zara = 8, Mahnaz = 31} Example 2 Map has its implementation in various classes like TreeMap which sorts the entries based on keys. Following is an example to explain map functionality using TreeMap − import java.util.Map; import java.util.TreeMap; public class CollectionsDemo { public static void main(String[] args) { Map<String, String> m1 = new TreeMap<>(); m1.put(“Zara”, “8”); m1.put(“Mahnaz”, “31”); m1.put(“Ayan”, “12”); m1.put(“Daisy”, “14”); System.out.println(); System.out.println(” Map Elements”); System.out.print(“t” + m1); } } Output Map Elements {Ayan=12, Daisy=14, Mahnaz=31, Zara=8} Example 3 Map has its implementation in various classes like HashMap. Following is an example to explain map functions using HashMap to add and remove elements to the map− import java.util.HashMap; import java.util.Map; public class CollectionsDemo { public static void main(String[] args) { Map<String, String> m1 = new HashMap<>(); m1.put(“Zara”, “8”); m1.put(“Mahnaz”, “31”); m1.put(“Ayan”, “12”); m1.put(“Daisy”, “14”); System.out.println(); System.out.println(” Map Elements”); System.out.print(“t” + m1); m1.remove(“Daisy”); System.out.println(” Map Elements”); System.out.print(“t” + m1); } } Output Map Elements {Daisy=14, Ayan=12, Zara=8, Mahnaz=31} Map Elements {Ayan=12, Zara=8, Mahnaz=31} java_collections.htm Print Page Previous Next Advertisements ”;
Java – Iterators
Java – How to Use Iterator? ”; Previous Next Often, you will want to cycle through the elements in a collection. For example, you might want to display each element. The easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface. Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements. Before you can access a collection through an iterator, you must obtain one. Each of the collection classes provides an iterator( ) method that returns an iterator to the start of the collection. By using this iterator object, you can access each element in the collection, one element at a time. In general, to use an iterator to cycle through the contents of a collection, follow these steps − Obtain an iterator to the start of the collection by calling the collection”s iterator( ) method. Set up a loop that makes a call to hasNext( ). Have the loop iterate as long as hasNext( ) returns true. Within the loop, obtain each element by calling next( ). For collections that implement List, you can also obtain an iterator by calling ListIterator. The Methods Declared by Iterator Sr.No. Method & Description 1 boolean hasNext( ) Returns true if there are more elements. Otherwise, returns false. 2 Object next( ) Returns the next element. Throws NoSuchElementException if there is not a next element. 3 void remove( ) Removes the current element. Throws IllegalStateException if an attempt is made to call remove( ) that is not preceded by a call to next( ). The Methods Declared by ListIterator Sr.No. Method & Description 1 void add(Object obj) Inserts obj into the list in front of the element that will be returned by the next call to next( ). 2 boolean hasNext( ) Returns true if there is a next element. Otherwise, returns false. 3 boolean hasPrevious( ) Returns true if there is a previous element. Otherwise, returns false. 4 Object next( ) Returns the next element. A NoSuchElementException is thrown if there is not a next element. 5 int nextIndex( ) Returns the index of the next element. If there is not a next element, returns the size of the list. 6 Object previous( ) Returns the previous element. A NoSuchElementException is thrown if there is not a previous element. 7 int previousIndex( ) Returns the index of the previous element. If there is not a previous element, returns -1. 8 void remove( ) Removes the current element from the list. An IllegalStateException is thrown if remove( ) is called before next( ) or previous( ) is invoked. 9 void set(Object obj) Assigns obj to the current element. This is the element last returned by a call to either next( ) or previous( ). Example 1 Here is an example demonstrating Iterator. It uses an ArrayList object, but the general principles apply to any type of collection. import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class IteratorDemo { public static void main(String args[]) { // Create an array list List<String> al = new ArrayList<>(); // add elements to the array list al.add(“C”); al.add(“A”); al.add(“E”); al.add(“B”); al.add(“D”); al.add(“F”); // Use iterator to display contents of al System.out.print(“Original contents of al: “); Iterator<String> itr = al.iterator(); while(itr.hasNext()) { Object element = itr.next(); System.out.print(element + ” “); } System.out.println(); } } Output Original contents of al: C A E B D F Example 2 Here is an example demonstrating ListIterator. It uses an ArrayList object, but the general principles apply to any type of collection. Of course, ListIterator is available only to those collections that implement the List interface. import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class IteratorDemo { public static void main(String args[]) { // Create an array list List<String> al = new ArrayList<>(); // add elements to the array list al.add(“C”); al.add(“A”); al.add(“E”); al.add(“B”); al.add(“D”); al.add(“F”); // Use iterator to display contents of al System.out.print(“Original contents of al: “); Iterator<String> itr = al.iterator(); while(itr.hasNext()) { Object element = itr.next(); System.out.print(element + ” “); } } } Output Original contents of al: C A E B D F Example 3 Here is an example demonstrating ListIterator to modify the list while iterating. It uses an ArrayList object, but the general principles apply to any type of collection. import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class IteratorDemo { public static void main(String args[]) { // Create an array list List<String> al = new ArrayList<>(); // add elements to the array list al.add(“C”); al.add(“A”); al.add(“E”); al.add(“B”); al.add(“D”); al.add(“F”); // Use iterator to display contents of al System.out.print(“Original contents of al: “); Iterator<String> itr = al.iterator(); while(itr.hasNext()) { Object element = itr.next(); System.out.print(element + ” “); } System.out.println(); // Modify objects being iterated ListIterator<String> litr = al.listIterator(); while(litr.hasNext()) { Object element = litr.next(); litr.set(element + “+”); } System.out.print(“Modified contents of al: “); itr = al.iterator(); while(itr.hasNext()) { Object element = itr.next(); System.out.print(element + ” “); } System.out.println(); // Now, display the list backwards System.out.print(“Modified list backwards: “); while(litr.hasPrevious()) { Object element = litr.previous(); System.out.print(element + ” “); } System.out.println(); } } Output Original contents of al: C A E B D F Modified contents of al: C+ A+ E+ B+ D+ F+ Modified list backwards: F+ D+ B+ E+ A+ C+ Print Page Previous Next Advertisements ”;