Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Rishi Raj
Page 4 of 8
Why do we use import statement in Java? Where it should be included in the class?
The import statement can be used to import an entire package or sometimes import certain classes and interfaces inside the package. The import statement is written before the class definition and after the package statement(if there is any). Also, the import statement is optional.A program that demonstrates this in Java is given as follows:Exampleimport java.util.LinkedList; public class Demo { public static void main(String[] args) { LinkedList l = new LinkedList(); l.add("Apple"); l.add("Mango"); l.add("Cherry"); l.add("Orange"); l.add("Pear"); System.out.println("The LinkedList ...
Read MoreReplace all the elements of a Vector with Collections.fill() in Java
All the elements of a vector can be replaced by a specific element using java.util.Collections.fill() method. This method requires two parameters i.e. the Vector and the element that replaces all the elements in the Vector. No value is returned by the Collections.fill() method.A program that demonstrates this is given as follows:Exampleimport java.util.Collections; import java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(5); vec.add(7); vec.add(4); vec.add(1); ...
Read MoreEnumerate through the Vector elements in Java
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 −Exampleimport java.util.Enumeration; import java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(); vec.add(7); vec.add(3); vec.add(5); vec.add(9); vec.add(2); ...
Read MoreWhy Abstract Class is used in Java?
A class is an abstract class if it contains at least one abstract method. It can contain other non-abstract methods as well. A class can be declared as abstract by using the abstract keyword. Also, an abstract class cannot be instantiated.A program that demonstrates an abstract class in Java is given as follows:Exampleabstract class Animal { abstract void sound(); } class Cat extends Animal { void sound() { System.out.println("Cat Meows"); } } class Dog extends Animal { void sound() { System.out.println("Dog Barks"); } } class Cow extends Animal { ...
Read MoreHow to prevent Cloning to break a Singleton Class Pattern in Java?
A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using cloning, we can still create multiple instance of a class. See the example below − Example - Breaking Singleton public class Tester{ public static void main(String[] args) throws CloneNotSupportedException { A a = A.getInstance(); ...
Read MorePrivate Constructors and Singleton Classes in Java Programming
As we know the primary role of the constructor is to instantiate a class object now if we made the constructor as private then we restrict its calling to be only in defining a class and not in some other class. Now the singleton class in Java is defined as the class which restricts the instantiation of a class and ensure that only one instance of the class exists in the JVM. After first time if instantiate the singleton class the new variable also points to the first instance created. In order to create a singleton class we could use ...
Read MoreWhat does synchronized modifier do in Java?
The synchronized keyword used to indicate that a method can be accessed by only one thread at a time. The synchronized modifier can be applied with any of the four access level modifiers. Example public class TestThread { public static Object Lock1 = new Object(); public static Object Lock2 = new Object(); public static void main(String args[]) { ThreadDemo1 T1 = new ThreadDemo1(); ThreadDemo2 T2 = new ThreadDemo2(); ...
Read MoreJava ResultSetMetaData getPercision() method with example
In this article, we will learn how to retrieve the size of a specified column in a ResultSet object using the getPrecision() method of the ResultSetMetaData interface in JDBC. What does getPrecision() in ResultSetMetaData do? The getPercision() method of the ResultSetMetaData (interface) retrieves the size of the specified column in the current ResultSet object. This method accepts an integer value representing the index of a column and, returns an integer value representing the size of the given column. Steps to get the ResultSetMetaData object To get the ResultSetMetaData object, you need to − Step 1. Register ...
Read MoreHow to create immutable class in Java?
An immutable class object's properties cannot be modified after initialization. The state of an immutable object remains constant throughout its lifetime. To achieve immutability, the class is designed such that its fields are initialized only once through a constructor, and no methods are provided to modify those fields. For example: String is an immutable class in Java. We can create an immutable class by following the given rules below − Make class final − class should be final so that it cannot be extended. ...
Read MoreJava DatabaseMetaData getDatabaseMinorVersion() method with example
In this article, we will learn how to use the getDatabaseMinorVersion() method of the DatabaseMetaData interface in Java to retrieve the minor version of the underlying database. This method helps check the version details of the connected database. What is DatabaseMetaData?The DatabaseMetaData is an interface that provides methods to retrieve metadata about a database, such as the database product name, version, driver name, the total number of tables, views, and more. To get the DatabaseMetaData object, use the getMetaData() method of the Connection interface.Syntax: public DatabaseMetaData getMetaData() throws SQLException; ...
Read More