What are the different way if creating String in Java?

There are 3 way of creating string in Java.
a) We can create a string just by assigning a group of characters to a string type variable.

String s = "hello";

b) We can create an object to String class  by allocating memory using new operator. This is just like creating an object to any class.

String s = new String ("hello");

Here we doing two things. First, we creating object using new operator. Then, we are storing the string: "hello" into the object.

c) The third way of creating the strings is by converting the character arrays into strings. Let us take a character type array: art[  ].

char arr[  ] = { 'c' , 'h' , 'a', 'i' , 'r', 's' } ;

Now create a string object by passing the array name to it, as:
String s = new String (arr) ;

Now the string s contains the string value "chairs" . This means all the characters of the array into the string.

Note

String s = new String (arr, 2, 3);

Here, starting from 2nd character a total of 3 characters are copied into the string s., the 0th character in the array is 'c' and the 2nd character is 'a' . Staring from 'a', a total of three characters implies 'air'. So these three characters are copied into the string s.

Can we call a class as a data type?

Yes, a class is also called user defined data type. This is because a user can create a class.

Is String a class or data type?

String is a class in java.lang package. But in Java, all classes also considered as data types, so we can take String as a data type also.

On which memory, array are created in Java?

Arrays are created on dynamic memory by JVM. There is no question of static memory in Java, every thing (variable, array and object etc) is created on dynamic memory only.

What is the difference between System.out and System.err ?

System.out and System.err both represent the monitor by default and hence can be used to display normal messages and results but System.out is used to display normal messages and results whereas System.err is used to display error messages.

What is the difference between System.exit(0) and System.exit(1) ?

System.exit(0) terminates the program normally. Whereas System.exit(1) terminates the program because of some error encountered in the program.

What is the difference between return and System.exit(0) ?

return statement is used inside a method to come out of it. System.exit(0) is used in a method to come out of the program.

What is the use of continue statement?

When continue is executed, subsequent statements in the loop are not executed and control of execution goes back to the next repetition of the loop.

Why goto statements are not available in Java?

1) goto statement lead to confusion for a programmer. Especially in large program, if several goto statements are used then the programmer would perplexed while understanding the flow.
2) goto statements make documentation of a program difficult.
3) goto statements are not part of structured programming.
Because of these reasons the goto statement is not supported in java

Note: The goto statement is reserved keyword in Java.

What are the uses of break statement?

The break statement can be used in 3 ways:
1) break is used inside a loop to come out of it.
2) break is used inside the switch block to come out of the switch block.
3) break can be used in nested blocks to go to the end of a block.

Write a program to display stars in a right angle triangle form

What is the difference between >> and >>> ?

Both bitwise right shift operator (>>) and bitwise zero fill right shift operator (>>>) are used to shift the bits towards right.

The difference is that >> will protect the sign bit whereas the >>> operator will not protect the sign bit. It always fills 0 in the sign bit.

How are positive and negative numbers represented internally?

Positive numbers are represented in binary using 1's complement notation and negative numbers are represented by using 2's complement notation.

What is the difference between float and double?

float can represent up to 7 digits accurately after decimal point whereas double can represent up to 15 digits accurately after decimal point.

Java program to bubble sort

Java program to bubble sort: This code sorts numbers inputted by user using Bubble sort algorithm.

Java programming code

import java.util.Scanner;
 
class BubbleSort {
  public static void main(String []args) {
    int n, c, d, swap;
    Scanner in = new Scanner(System.in);
 
    System.out.println("Input number of integers to sort");
    n = in.nextInt();
 
    int array[] = new int[n];
 
    System.out.println("Enter " + n + " integers");
 
    for (c = 0; c < n; c++) 
      array[c] = in.nextInt();
 
    for (c = 0; c < ( n - 1 ); c++) {
      for (d = 0; d < n - c - 1; d++) {
        if (array[d] > array[d+1]) /* For descending order use < */
        {
          swap       = array[d];
          array[d]   = array[d+1];
          array[d+1] = swap;
        }
      }
    }
 
    System.out.println("Sorted list of numbers");
 
    for (c = 0; c < n; c++) 
      System.out.println(array[c]);
  }
}
Complexity of bubble sort is O(n2) which makes it a less frequent option for arranging in sorted order when quantity of numbers is high.

Output of program:



You can also use sort method of Arrays class to sort integers in ascending order but remember that sort method uses a variation of Quick sort algorithm.
import java.util.Arrays;
 
class Sort
{
  public static void main(String args[])
  {
    int data[] = { 4, -5, 2, 6, 1 };
 
    Arrays.sort(data);
 
    for (int c: data) 
    {
      System.out.println(c);
    }
  }
}

Java program to multiply two matrices

This java program multiply two matrices. Before multiplication matrices are checked whether they can be multiplied or not.

Java programming code

import java.util.Scanner;
 
class MatrixMultiplication
{
   public static void main(String args[])
   {
      int m, n, p, q, sum = 0, c, d, k;
 
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns of first matrix");
      m = in.nextInt();
      n = in.nextInt();
 
      int first[][] = new int[m][n];
 
      System.out.println("Enter the elements of first matrix");
 
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            first[c][d] = in.nextInt();
 
      System.out.println("Enter the number of rows and columns of second matrix");
      p = in.nextInt();
      q = in.nextInt();
 
      if ( n != p )
         System.out.println("Matrices with entered orders can't be multiplied with each other.");
      else
      {
         int second[][] = new int[p][q];
         int multiply[][] = new int[m][q];
 
         System.out.println("Enter the elements of second matrix");
 
         for ( c = 0 ; c < p ; c++ )
            for ( d = 0 ; d < q ; d++ )
               second[c][d] = in.nextInt();
 
         for ( c = 0 ; c < m ; c++ )
         {
            for ( d = 0 ; d < q ; d++ )
            {   
               for ( k = 0 ; k < p ; k++ )
               {
                  sum = sum + first[c][k]*second[k][d];
               }
 
               multiply[c][d] = sum;
               sum = 0;
            }
         }
 
         System.out.println("Product of entered matrices:-");
 
         for ( c = 0 ; c < m ; c++ )
         {
            for ( d = 0 ; d < q ; d++ )
               System.out.print(multiply[c][d]+"\t");
 
            System.out.print("\n");
         }
      }
   }
}

Output of program:



This is a basic method of multiplication, there are more efficient algorithms available. Also this approach is not recommended for sparse matrices which contains a large number of elements as zero.

Java program to transpose matrix

This java program find transpose of a matrix of any order.

Java programming source code

import java.util.Scanner;
 
class TransposeAMatrix
{
   public static void main(String args[])
   {
      int m, n, c, d;
 
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns of matrix");
      m = in.nextInt();
      n = in.nextInt();
 
      int matrix[][] = new int[m][n];
 
      System.out.println("Enter the elements of matrix");
 
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            matrix[c][d] = in.nextInt();
 
      int transpose[][] = new int[n][m];
 
      for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < n ; d++ )               
            transpose[d][c] = matrix[c][d];
      }
 
      System.out.println("Transpose of entered matrix:-");
 
      for ( c = 0 ; c < n ; c++ )
      {
         for ( d = 0 ; d < m ; d++ )
               System.out.print(transpose[c][d]+"\t");
 
         System.out.print("\n");
      }
   }
}

Output of program:



This code can be used to check if a matrix symmetric or not, just compare the matrix with it's transpose if they are same then it's symmetric otherwise non symmetric, also it's useful for calculating orthogonality of a matrix.