Found 7442 Articles for Java

Send email using Java Program

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

934 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

8K+ 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

2K+ 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

169 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

502 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

308 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

472 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 28-Jan-2025 11:39:52

1K+ Views

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 More

Determine if a String is a legal Java Identifier

karthikeya Boyini
Updated on 26-Jun-2020 12:20:41

880 Views

To determine if a String is a legal Java Identifier, use the Character.isJavaIdentifierPart() and Character.isJavaIdentifierStart() methods.Character.isJavaIdentifierPart()The java.lang.Character.isJavaIdentifierPart() determines if the character (Unicode code point) may be part of a Java identifier as other than the first character.A character may be part of a Java identifier if any of the following are true.it is a letterit is a currency symbol (such as '$')it is a connecting punctuation character (such as '_')it is a digitit is a numeric letter (such as a Roman numeral character)Character.isJavaIdentifierStart()The java.lang.Character.isJavaIdentifierStart() determines if the character (Unicode code point) is permissible as the first character in a Java ... Read More

Advertisements