Posts

Showing posts from April, 2020

Program to find most frequent element in array

Java program to find the most frequent element in the Array import java.util.*;     class GFG {             static int mostFrequent(int arr[], int n)      {                     Arrays.sort(arr);                     int max_count = 1, res = arr[0];          int curr_count = 1;                     for (int i = 1; i < n; i++)          {              if (arr[i] == arr[i - 1])                  curr_count++;              else              {                  if (curr_count > max_count)                  {                      max_count = curr_count;                      res = arr[i - 1];                  }                  curr_count = 1;              }          }                 if (curr_count > max_count)          {              max_count = curr_count;              res = arr[n - 1];          }                 return res;      }             public static void main (String[] args) {                     int arr[] = {1, 5, 2, 1, 3, 2, 1};       

Core Java - JVM Tutorial

Image
JVM - Java virtual machine JDK - Java Development Kit It is a software which contains all the programs required for compilation, execution, debugging etc. JDK contains tools to develop the Java program and JRE to run the program. JRE - JRE stands for Java Run-time Environment JRE is targeted for execution of the Java files that is JRE is nothing but JVM with the Java Packages Classes(like util, math, lang, awt,swing etc) with addition of runtime libraries. JDK is mainly targeted for java development purpose. JVM - Java Virtual Machine It executes the whole program. It provides a platform to run the program. JIT - Just In Time Normally the source code is completely converted into the machine code. In JIT Scenerio, the source code is converted to assembly language like structures (intermediate language for C# or Byte code for Java or P code for Pascal). The intermediate code is only converted to machine code only when the application needs i.e. fraction of codes which are requ

Core Java - Type Casting

Typecasting Converting one type to another type is called typecasting. Conversion is done from primitive to primitive or nonprimitive to nonprimitive. Example int i = 10 ; double d = ( double ) i ; SOP ( i ) ; // 10 SOP ( d ) ; // 10.0 int j = ( int ) 102.56 ; SOP ( j ) ; // 102 Copy Boolean type can't be casted. If we convert smaller to bigger type, it is called widening or explicit type casting. If we convert bigger to a smaller type, it is called narrowing or implicit type casting. Widening is implicit or automatic. Narrowing is explicit. In widening there is no loss of data. In narrowing, loss of data or loss of precision may take place. 8 primitive types, their size and default values Type Size Default value byte 1 byte 0 short 2 byte 0 int 4 byte 0 long 8 byte 0 float 4 byte 0.0f double 8 bye 0.0d char 2 byte '\u0000' boolean Not defined false public static void main ( String [ ] arg ) { m1 ( 'a' ) ; m1 (

Core Java - Introduction

Overview Java language was originally developed by Sun Microsystems and released in the year 1995 as the core component of Sun Microsystems' Java platform product till that year. The latest release is Java SE 8. With its improvements and widespread popularity, multiple configurations were built to cater for the various types of platforms. for Example: Enterprise Applications(J2EE), for Mobile Applications(J2ME). The new J2 versions of Java were renamed as Java SE, Java EE, and Java ME respectively. Java maintains the Write Once, Run Anywhere principle from the beigning. Characteristics of Java Dynamic - Java is more dynamic than C or C++ because it is designed to adapt to a changing environment. Java programs are capable of carrying the large amount of run-time information data that can be used to verify and resolve accesses to objects at run-time. Distributed - Java is designed for the distributed nature environment on the internet. High Performance With the use of

Java program for implementation of Insertion Sort

Java program for implementation of Insertion Sort  class InsertionSort {          void sort(int arr[])      {          int n = arr.length;          for (int i = 1; i < n; ++i) {              int key = arr[i];              int j = i - 1;                 while (j >= 0 && arr[j] > key) {                  arr[j + 1] = arr[j];                  j = j - 1;              }              arr[j + 1] = key;          }      }         static void printArray(int arr[])      {          int n = arr.length;          for (int i = 0; i < n; ++i)              System.out.print(arr[i] + " ");             System.out.println();      }         public static void main(String args[])      {          int arr[] = { 12, 11, 13, 5, 6 };             InsertionSort ob = new InsertionSort();          ob.sort(arr);             printArray(arr);      }  } 

Java Program to count the total number of vowels and consonants in a string

Java Program to count the total number of vowels and consonants in a string public class CountVowelConsonant {         public static void main(String[] args) {                          int vCount = 0, cCount = 0;             String str = "This is a really simple sentence";                          str = str.toLowerCase();                          for(int i = 0; i < str.length(); i++) {                 if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {                     vCount++;                 }                 else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {                      cCount++;                 }             }             System.out.println("Number of vowels: " + vCount);             System.out.println("Number of consonants: " + cCount);         }     }