The this keyword in Java is mainly used to refer to the current instance variable of the class. It can also be used to implicitly invoke the method or to invoke the constructor of the current class. A program that demonstrates the this keyword in Java is given as follows: Live Demo , , Now let us understand the above program. The Student class is created with data members rno, name. T.....
A method that accepts a variable number of Object arguments in Java is a form of variable length arguments(Varargs). These can have zero or multiple Object type arguments. A program that demonstrates this is given as follows: Live Demo , , Now let us understand the above program. The method Varargs() in class Demo has variable-length arguments of type Object. This method prints the nu.....
Method overloading in a class contains multiple methods with the same name but the parameter list of the methods should not be the same. One of these methods can have a long parameter in their parameter list. A program that demonstrates this is given as follows: Live Demo , , Now let us understand the above program. The PrintValues class is created with two methods print() in the impl.....
A copy constructor can be used to duplicate an object in Java. The copy constructor takes a single parameter i.e. the object of the same class that is to be copied. However, the copy constructor can only be explicitly created by the programmer as there is no default copy constructor provided by Java. A program that demonstrates this is given as follows: Live Demo , , Now let us unders.....
A variable cannot be modified after it is declared as final. In other words, a final variable is constant. So, a final variable must be initialized and an error occurs if there is any attempt to change the value. A program that demonstrates a final variable in Java is given as follows: Live Demo , , Now let us understand the above program. In the main() method in class Demo, the final.....
The Matcher.find(int) method finds the subsequence in an input sequence after the subsequence number that is specified as a parameter. This method is available in the Matcher class that is available in the java.util.regex package. The Matcher.find(int) method has one parameter i.e. the subsequence number after which the subsequence is obtained and it returns true is the required subsequence.....
A thread can be created by implementing the Runnable interface and overriding the run() method. The current thread is the currently executing thread object in Java. The method currentThread() of the Thread class can be used to obtain the current thread. This method requires no parameters. A program that demonstrates this is given as follows: Live Demo , The output of the above program.....
A list can be sorted in reverse order i.e. descending order using the java.util.Collections.sort() method. This method requires two parameters i.e. the list to be sorted and the Collections.reverseOrder() that reverses the order of an element collection using a Comparator. The ClassCastException is thrown by the Collections.sort() method if there are mutually incomparable elements in the li.....
A Vector can be cloned using the java.util.Vector.clone() method. This method does not take any parameters but returns a clone of the specified Vector instance as an object. A program that demonstrates this is given as follows: Live Demo , The output of the above program is as follows: , Now let us understand the above program. The Vector vec1 is created. Then Vector.add() is used to .....
The Vector elements can be traversed using the Enumeration interface. The method hasMoreElements( ) returns true if there are more elements to be enumerated and false if there are no more elements to be enumerated. The method nextElement( ) returns the next object in the enumeration. A program that demonstrates this is given as follows: Live Demo , The output of the above program is a.....
Sign up now to get your own personalized profile!
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more