publicenumDirection{EAST{@OverridepublicStringmessage(){return"You are moving in east. You will face sun in morning time.";}},WEST{@OverridepublicStringmessage(){return"You are moving in west. You will face sun in evening time.";}},NORTH{@OverridepublicStringmessage(){return"You are moving...
String.valueOf(Integer.MAX_VALUE));Assertions.assertEquals("-2147483648",String.valueOf(Integer.MAX_VALUE+1));Assertions.assertEquals("-2147483648",String.valueOf(Integer.MIN_VALUE));Assertions.assertEquals
public class Main { public static void main(String[] args) { int[] arr = new int[]{1,2,3,4,5}; int[] arr_new = new int[arr.length-1]; int j=3; for(int i=0, k=0;i<arr.length;i++){ if(arr[i]!=j){ arr_new[k]=arr[i]; k++; } } System.out.println("Before ...
publicsynchronizedvoidadd(intvalue){this.count+=value;} 2. Static methods,这个是类级别的方法同步。 publicstaticsynchronizedvoidadd(intvalue){count+=value;} 3. Code blocks inside instance methods,实例级别方法内部的代码块同步 publicvoidadd(intvalue){synchronized(this){this.count+=value;}} 4. Code...
Moreover, the java.lang.String class also has inbuilt regex support that we commonly use in our code. 3. Java Regex Package The java.util.regex package consists of three classes: Pattern, Matcher, and PatternSyntaxException: Pattern object is a compiled regex. The Pattern class provides no pu...
Java's inbuilt iterator methods next() and hasNext() - these are useful when you want to fetch data without manually handling pagination. User and Contact listing only works up to 10k records. To retrieve all records use the Scroll API via scroll() Error handling You do not need to deal...
Java providesComparableinterface which should be implemented by any custom class if we want to useArraysorCollectionssorting methods. The Comparable interface hascompareTo(T obj)method which is used by sorting methods, you can check any Wrapper, String or Date class to confirm this. We should ove...
Here is an example in Java that sorts an list of objects using the Comparable interface: importjava.util.List;importjava.util.ArrayList;importjava.util.Collections;classStudentimplementsComparable<Student>{introll_no;Stringname;Student(introll_no,Stringname){this.roll_no=roll_no;this.name=name;}...
Thread class provides various inbuilt methods such as getPriority(), isAlive and many more while the Runnable interface provides a single method, i.e., run(). Describe the purpose and working of sleep() method. The sleep() method in java is used to block a thread for a particular time,...
The mechanism is inbuilt in Java and the Java Garbage Collection methods take care of all the nuances of freeing the memory of unused objects. The GC mechanism works by keeping a track of all the live objects that are used by the program. The live object can be traversed through a GC ...