Programming Articles

Page 2379 of 2547

Which method must be implemented by all threads in Java?

Monica Mona
Monica Mona
Updated on 25-Feb-2020 1K+ Views

While creating a thread class we must override the run() method of the Thread class. This method provides an entry point for the thread and you will put your complete business logic inside this method.Exampleclass ThreadDemo extends Thread { private String threadName; ThreadDemo( String name) { threadName = name; System.out.println("Creating " + threadName ); } public void run() { System.out.println("Running " ...

Read More

Where and how can I create a private constructor in Java?

Swarali Sree
Swarali Sree
Updated on 25-Feb-2020 411 Views

We can use a private contractor in a Java while creating a singleton class. The Singleton's purpose is to control object creation, limiting the number of objects to only one. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources, such as database connections or sockets.ExampleThe easiest implementation consists of a private constructor and a field to hold its result, and a static accessor method with a name like getInstance().The private field can be assigned from within a static initializer block ...

Read More

What does the method ensureCapacity(int, minCapacity) do in java?

Govinda Sai
Govinda Sai
Updated on 25-Feb-2020 210 Views

The ensureCapacity(int minCapacity) method of the class java.util.ArrayList increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.Exampleimport java.util.ArrayList; public class ArrayListDemo { public static void main(String args[]) { ArrayList arrlist = new ArrayList(5); arrlist.add(10); arrlist.add(50); arrlist.add(30); arrlist.ensureCapacity(15); for (Integer number : arrlist) { System.out.println("Number = " + number); } } }OutputNumber = 10 Number = 50 Number = 30

Read More

What does the method contains(obj o) do in java?

Nikitha N
Nikitha N
Updated on 25-Feb-2020 178 Views

The contains(Object) method of the java.util.ArrayList class returns true if this list contains the specified element.Exampleimport java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList arrlist = new ArrayList

Read More

How to create an unordered_set of user defined class or struct in C++?

Ayush Gupta
Ayush Gupta
Updated on 25-Feb-2020 893 Views

In this tutorial, we will be discussing a program to understand how to create an unordered set of user defined class or struct in C++.For this we will create a structure type and then compare two structure types with the function defined by the user to store the hash function.Example#include using namespace std; //defined structure struct Test {    int id;    bool operator==(const Test& t) const{       return (this->id == t.id);    } }; //defined class for hash function class MyHashFunction {    public:       size_t operator()(const Test& t) const{         ...

Read More

Initialization, declaration and assignment terms in Java

Arushi
Arushi
Updated on 25-Feb-2020 2K+ Views

A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.You must declare all variables before they can be used. Following is the basic form of a variable declaration − data type variable [ = value][, variable [ = value] ...] ;Here data type is one of Java's datatypes and variable is the name of the variable. To ...

Read More

Best book to learn Java programming for beginners

Daniol Thomas
Daniol Thomas
Updated on 25-Feb-2020 326 Views

Following books are on top from popularity and content wise and are a good resource to learn java programming from beginner to advance level.

Read More

Method overloading with autoboxing and widening in Java.

Arjun Thakur
Arjun Thakur
Updated on 25-Feb-2020 326 Views

Widening refers to passing a lower size data type like int to a higher size data type like long. Method overloading is possible in such case. Example public class Tester {     public static void main(String args[]) {         Tester tester = new Tester();         short c = 1, d = 2;         int e = 1, f = 2;         System.out.println(tester.add(c, d));         System.out.println(tester.add(e, f));     }     public int add(short a, short b) {         System.out.println("short");         return a + b;     }     public int add(int a, int b) {         System.out.println("int");         return a + b;     } } Output Short 3 Int 3

Read More

Which is the best book for Java interview preparation

Krantik Chavan
Krantik Chavan
Updated on 25-Feb-2020 219 Views

Following books are on top from popularity and content wise and are a good resource to learn java programming from beginner to advance level.

Read More

Pass by reference vs Pass by Value in java

Giri Raju
Giri Raju
Updated on 25-Feb-2020 7K+ Views

Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter. While Call by Reference means calling a method with a parameter as a reference. Through this, the argument reference is passed to the parameter. In call by value, the modification done to the parameter passed does not reflect in the caller's scope while in the call by reference, the modification done to the parameter passed are persistent and changes are reflected in the caller's scope. But Java uses only call by value. It creates a copy of references ...

Read More
Showing 23781–23790 of 25,466 articles
Advertisements