CharsetEncoderreset() method in Java, example

CharsetEncoder reset() method in Java, examples Original text: https://www . geesforgeks . org/charsetencoder-reset-method-in-Java-with-examples/ reset() method is a built-in method of Java . nio . charset . charset encoder, which resets the encoder if there is internal state , all internal states are cleared. It also resets character set-independent state and calls the implReset method to perform any character set-specific reset operations. Syntax : public final CharsetEncoder reset() Parameters: This function does not accept any parameters. Return value: This function resets the specific encoder. The following is the implementation of the above functions: Program 1: // Java program to implement// the above functionimport java.nio.CharBuffer;import java.nio.charset.Charset;import java.nio.charset.CharsetEncoder;public class Main { public static void main(String[] args) throws Exception { // Gets the encoder CharsetEncoder encoder = Charset.forName (“US-ASCII”)                                                    “p”>.newEncoder(); // It will reset the encoder // and clear all internal activities if there are encoder .reset(); // Prints the encoder after resetting it System.out.println(encoder); }} Output: sun.nio.cs.US_ASCII$Encoder@ 232204a1 Procedure 2: // Java program to implement// the above functionimport java.nio.CharBuffer;import java.nio.charset.Charset;import java.nio.charset.CharsetEncoder; public class Main { public static void main(String[] args) throws Exception { // Gets the encoder CharsetEncoder encoder = Charset.forName(“UTF8”) .newEncoder();  // It will reset the encoder // and clear all internal activities if there are encoder.reset(); // Prints the encoder after resetting it  System.out.println(encoder); }} Output: sun.nio.cs.UTF_8$Encoder@232204a1 Reference:https://docs.Oracle.com/javase/10/docs/API/Java/nio/charset/charsetencoder. html # reset()

DelayQueuetoArray() method in Java, example

DelayQueue toArray() method in Java, examples Original text: https://www . geeksforgeeks . org/delay queue-to array-method-in-Java-with-examples/ The toArray() method of the deferred queue is used to return an array containing all elements in the deferred queue. There is no specific order of elements in the array. Syntax: public Object[] toArray () orpublic T[ ] toArray (T[] a) Parameters:This method either does not accept parameters, or takes an arrayT[]a as a parameter. If it is large enough, this array is the array to store the list elements; otherwise, it will Allocate a new array of the same runtime type for this purpose. Return value: This function returns an array containing all the elements in the list. Exception:The first overload of this method does not throw an exception. However, the second overload throws the following exception: arraystorexception:If the runtime type of the specified array is not the runtime type of each element in the queue The supertype of the time type. NullPointRexception:If the specified array is empty. The following program illustrates the DelayQueue.toArray() method:Program 1: Java Language (1 A computer language, especially used for creating websites) // Java Program Demonstrate DelayQueue toArray() methodimport java.util.concurrent.*; import java.util.*;// The DelayObject for DelayQueue// It must implement…

CalendargetAvailableLocales() method in Java, example

Calendar getAvailableLocales() method in Java, examples Original text: https://www . geeksforgeeks . org/calendar-getavailable locales-method-in-Java-with-examples/ The clear() method in the Calendar class is used to return an array of all locales available in the localized instance of the getInstance method of the class. Syntax: public static Locale[] getAvailableLocales() Parameters:This method does not take any parameters. Return Value:This method returns an array of all locales available to the localized instance. The following program illustrates the work of the getAvailableLocales() method of the calendar class:Example 1: // Java Code to illustrate// getAvailableLocales() Method import java.util.*;public class Calendar_Demo_Locale { public static void main(String args[]) { // Creating a locale object Locale loc_arr[] = new Locale[5]; // Assigning locales to array loc_arr = Locale.getAvailableLocales(); // Displaying the results System.out.println(“The first ” + “five locales are:”); for (int cnt = 0; cnt < 5; cnt++)                                                          /span>.out. println( loc_arr[cnt]    .getISO3Country()); }} Output: The first five locales are:AREJOR SYRHRV Example 2: // Java Code to illustrate// getAvailableLocales() Methodimport java.util.*;public class Calendar_Demo_Locale { public static void main(String args[]) {  // Creating a locale object Locale loc_arr [] = new Locale[20] ; // Assigning locales to array loc_arr = Locale.getAvailableLocales(); // Displaying the results System.out .println(“The first ”                                                                                                                             “>+ ” locales are:”); for (int cnt = 0; cnt < 20; cnt++)           System.out.println ( loc_arr[cnt] .getISO3Country()) ; }} Output: The first twenty availables locales…

Constructor getAnnotation() method in Java, example

Constructor getAnnotation() method in Java, examples Original text: https://www . geeksforgeeks . org/constructor-getannotation-method-in-Java-with-examples/ Constructor The getAnnotation() method of the class is used to obtain the constructor object annotation of the specified type. If such an annotation exists, then Is empty. The specified type is passed as a parameter. Syntax: public <T extends Annotation> T getAnnotation(Class<T > annotationClass) Parameters: This method accepts a single parameter Annotation class, which represents the class object corresponding to the annotation type. Return value: This method returns the annotation of this element of the specified annotation type (if it exists in the element, otherwise it is empty). Exception: If the given annotation class is null, this method throws Null Pointer Exception. The following program illustrates the getAnnotation() method:Program 1: // Java program to demonstrate// Constructor.getAnnotation() methodimport java.lang.annotation.Annotation;import span> java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.lang.reflect.AnnotatedType;import java.lang.reflect.Constructor;public class GFG { public static void main(String… args) throws NoSuchMethodException { // Create Constructor Object Constructor[] constructors = Demo.class.getConstructors() ; // Create annotation object Annotation annotation  = constructors[0]                                                       >(PathVar.class); if (annotation instanceof PathVar) { PathVar customAnnotation                                                             >= (PathVar)annotation;                                                            out.println(                                                                                                                            .Path()); } }}// Demo classclass Demo { public Demo(@PathVar(Path = “Demo/usr”)                                                                 span> str) {}}// PathVar interface@Target({ ElementType.TYPE_USE })@Retention(RetentionPolicy .RUNTIME)@interface PathVar { public String Path();} Output: Path: Demo/usr Program 2: // Java program to demonstrate// Constructor.getAnnotation() methodimport java.lang.annotation.Annotation;import…

Instant adjust() method in Java, example

Instant adjustment () method in Java, example Original text: https://www . geesforgeks . org/instant-adjust to-method-in-Java-with-example/ The Adjust(TenseTense) method of the Immediate class adjusts the passed tense object so that it has the instant in which the method is applied . Syntax: public Temporal adjustInto(Temporal temporal) Parameters:This method accepts A parametertime that is the target time object to be adjusted. It should not be empty. Return value:This method returns the adjusted tense object. Exception:This method throws the following exception: datetimeexception If this method cannot be adjusted . Arithmetic exception If there is a numerical overflow during adjustment. The following program illustrates Instant. adjustInto() method: Program 1: // Java program to demonstrate// Instant.adjustInto() methodimport java.time.*;import java.time.temporal.Temporal;public class GFG { public static void main(String[] args)  { // create an instance object Instant instant = Instant.parse(“2018-11-20T16:55 :30.00Z”); // create a Temporal object // which is equal to OffsetDateTime object OffsetDateTime passTemporal = OffsetDateTime.now (); // print passed Value System.out.println(“Passed Value: ”                                                               span class=”n”>passTemporal); // apply adjustInto method // to adjust OffsetDateTime Temporal // to instant object Temporal returnTemporal = instant.adjustInto(passTemporal); // print results System.out.println(“Returned Value: ”                                                  “>(OffsetDateTime)returnTemporal); }} Output: Passed Value: 2018-11-22T09:22:17.297ZReturned Value: 2018-11-20T16:55:30Z Program 2: // Java program to demonstrate// Instant.adjustInto() methodimport java.time.*;import java.time.temporal.Temporal;public class GFG { public static void main(String[] args) { // create an instance object Instant instant = Instant.parse(“2018 -11-17T06:50:39.00Z”); // create a Temporal…

PrintWriterprintln(char[]) method in Java, example

PrintWriter println(char[]) method in Java, examples Original text: https://www . geesforgeks . org/print writer-printlnchar-method-in-Java-with-examples -2/ The println(char[]) method of the PrintWriter class in Java is used to print the specified character array on the stream. Then wrap. This character array is taken as a parameter. Syntax: public void println(char[] charArray) Parameters:This method accepts a mandatory parameter Character array, which is the character array to be printed in the stream. Return value:This method does not return a value. Exception: If the specified charArray() is null, this method returns Null Pointer Exception. The following method illustrates how the println(char[]) method works: Program 1: // Java program to demonstrate// PrintWriter println(char[]) method import java.io.*;class GFG { public static void main(String[] args) { try {  // Create a PrintWriter instance PrintWriter writer  = new PrintWriter(System.out); // Get the character array // to be printed in the stream char[] charArray = { ‘G’, ‘e’, ‘e’, ‘k’, ‘s’,                                                                                                    “sc”>’o’, ‘r’, span>                                                                                                 class=”p”>, ‘e’, ‘k’ , ‘s’ };                     span class=”c1″>// print the charArray    // to this writer using println() method    // This will put the charArray in the stream                                                                                >writer.println(charArray); writer.flush(); } catch (Exception e ) {             System .out.println(e); }  }} Output : GeeksForGeeks Program 2: // Java program to demonstrate// PrintWriter println(char[]) methodimport java.io.*;class GFG { public static void main(String[] args) { try…

Decimal style equals() method in Java, example

Decimal style equals () method in Java, example Original text: https://www . geesforgeks . org/decimal style-equals-method-in-Java-with-example/ The equals() method of the Java . time . format . DecimalStyle class in java is used to check whether this DecimalStyle matches the specified decimal style are equal. This method takes a decimal style instance, compares it to that decimal style, and returns a Boolean value representing the same. Syntax: public boolean equals(Object obj) Parameters:This method accepts A parameter Object that is the decimal style to check for equality with. Return Value: This method returns a Boolean that tells whether this decimal style is equal to the specified object. Exceptions:This method does not throw any exceptions. Program: // Java program to demonstrate// the above methodimport java.time.format .*;import java.util.*;public class DecimalStyleDemo { public static void main(String[] args) { DecimalStyle ds1                                                             span> DecimalStyle.STANDARD;  DecimalStyle ds2 = DecimalStyle.of(                       new Locale(“ENGLISH”)); DecimalStyle ds3 = null;  System.out.println(“Comparing DS 1 and DS 2: ”                                                         “o”>+ ds1.equals(ds2));  System.out.println(“Comparing DS 2 and DS 3: ”                                                               “>+ ds2.equals(ds3)); System.out. println(“Comparing DS 1 and DS 3: ”                                                                                + ds1.equals(ds3)); }} Output: Comparing DS 1 and DS 2: trueComparing DS 2 and DS 3 : falseComparing DS 1 and DS 3: false Reference:https://docs . Oracle . com/javase/10/docs/API/Java/time/format/decimal style . html # equals(Java…

ConcurrentModificationException in Java, example

ConcurrentModificationException in Java, examples Original text: https://www . geeksforgeeks . org/concurrentmodificationexception-in-Java-with-examples/ Exceptions in concurrently modified multi-threaded environments In multi-threaded environments, if during resource detection, any If the method finds that the object has concurrent modifications that are not allowed, the concurrent modification exception may be thrown. If this exception is detected, the result of iteration is undefined. Generally speaking, some iterator implementations choose to throw this exception, which is called Fail fast iterator . For example: If we try to modify any collection in the code using one thread, but another If a thread is already using the collection, this is not allowed. Concurrently modify exceptions in a single-threaded environment Because there is no guarantee that every time this exception is raised, an object has been Modified concurrently by a different thread, this exception will also be thrown in a single-threaded environment. If we call a series of methods on an object that violates the contract, the object will throw ConcurrentModificationException. For example: If while iterating over a collection we directly try to modify the collection, the given fail-fast iterator will throw this ConcurrentModificationException. Example: In the following code, an ArrayList is implemented. Then add few values ​​to it and…

TreeSettoArray(T[]) method in Java, example

TreeSet toArray(T[]) method in Java, example Original text: https://www . geesforgeks . org/treeset-to arrayt-method-in-Java-with-example / The toArray(T[]) method of the TreeSet class in Java is used to form an array of the same elements as the TreeSet. It returns an array containing all the elements in the tree set in correct order;The runtime type of the returned array is the runtime type of the specified array. If the TreeSet fits into the specified array, it will be returned in it. Otherwise, a new array is allocated using the runtime type of the specified array and the size of this TreeSet. If the TreeSet fits into the specified array with free space (that is, there are more elements in the array than the TreeSet), the elements in the array immediately following the end of the TreeSet are set to empty. (This is only useful in determining the length of a tree collection if the caller knows that the tree collection does not contain any empty elements.) Syntax: public <T> T[] toArray(T[] a) Parameters:This method accepts one parameterarr [], if the elements of the tree collection are large enough, this argument is the array to be stored into; otherwise, a new array…

Byte buffer is direct() method in Java, example

Bytebuffer is direct() method in Java, example Original text: https://www . geeksforgeeks . org/bytebuffer-is direct-methods-in-Java-with -examples/ java.nio.ByteBuffer class‘s isDirect() method is used to determine whether this byte buffer is direct of. Syntax: public abstract boolean isDirect() Return value:This method returns true if and only if the buffer is direct. The following is an example of the direct() method: Example 1: // Java program to demonstrate// isDirect() methodimport java.nio.*;import java.util.*;public class GFG { public static void main (String[] args) { // defining and allocating ByteBuffer // using allocate() method ByteBuffer byteBuffer  = ByteBuffer.allocateDirect(4);                                                                                                                                 “>boolean val = byteBuffer.isDirect(); // checking the condition if (val) System.out .println(“buffer is direct”); else System.out.println(“buffer is not direct”); }} Output: buffer is direct Example 2: // Java program to demonstrate// isDirect() methodimport java.nio.*;import java.util.*;public class GFG { public static void main(String[] args) { // defining and allocating ByteBuffer                                                                                                        = ByteBuffer.allocate(4);  // check the byteBuffer // using isDirect() method boolean val = byteBuffer. span>isDirect(); // checking the condition  if (val)     System .out.println(“buffer is direct”); else  System.out.println(“buffer is not direct”); }} Output: buffer is not direct Reference:https://docs . Oracle . com/javase/9/docs/API/Java/nio/bytebuffer . html # isDirect–

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索