Found 9326 Articles for Object Oriented Programming

What does the method addElement(E obj) do in java?

Rishi Raj
Updated on 26-Feb-2020 06:10:20

283 Views

The addElement(E obj) method is used to add the specified component to the end of this vector and increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity. This addElement() method is identical in functionality to the add(Object) method. The add() method returns true/false but the addElement() method does not return any value.Exampleimport java.util.Vector; public class VectorDemo {    public static void main(String[] args) {       Vector vec = new Vector(4);       vec.add(4);       vec.add(3);       vec.add(2);       vec.add(1);       System.out.println("Initial ... Read More

What is a switch case statement in Java and how to use it?

Johar Ali
Updated on 25-Feb-2020 09:34:41

316 Views

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.Syntaxswitch(expression) {    case value :       // Statements       break;    case value :       // Statements       break;    // You can have any number of case statements.    default :       // Statements }The following rules apply to a switch statement −The variable used in a switch statement can only be integers, convertible integers (byte, short, char), ... Read More

How to use ‘do while loop’ in Java?

Rahul Sharma
Updated on 25-Feb-2020 09:49:15

156 Views

A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.SyntaxFollowing is the syntax of a do...while loop −do {    // Statements }while(Boolean_expression);Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested.If the Boolean expression is true, the control jumps back up to do statement, and the statements in the loop execute again. This process repeats until the Boolean expression is false.Examplepublic class Test {    public static void main(String args[]) {   ... Read More

What is the difference between >> and >>> operators in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:21

1K+ Views

>> Binary Right ShiftThe left operand value is moved right by the number of bits specified by the right operand.>>> Shift right zero fillThe left operand value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.

What is dot operator in Java?

Debarpito Sarkar
Updated on 05-Sep-2022 10:27:17

5K+ Views

This article will help you understand what a dot operator in Java Programming language is. Before jumping into Dot Operator, let us revise about Operators. OPERATORS In computer programming we often need to perform some arithmetical or logical operations. In such circumstances, we need operators to perform these tasks. Thus, an Operator is basically a symbol or token, which performs arithmetical or logical operations and gives us meaningful result. The values involved in the operation are called Operands. Here is a basic Pictorial representation of Operators. Now, let us discuss the types of Operators available. TYPES OF OPERATORS There ... Read More

What does the method clone() do in java?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:20

117 Views

The clone() method of the java.util.ArrayList class returns a shallow copy of this ArrayList instance(i.e. the elements themselves are not copied). Example import java.util.ArrayList; public class ArrayListDemo { public static void main(String args[]) { ArrayList arrlist1 = new ArrayList(); arrlist1.add(new StringBuilder("Learning-")); ArrayList arrlist2 = (ArrayList) arrlist1.clone(); StringBuilder strbuilder = arrlist1.get(0); strbuilder.append("list1, list2-both pointing to the same StringBuilder"); ... Read More

What does the method clear() do in java?

Abhinaya
Updated on 20-Feb-2020 12:15:41

1K+ Views

The clear() method of the class java.util.ArrayList removes all of the elements from this list. The list will be empty after this call returns.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(20);       arrlist.add(30);       arrlist.add(10);       arrlist.add(50);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }       int retval = arrlist.size();       System.out.println("List consists of "+ retval +" elements"); ... Read More

What does the method remove(int) do in java?

Govinda Sai
Updated on 25-Feb-2020 10:19:36

67 Views

The remove(int index) method of the java.util.ArrayList class removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(20);       arrlist.add(15);       arrlist.add(30);       arrlist.add(45);       System.out.println("Size of list: " + arrlist.size());       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }       ... Read More

What does the method copyOfRange(int[] original, int from, int to) do in java?

Samual Sam
Updated on 13-Jun-2020 12:29:44

127 Views

The copyOfRange(int[] original, int from, int to) method of java.util.Arrays class copies the specified range of the specified array into a new array. The final index of the range (to), which must be greater than or equal to from, may be greater than original. Length, in which case 0 is placed in all elements of the copy whose index is greater than or equal to original. Length - from. The length of the returned array will be to - from.Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       int[] arr1 = new ... Read More

What does the method copyOf(int[] original, int newLength) do in java?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:21

167 Views

The copyOf (int[] original, int newLength) method of the java.util.Arrays class copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid within the copy however not the original, the copy can contain zero. Such indices will exist if and only if the specified length is greater than that of the original array. Example import java.util.Arrays; public class ArrayDemo { public ... Read More

Advertisements