Java Archive

Difference between arithmetic + (Plus) operator and ++ (Increment) operator.

Difference between arithmetic + (a+1) operator and ++ (a++) operator. Here is some example to understand both operators. Source 1: package com; public class DiffBtwArthOperAndIncreOperator { public static void main(String args) { byte b = 2; b = b+1; System.out.println("Print a value "+b); } }   Response 1: Compile Time Error: Exception in thread "main"

Sum of the Even Numbers in an Array

Sum of the Even Numbers in an Array: Source: package com; public class EvenNumberSum { public static void main(String args) { int intArr = { 4, 7, 1, 2, 8, 9, 56, 12, 1, 34, 2, 56 }; int sum = 0; for (int i = 0; i <= intArr.length-1; i++) { if (intArr[i] %

Example Java Program for Odd and Even Numbers

Please see below java program to display Odd and even numbers between two numbers. Source: package com; public class OddNumbers { public static void main(String args) { int i; System.out.println( "######################### Odd numbers between any two numbers (In following example two numbers are 1 and 100###############)"); for (i = 1; i <= 100; i++) {

How to sort array by single for loop in java

Please see below example to sort number types of arrays by using single for loop. The code should be self-explanatory. Example Code: import java.util.Arrays; public class SortArray { public static void main(String args) { int intArray = new int { 67, 6, 7,88, 1, 6,7,3, 2, 4, 5, 11, 22 }; System.out.println(“**************int Array before sorting************”); for

How to Sort an Array in java

Please see below example to sort different types of arrays in java. The code should be self-explanatory. Example Code: import java.util.Arrays; import java.util.Collections; public class SortArray { public static void main(String args) { int intArray = new int { 67, 43, 88, 1, 3, 2, 4, 5, 11, 22 }; String strArray = new String

How can we compare String creation performance?

In this article we will discuss performance wise what is the best way to create String object. There are two ways to create String object: By string literal String str = “10Article”; When we create string literal then JVM checks Heap string constant pool first. If the string already exists in the pool, a reference

How to reverse each word of string?

How to reverse each word of string. Problem Statement: I have a string with multiple words. I want to reverse each word of this string. Reverse each word of string. Source Code: package com; public class Test { public static void main(String args) { String str1 = "Java and J2EE Technology Servlet"; String str2 =

How to count the occurrences of a Sub String in another String?

How to count the occurrences of a Sub String in the given String > String search Problem Statements: I have a long String, I want to find out the occurrence of sub string in this. How to find out a duplicate sub string in a long String. I want to print multiple occurrence count of

How to get the nth index of the given token occurring in the given string > String search

How to get the nth index of the given token occurring in the given string > String search Problem Statements: I have a long String with multiple lines, I want to print last 2 lines from this String. I want to print second last line from my String. I want to show only three lines

JAX-WS Tutorial

JAX-WS Tutorial: As we know there is two types of web services. SOAP Restful   Here I am talking about SOAP based web services. Java provides a set of APIs to develop web services in XML format is called JAX-WS. JAX-WS provides many annotation to simplify the development and deployment for both web service clients

Java Singleton Design pattern Question & Answer

Java Singleton Design pattern Question & Answer. Q1. What is the Significance of Classloader in Singleton Pattern? Ans1. If singleton class is loaded by two classloaders, two instance of singleton class will be created, one for each classloader. Q2. What is the significance of Serialization in Singleton Pattern? Ans2.  If a singleton class is serializable,

Factory Design Pattern

Factory Design Pattern: This is most used design pattern in Java. Factory pattern also called as Factory method design pattern. Factory design pattern comes under creational pattern. This pattern provides one of the best ways to create an object. By this pattern we create object without exposing creation logic of these objects to the users.

Singleton design pattern

Singleton design pattern: This is the simplest and most useful design pattern in java. Singleton design comes under creational pattern as this pattern provides one of the best ways to create an object. This pattern involves a single class which is responsible to create an object while making sure that only single object gets created.

Behavior Design Patterns

Behavior Design Patterns: Behavioral pattern explain interaction between the objects. Means objects look like they are talking with each other without hard coding .Objects are loosely coupled. Behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. Behavioral patterns increase flexibility in carrying out this communication. As we

Structural Design Patterns

Structural Design Patterns: These types of patterns describe how objects and classes can be combined and create big structure. Than question arise what is the difference between these class patterns and object patterns? Class patterns explain abstraction with the help of inheritance and how it can be used to provide more useful program interface. Object

Creational Design Patterns

Creational Design Patterns: The entire creational design patterns give us best way to create objects. In simple way by these patterns we can learn best possible way in which an object can be instantiated. We all know how we can create objects in java. Test obj = new Test(); Now you can ask me so

Design Patterns in Java

Design Patterns in Java: How Many types of design pattern in java? Design patterns are most popular topic of software development. Design patterns are approaches or well-described solution for a common software problem. By using of design patterns results in increased. Types of design patterns include creational, structural, and behavioral design patterns. There are 23

How to check key exist in HashMap:

How to check key exist in HashMap: We can use the Map.containsKey() method to check if a key exists in a Map or not. package packagename; import java.util.HashMap; import java.util.Map; public class CheckHashMap { public static void main(String args) {               Map<String, Integer> testHashMap = new HashMap();               testHashMap.put(“V1”, 1);               testHashMap.put(“V2”, 2);               testHashMap.put(“V3”,

Compare two String values by equals() and equalsIgnoreCase()

Compare two String values. There are two methods those help up to compare two strings for equality. boolean equals(Object anObject) compares this string to the specified object. boolean equalsIgnoreCase(String anotherString) compares this String to another String, ignoring case considerations. Results: Value Meaning true Both String are equals false Not equal. Example equals : package packagename;

Java – String compareTo() && compareToIgnoreCase() Method example

The method compareTo() use compares two string lexicographically. compareToIgnoreCase() is also similar to compareTo() method only difference is that compareToIgnoreCase() ignores the case uppercase and lowercase while comparing. Both methods also compares the strings based on the Unicode value of their each character. It returns 0 when the strings are equal else it returns positive