Bubble sort algorithm in java with example

  Java Program to sort array using bubble sort

  1. package com.instaceofjava; 

  2. public class BubbleSortExample{
  3.   
  4. int[] array={4,2,5,6,9,1};
  5.  
  6. int n = array.length;
  7. int k;
  8.  
  9. for (int m = n; m>= 0; m--) {
  10.  
  11. for (int j = 0; j < n-1; j++) {
  12.  
  13.    k = j + 1;
  14.  
  15.  if (array[j] > array[k]) {
  16.  
  17.        int temp;
  18.        temp = array[j];
  19.        array[j] = array[k];
  20.        array[k] = temp;
  21.  
  22. }
  23. }
  24.            
  25. for (int x = 0; x < array.length; x++) {
  26.  
  27. System.out.print(array[x] + ", ");
  28.  
  29.  }
  30.  
  31. System.out.println();
  32. }  
  33.  
  34. }
  35.  
  36. }



Output:

  1. 2, 4, 5, 6, 1, 9, 
  2. 2, 4, 5, 1, 6, 9, 
  3. 2, 4, 1, 5, 6, 9, 
  4. 2, 1, 4, 5, 6, 9, 
  5. 1, 2, 4, 5, 6, 9, 
  6. 1, 2, 4, 5, 6, 9, 
  7. 1, 2, 4, 5, 6, 9,



AtomicInteger in Java


  • Java.util.concurrent.atomic package provides very useful classes that support lock free and thread safe programming.
  • The main use of this class is an int value that may be updated automatically.
  • AtomicInteger has some useful methods. Before that lets see the some points about this class.
  • Commonly we will use this AtomicInteger to handle the counter that is accessible by different threads simultaneously.

Java.util.concurrent.atomic.AtomicInteger:

  1. public class AtomicInteger
  2. extends Number
  3. implements Serializable

AtomicInteger Class Constructors:


  • public AtomicInteger(): Creates a new AtomicInteger object with default value 0.
  1. AtomicInteger atomicInteger = new AtomicInteger();
  • public AtomicInteger(int initialValue): Creates a new AtomicInteger object with given initial  value.
  1. AtomicInteger atomicInteger = new AtomicInteger(10);


 AtomicInteger Class Methods:


 1.public final void set(int newValue):
  •  Sets given value to the object.

  Java Program to create AtomicInteger class object and sets some value.

  1. package com.instaceofjava;
  2.  
  3. import java.util.concurrent.atomic.AtomicInteger;
  4.  
  5. public class AtomicIntegerExample{
  6.  
  7. public static void main(String[] args) {
  8.  
  9. AtomicInteger atomicInteger = new AtomicInteger();
  10.  
  11.   System.out.println(atomicInteger);
  12.   atomicInteger.set(10);
  13.   System.out.println(atomicInteger);
  14.  
  15. }
  16.  
  17. }

Output:

  1. 0
  2. 10

2, public final void get():

  • Used to get current value.

Java Program to create AtomicInteger class object and sets some value and get.

  1. package com.instaceofjava;
  2.  
  3. import java.util.concurrent.atomic.AtomicInteger;
  4.  
  5. public class AtomicIntegerExample{
  6.  
  7. public static void main(String[] args) {
  8.  
  9. AtomicInteger atomicInteger = new AtomicInteger();
  10.  
  11.   System.out.println(atomicInteger.get());
  12.   atomicInteger.set(10);
  13.   System.out.println(atomicInteger.get());
  14.  
  15. }
  16.  
  17. }

Output:

  1. 0
  2. 10
3.public final int getAndSet(int newValue):
  • Automatically sets the given value and returns old value.

  Java Program which explains getAndSet(int x) method of AtomicInteger class

  1. package com.instaceofjava;
  2.  
  3. import java.util.concurrent.atomic.AtomicInteger;
  4.  
  5. public class AtomicIntegerExample{
  6.  
  7. public static void main(String[] args) {
  8.  
  9. AtomicInteger atomicInteger = new AtomicInteger();
  10.  
  11.    System.out.println(atomicInteger.get());
  12.  
  13.    atomicInteger.set(10);
  14.  
  15.    System.out.println(atomicInteger.get());
  16.  
  17.    System.out.println(atomicInteger.getAndSet(12));
  18.  
  19.    System.out.println(atomicInteger.get());
  20.  
  21. }
  22.  
  23. }

Output:

  1. 0
  2. 10
  3. 10
  4. 12



4.public final int incrementAndGet()
  • Automatically increments the value one and returns updated value

  Java Program which explains incrementAndGet() method of AtomicInteger class

  1. package com.instaceofjava;
  2.  
  3. import java.util.concurrent.atomic.AtomicInteger;
  4.  
  5. public class AtomicIntegerExample{
  6.  
  7. public static void main(String[] args) {
  8.  
  9. AtomicInteger atomicInteger = new AtomicInteger();
  10.  
  11.    System.out.println(atomicInteger.get());
  12.  
  13.    atomicInteger.set(10);
  14.  
  15.    System.out.println(atomicInteger.get());
  16.  
  17.    System.out.println(atomicInteger.incrementAndGet());
  18.  
  19.  
  20. }
  21.  
  22. }

Output:

  1. 0
  2. 10
  3. 11


How to create immutable class in java

  • In Java String and all wrapper classes are immutable classes.
  • So how to create custom immutable class in java?
  • Lets see how to make a class object immutable.

  Immutable class:

  • Make class final so that it should not be inherited.
  • All the variables should be private so should not be accessible outside of class. 
  • Make all variables final so that value can not be changed.
  • A constructor to assign values to variables in class.
  • Do not add any setter methods.

1. Java Program to create custom immutable class object in java.

  1. package com.instaceofjava;

  2.  
  3. public final class ImmutableClass{
  4.   
  5. private final int a;
  6. private final int b;
  7.  
  8. ImmutableClass( int x, int y){
  9.  
  10.  a=x;
  11.  b=y; 

  12.  
  13. public getA(){
  14.  
  15.  return a;

  16. }
  17.  
  18. public getB(){
  19.  
  20.  return b;

  21. }
  22.  
  23. public static void main(String[] args) {
  24.  
  25. ImmutableClass obj= new ImmutableClass(10,20);
  26.  
  27. System.out.println("a="+obj.getA());
  28.  
  29. System.out.println("b="+obj.getB());
  30.  
  31. }
  32. }
Output:

  1. a=10
  2. b=20


String class in java: Immutable


  1. public final class String
  2.         implements java.io.Serializable, Comparable<String>, CharSequence
  3. {  
  4.   
  5. //String class variables

  6.   private final char value[];
  7.   private final int offset;
  8.   private final int count;
  9.   private int hash; // Default to 0
  10.   private static final ObjectStreamField[] serialPersistentFields =
  11.       new ObjectStreamField[0];
  12.   
  13. //String class constructor
  14. public String(String original) {
  15.  
  16.             int size = original.count;
  17.              char[] originalValue = original.value;
  18.              char[] v;
  19.              if (originalValue.length > size) {
  20.                 // The array representing the String is bigger than the new
  21.               // String itself.  Perhaps this constructor is being called
  22.                 // in order to trim the baggage, so make a copy of the array.
  23.                  int off = original.offset;
  24.                 v = Arrays.copyOfRange(originalValue, off, off+size);
  25.             } else {
  26.                  // The array representing the String is the same
  27.                  // size as the String, so no point in making a copy.
  28.                  v = originalValue;
  29.            }
  30.             this.offset = 0;
  31.              this.count = size;
  32.             this.value = v;
  33.         } 

  34. }



Java Program to find Sum of Digits


1. Java Program to find sum of digits without using recursion.

  1. package com.instaceofjava;
  2. import java.util.Scanner;
  3.  
  4. public class SumOfDigits {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. int number;
  9. Scanner in = new Scanner(System.in);
  10.  
  11. System.out.println("Please Enter a number");
  12.  
  13. number=in.nextInt(); 
  14.  
  15. int sum=0 ;
  16.  
  17. while(number!=0){
  18.  
  19. sum=sum+(number%10);
  20. number=number/10;
  21. }
  22.  
  23. System.out.println("Sum of Digits ="+sum);
  24.  
  25. }
  26. }
Output:

  1. Please Enter a number
  2. 123
  3. Sum of Digits=6


2. Java Program to find sum of digits using recursion.

  1. package com.instaceofjava;
  2. import java.util.Scanner;
  3.  
  4. public class SumOfDigits {
  5.  
  6. int sum;

  7. public int CalRecSum(int n){
  8.  
  9. if(n==0){
  10. return sum;
  11. }
  12. else{
  13.  
  14.  sum+=n%10;
  15.  CalRecSum(n/10);
  16.  
  17.  
  18. return sum;
  19. }

  20. public static void main(String[] args) {
  21.  
  22. int number;
  23. Scanner in = new Scanner(System.in);
  24.  
  25. System.out.println("Please Enter a number");
  26.  
  27. number=in.nextInt(); 
  28.  
  29. SumOfDigits   ob= new SumOfDigits();
  30. System.out.println("Sum of Digits ="+ob.CalRecSum(number));
  31.  
  32. }
  33.  
  34. }


Output:

  1. Please Enter a number
  2. 326
  3. Sum of Digits=11

Swap two numbers without using third variable

1. Java Interview Program to Swap two numbers without using third variable in java



  1. package com.instaceofjava;
  2.  
  3. public class SwapTwoNumbers {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. int number1=20;
  8. int number2=30;
  9.  
  10. System.out.println("Before Swapping");
  11. System.out.println("Value of number1 is :" + number1);
  12. System.out.println("Value of number2 is :" +number2); 
  13.  
  14. number1=number1+number2;
  15. number2=number1-number2;
  16. number1=number1-number2;
  17.  
  18. System.out.println("After Swapping");
  19. System.out.println("Value of number1 is :" + number1);
  20. System.out.println("Value of number2 is :" +number2);
  21.  
  22. }
  23. }
Output:

  1. Before Swapping
  2. Value of number1 is :20
  3. Value of number2 is :30
  4. After Swapping
  5. Value of number1 is :30
  6. Value of number2 is :20

2. Java Program to Swap two numbers by using division and multiplication.


  1. package com.instaceofjava;
  2.  
  3. public class SwapTwoNumbers {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. int number1=20;
  8. int number2=30;
  9.  
  10. System.out.println("Before Swapping");
  11. System.out.println("Value of number1 is :" + number1);
  12. System.out.println("Value of number2 is :" +number2); 
  13.  
  14. number1=number1*number2;
  15. number2=number1/number2;
  16. number1=number1/number2;
  17.  
  18. System.out.println("After Swapping");
  19. System.out.println("Value of number1 is :" + number1);
  20. System.out.println("Value of number2 is :" +number2);
  21.  
  22. }
  23. }


Output:

  1. Before Swapping
  2. Value of number1 is :20
  3. Value of number2 is :30
  4. After Swapping
  5. Value of number1 is :30
  6. Value of number2 is :20


3. Java Program to Swap two integers by using bit wise operators


  1. package com.instaceofjava;
  2.  
  3. public class SwapTwoNumbers {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. int number1=2;
  8. int number2=4;
  9.  
  10. System.out.println("Before Swapping");
  11. System.out.println("Value of number1 is :" + number1);
  12. System.out.println("Value of number2 is :" +number2); 
  13.  
  14. number1=number1^number2;
  15. number2=number1^number2;
  16. number1=number1^number2;
  17.  
  18. System.out.println("After Swapping");
  19. System.out.println("Value of number1 is :" + number1);
  20. System.out.println("Value of number2 is :" +number2);
  21.  
  22. }
  23. }
Output:

  1. Before Swapping
  2. Value of number1 is :2
  3. Value of number2 is :4
  4. After Swapping
  5. Value of number1 is :4
  6. Value of number2 is :2


Select Menu