Found 9326 Articles for Object Oriented Programming

Sorting collection of String and StringBuffer in Java

Fendadis John
Updated on 30-Jul-2019 22:30:23

586 Views

In order to sort in Java as we know we can use either Comparable or Comparator interfaces in which we could also define our custom logic to sort.One of the approach to sort is to add the entity in TreeSet or TreeMap which would sort the entries as internally they also uses the comparable interface. Now String class in Java internally implements comparable interface so whenever we add string to a tree set or map it uses comparable logic of string class and sort the input entry strings.But String buffer do not have the implementation of comparable interface so ... Read More

Send email using Java Program

Rishi Raj
Updated on 30-Jul-2019 22:30:23

461 Views

To send an e-mail using your Java Application is simple enough but to start with you should have JavaMail API and Java Activation Framework (JAF) installed on your machine. You can download latest version of JavaMail (Version 1.2) from Java's standard website. You can download latest version of JAF (Version 1.1.1) from Java's standard website. Download and unzip these files, in the newly created top level directories you will find a number of jar files for both the applications. You need to add mail.jar and activation.jar files in your CLASSPATH. Send a Simple E-mail Here is ... Read More

Private and final methods in Java Programming

Vikyath Ram
Updated on 27-Jun-2020 08:12:16

7K+ Views

In Java private methods are the methods having private access modifier and are restricted to be access in the defining class only and are not visible in their child class due to which are not eligible for overridden. However, we can define a method with the same name in the child class and could access in parent class.Like private methods final methods in Java are the methods having final non-access modifier instead of private and are again restricted to be accessed in the defining class only and are not visible in their child class due to which are not eligible ... Read More

Private Constructors and Singleton Classes in Java Programming

Rishi Raj
Updated on 30-Jul-2019 22:30:23

1K+ Views

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 More

PriorityQueue Class in Java Programming

Rishi Raj
Updated on 30-Jul-2019 22:30:23

80 Views

The java.util.PriorityQueue class is an unbounded priority queue based on a priority heap.Following are the important points about PriorityQueue − The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects. Class declaration Following is the declaration for java.util.PriorityQueue class − public class PriorityQueue extends AbstractQueue implements Serializable Parameters Following ... Read More

Print a 2 D Array or Matrix in Java Programming

Arushi
Updated on 30-Jul-2019 22:30:23

1K+ Views

In this post we will try to print an array or matrix of numbers at console in same manner as we generally write on paper. For this the logic is to access each element of array one by one and make them print separated by a space and when row get to emd in matrix then we will also change the row Example Live Demo public class Print2DArray { public static void main(String[] args) { final int[][] matrix = { ... Read More

Math class methods in Java Programming

Arushi
Updated on 30-Jul-2019 22:30:23

355 Views

The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Class Declaration Following is the declaration for java.lang.Math class − public final class Math extends Object Field Following are the fields for java.lang.Math class − static double E − This is the double value that is closer than any other to e, the base of the natural logarithms. static double PI − This is the double value that is closer than any other to pi, the ratio of the circumference of a circle ... Read More

How to prevent Cloning to break a Singleton Class Pattern in Java?

Rishi Raj
Updated on 30-Jul-2019 22:30:23

235 Views

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 Live Demo public class Tester{ public static void main(String[] args) throws CloneNotSupportedException { A a = A.getInstance(); ... Read More

Functional Interfaces in Java Programming

Fendadis John
Updated on 30-Jul-2019 22:30:23

310 Views

Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method 'compareTo' is used for comparison purpose. Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions. Following is the list of functional interfaces defined in java.util.Function package. Given below is the list of interfaces in Java8. Sr.No. Interface & Description 1 BiConsumer Represents an operation that accepts two input arguments, and returns no result. 2 BiFunction Represents a function that accepts two arguments and produces a result. ... Read More

How to create immutable class in Java?

Rishi Raj
Updated on 30-Jul-2019 22:30:23

492 Views

An immutable class object's properties cannot be modified after initialization. For example String is an immutable class in Java. We can create a immutable class by following the given rules below − Make class final − class should be final so that it cannot be extended. Make each field final − Each field should be final so that they cannot be modified after initialization. Create getter method for each field. − Create a public getter method for each field. fields should be private. No setter method for each field. − Do not create a public setter method for any ... Read More

Advertisements