Friday, March 24, 2017

Is JVM physically exist?

JVM (Java Virtual Machine) is not physical entity. It is just a Virtual machine. Here Virtual means not having physical existence.

What is Virtual machine?

Virtual machine is a simple software program which simulates the functions of a physical machine. This is not having physical existence, but which can perform all operations like physical machines. Physical machine whatever it is doing, all those things we can able to do on the virtual machine. Best example of virtual machine is computer; it is worked like physical calculator.

All virtual machines categorized in to 2 types

1. Hardware based or System based Virtual Machine

2. Software based or Application based or process based Virtual Machine

Hardware based virtual machines

Physically only one physical machine is there but several logical machines we can able to create on the single physical machine with strong isolation (worked independently) of each other. This type of virtual machines is called hardware based virtual machines.

Main advantage of hardware based virtual machine is effective utilization of hardware resources. Most of the times these types of virtual machines associated with admin role (System Admin, Server Admin etc). Programmers never associated with the hardware based virtual machines.

Examples of hardware based virtual machines are

1. KVM (Kernel Based VM) for Linux systems

2. VMware

3. Xen

4. Cloud Computing

Software based virtual machines

These virtual machines acts as run time engine to run a particular programming language applications. Examples of software based virtual machines are

1. JVM (Java Virtual Machine) acts as runtime engine to run Java applications

2. PVM (Parrot Virtual Machine) acts as runtime engine to run Perl applications

3. CLR (Common Language Runtime) acts as runtime engine to run .NET based applications

Thanks,

Regards
Ashok Kumar, Software Developer at Vistata IT

Wednesday, March 22, 2017

How to convert String into integer array

If the string is something like “12 41 21 19 15 10” and we want an into array, do like follows:

String test = "12 41 21 19 15 10";
// The string to be extracted to an integer array.

String[] intAsString = test.split(" ");
// Splits each space separated integer into a String array.
int[] integers = new int[intAsString.length];

// Creates the integer array.
int i=0;
for (String no : intAsString){
integers[i++] = Integer.parseInt(no);
}

Or if numbers are comma separated:

// String source = "1,2,3,56789,42";
private static int[] getIntsFromString(String source ) {
  String[] integersAsText =  
  source.split(","); 
  int[] results = new 
  int[ integersAsText.length ]; 
  int i = 0;   for ( String textValue :
  integersAsText ) {
  results[i++] = 
  Integer.parseInt( textValue );
  }
  return results ;
}

How do you convert integer to String in Java?

Different possible ways to convert integer into String!

The first one is the easiest and the simplest:

int i = 1234;
String s = "" + i;

The second method uses the valueOf()function of the String class:

int i = 1234;
String s = String.valueOf(i);

The third one requires wrapper class Integer and uses its toString() function:

int i = 1234;
String s = new Integer(i).toString();

One more program

public class Test { public static void main(String[] args) {
// First Method
System.out.println(String.valueOf(12345));
// Second Method
System.out.println(Integer.toString(12345));
// Third Method
System.out.println(new Integer(12345).toString());
// Fourth Method
System.out.println(String.format("%d", 12345));
// Fifth Method
System.out.println(new StringBuffer().append(12345).toString());
// Six Method
System.out.println(new StringBuilder().append(12345).toString()); // Seventh Method
int num = 12345; String numString = "" + num; System.out.println(numString);
}

Output
12345
12345
12345
12345
12345
12345
12345

A video to explain the same