Found 9321 Articles for Object Oriented Programming

Swap two variables in one line in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:23

1K+ Views

In order to swap two variable using single expression or in single line we could use bitwise XOR operator of Java. As we now that in Java XOR functions as XOR of two numbers a and b returns a number which has all the bits as 1 wherever bits of a and b differs. So for swapping of two variable we would use this operator as Example Live Demo public class SwapUsingBitwise { public static void main(String[] args) { int a = 8 ; int b = 10; ... Read More

Swap two Strings without using third user defined variable in Java

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

211 Views

In order to swap two strings i.e interchange the content of two strings we will use sub string method of string class in Java.First of all get the length of both strings before making any change in any of string.Now modify one string as concatenate both strings and assign to one string. After this use sub string method of String class using begin index as length of new modified string 1 and last index as initial length of string 1.This will give us the swiped string 1 which contain content of string 2. Now to get swiped string 2 again ... Read More

Streams in Java

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

1K+ Views

Stream is a new abstract layer introduced in Java 8. Using stream, you can process data in a declarative way similar to SQL statements. For example, consider the following SQL statement. SELECT max(salary), employee_id, employee_name FROM Employee The above SQL expression automatically returns the maximum salaried employee's details, without doing any computation on the developer's end. Using collections framework in Java, a developer has to use loops and make repeated checks. Another concern is efficiency; as multi-core processors are available at ease, a Java developer has to write parallel code processing that can be pretty error-prone. To resolve ... Read More

Static methods vs Instance methods in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:23

2K+ Views

In Java as we know that the behavior of any variable/method is defined by the keyword that is used in front of its declaration name. So one of the non-access modifiers is Static which can be used along with method as well as with variable. Static methods as name states defined at the class level and could be accessed on the class name i.e no need of class object creation in order to access/call the static methods. While on another hand if we do not uses the static keyword with variable/method than it belongs or categorized as instance method which ... Read More

Stack in Java Programming

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

188 Views

Stack is a subclass of Vector that implements a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own. Stack( ) Apart from the methods inherited from its parent class Vector, Stack defines the following methods − Sr.No. Method & Description 1 boolean empty() Tests if this stack is empty. Returns true if the stack is empty, and returns false if the stack contains elements. 2 Object peek( ) Returns the element ... Read More

Sorting a HashMap according to values in Java

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

520 Views

As we know that Hash map in Java does not maintain insertion order either by key or by order.Also it does not maintain any other order while adding entries to it. Now in order to sort a hash map according to the values mapped to its corresponding keys we first need to get all values of map considering that hash map has unique values only.Now put all the values in a list and sort this list with the comparator or comparable interface of Java. As we get sorted list of unique values now get corresponding keys from the map and ... Read More

Sorting collection of String and StringBuffer in Java

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

603 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

466 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

Advertisements