Found 9150 Articles for Object Oriented Programming

Can we define multiple methods in a class with the same name in Java?

Vivek Verma
Updated on 14-May-2025 13:11:32

8K+ Views

Yes, we can define multiple methods in a class with the same name. But if we have two methods with the same name, the compiler should be able to differentiate between the two methods. Therefore, in Java, "we can define multiple methods" with the same name in a single class, as long as each method has a different set of parameters. When we invoke a method, the compiler executes the respective body (code) based on the arguments passed.This concept is known as method overloading. Method Overloading in Java In Java, method overloading is a type of compile-time polymorphism. Polymorphism is one ... Read More

First element and last element in a JavaScript array?

vineeth.mariserla
Updated on 20-Aug-2019 08:19:15

768 Views

An array is a group of elements. Each element has its own index value. We can access any element using these indexes. But in the case of the last element, we don't know the index until we know the number of elements present in the array. In this case, we have to use logic. Let's discuss these details in a nutshell.Accessing the first elementSince we know the index of the first element we can get the value of that element very easily. Let the array be arr. then the value of the first element is arr[0].ExampleIn the following example, there are ... Read More

How to select different cells of a JTable programmatically in Java?

raja
Updated on 12-Feb-2020 06:17:43

1K+ Views

A JTable is a subclass of JComponent class and it can be used to create a table with information displayed in multiple rows and columns. When a value is selected from a JTable, a TableModelEvent is generated, which is handled by implementing a TableModelListener interface.In general, a user can select the rows and columns manually in a JTable, we can also select different cells of a JTable programmatically using setRowSelectionInterval() and setColumnSelectionInterval() methods of JTable class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTableCellSelectionTest extends JFrame {    private JTable table;    public JTableCellSelectionTest() {       setTitle("JTableCellSelection Test");       Object[][] data = ... Read More

Get the first and last item in an array using JavaScript?

Ayush Gupta
Updated on 16-Sep-2019 07:32:55

318 Views

Javascript arrays are 0-indexed. This means that the first element is at the 0th position. The last element is at the length-of-array - 1th position. So we can access these elements using −Examplearr[0] // First element arr[arr.length - 1] // last element For example, let arr = [1, 'test', {}, 'hello'] console.log(arr[0]) console.log(arr[arr.length - 1])Output1 hello

How can we sort a string without using predefined methods in Java?

Vivek Verma
Updated on 14-May-2025 12:39:37

7K+ Views

The java.lang.String class represents an immutable sequence of characters and cannot be changed once created. We need to instantiate this class or assign values directly to its literal to create a string in Java. The String class does not provide any built-in method to sort the contents of a string. To sort a String, we need to convert it into a character array using the toCharArray() method and sort the array. To sort a character array, we can either use the Arrays.sort() method or use sorting algorithms.  Since the given task is to sort a string without using any predefined methods, we ... Read More

What is the use of Object Cloning in Java?

Vivek Verma
Updated on 27-May-2025 15:54:32

2K+ Views

The article will explain the use of object cloning in Java and provide an appropriate example to clarify the problem. What is the Object Cloning in Java? In Java, object cloning is a way to create an exact copy of an object. We can use the clone() method provided by the Object class to create a clone of an object. When an object is cloned, a new instance of the same class is created, and the fields of the original object are copied to the new object. The Cloneable interface must be implemented by a class whose object is ... Read More

When to call the Thread.run() instead of Thread.start() in Java?

Vivek Verma
Updated on 14-May-2025 12:15:29

2K+ Views

This article will briefly discuss the run() and start() methods, and also explain when to call run() instead of the start() method. Calling run() Method Instead of start() Usually, to execute a thread, we will call the start() method, and the start method calls run() implicitly, and the contents of the run() method will be executed as a separate thread. We can also call the run() method explicitly instead of the start() method. If we do so, we will be executing the contents of the run method in the current thread but not in a separate one. In this case, the ... Read More

How to detect the value change of a JSlider in Java?

raja
Updated on 12-Feb-2020 05:32:30

1K+ Views

A JSlider is a subclass of JComponent class and it is similar to scroll bar which allows the user to select a numeric value from a specified range of integer values. A JSlider has a knob which can slide on a range of values and can be used to select a particular value. and it can generate a ChangeListener interface.We can detect the value changed when the slider is moved horizontally using the Graphics2D class and override paint() method.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class ValueChangeJSliderTest extends JFrame {    private JSlider slider;    public ValueChangeJSliderTest() { ... Read More

How to read the data from a CSV file in Java?

raja
Updated on 01-Jul-2020 12:10:16

24K+ Views

A CSV stands for Comma Separated Values. In a CSV file, each line contains words that are separated with a comma(, ) and it is stored with a .csv extension.We can read a CSV file line by line using the readLine() method of BufferedReader class. Split each line on comma character to get the words of the line into an array. Now we can easily print the contents of the array by iterating over it or by using an appropriate index.CSV FileExampleimport java.io.*; public class CSVReaderTest {    public static final String delimiter = ", ";    public static void read(String csvFile) {   ... Read More

How can we print all the capital letters of a given string in Java?

Vivek Verma
Updated on 14-May-2025 11:57:53

6K+ Views

The Character class is a subclass of the Object class, and it wraps a value of the primitive type char in an object. An object of type Character class contains a single field whose type is char. We can print all the capital letters of a given string using the following approaches - Using isUpperCase() Method Using ASCII Comparison Using isUpperCase() Method The isUpperCase() is the method of the Character class. It accepts a character as a parameter and returns true if it is an uppercase letter and false if not.To ... Read More

Advertisements