Home >Java >javaTutorial >Java program to check if JVM is 32-bit or 64-bit

Java program to check if JVM is 32-bit or 64-bit

WBOY
WBOYforward
2023-09-05 18:37:061493browse

Java program to check if JVM is 32-bit or 64-bit

Before writing a java program to check whether the JVM is 32-bit or 64-bit, let’s discuss the JVM first.

JVM is a java virtual machine, responsible for executing bytecode. It is part of the Java Runtime Environment (JRE). We all know that java is platform independent, but JVM is platform dependent. We need a separate JVM for each operating system. If we have the bytecode of any java source code, we can easily run it on any platform due to JVM. The entire process of java file execution is as follows -

  • First, we save the java source code with the extension .java, and the compiler converts it into bytecode with the extension .class. This happens at compile time.

  • Now, at runtime, the JVM reads and verifies the bytecode, allocates memory for variables, and then converts that bytecode into a machine-readable form.

We can check whether the JVM is 32-bit or 64-bit by using the two system properties "os.arch" and "sun.arch.data.model" in the java program. There is another way, we can check it through command line using "java --version".

Get attributes()

In this article, we will use this method to get the details of the JVM. It exists in system classes. It helps in retrieving system properties for specified parameters. Returns a string containing the value of the given parameter if the parameter exists, otherwise returns null.

grammar

System.getProperty(key); 

Here, "key" accepts parameters.

Method 1: Use os.arch

Example

public class Main {
   public static void main(String[] args) {
      String info = System.getProperty("os.arch");
      System.out.println(info + "-bit JVM is installed in your device ");
   }
}

Output

amd64-bit JVM is installed in your device

In the above code, we declare a string variable named "info" to store the value returned by the "getProperty()" method.

Method 2: Use sun.arch.data.model

Example

public class Main {
   public static void main(String[] args) {
      String info = System.getProperty("sun.arch.data.model");
      if (info.equals("64")) {
         System.out.println(info + "-bit JVM is installed in your device ");
      } else {
         System.out.println(info + "-bit JVM is installed in your device ");
      }
   }
}

Output

64-bit JVM is installed in your device

In the above code, we declare a string variable named "info" to store the value returned by the "getProperty()" method. In the if else block we check if the returned string is equal to "64" or not using the "equals()" method. The ‘equals()’ method is used to compare two strings. Its return type is a Boolean value that returns true if the two strings are equal, false otherwise. In this example, if block is true. Therefore, it prints 64-bit.

Method 3: Using the command line interface

Open cmd in your device and enter "java --version". When you press Enter, you'll see results based on your device's configuration -

C:\Users\Lenovo>java --version
java 17.0.6 2023-01-17 LTS
Java(TM) SE Runtime Environment (build 17.0.6+9-LTS-190)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.6+9-LTS-190, mixed mode, sharing)

in conclusion

The JVM is a platform-dependent machine that converts byte code into a machine-readable format. The main difference between 32-bit and 64-bit JVMs is the memory size limit. If it's 32-bit, we can specify a maximum of 4 GB, but 64-bit is much larger. In this article, we discussed two java programs to check whether a JVM is 32-bit or 64-bit.

The above is the detailed content of Java program to check if JVM is 32-bit or 64-bit. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete