Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction🎯
2.
Java System getenv 
2.1.
Java System.getenv()🔶
2.2.
Example1
2.3.
Output
2.4.
Java System.getenv(String key)🔶
2.5.
Example2
2.6.
Output
3.
Frequently Asked Questions
3.1.
To which class does the Java system getenv belong?
3.2.
What is the primary use of the Java system getenv function?
3.3.
Why would getenv return null?
3.4.
How to use the Java system getenv method? 
3.5.
How does the Java system getenv return the output ?
4.
Conclusion 
Last Updated: Mar 27, 2024
Easy

System.getenv() Method in Java

Author Aashna Luthra
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction🎯

Java is a powerful programming language that helps develop mobile apps, web apps, games, etc. It offers multiple functions, and one such function is Java System getenv.  

intro_image

In this article, we will explore how the Java system getenv works.

Also read, Duck Number in Java and Hashcode Method in Java

Java System getenv 

Java System.getenv()🔶

Syntax: 

getenv()


System.getenv() is provided in the Java System class. The environment variable value set in the current system can be obtained using this function. Java system getenv function returns an unchangeable string map view in the form of Map<String,String> of the current system environment. 

To better understand, let’s look at an example of how the Java system getenv works.

Example1

Now let’s look at another example to get the Map view of the System Environment.

import java.util.Map;
public class Example1 {
    public static void main(String[] args) { 
        Map<String, String> env  = System.getenv(); 
        for (String name : env.keySet()) { 
            System.out.format("%s = %s%n", name, env.get(name)); 
        } 
    } 
}

Output

output_getenv()

Note: The output may vary depending on the environment variables that you have configured on your PC.

Java System.getenv(String key)🔶

Syntax 

System.getenv(String key)


where key denotes the environment variable whose values the user wants. Let’s look at the examples of how the getenv function with parameters works.

Example2

Let’s look at a Java program to get the value of a specific environment variable.

public class Example2 { 
    public static void main(String[] args) 
    { 
        // Get the value of the environment variable TEMP
        System.out.println(System.getenv("TEMP")); 
  
        // Get the value of the environment variable OS
        System.out.println(System.getenv("OS")); 

        // Get the value of the environment variable PROCESSOR_ARCHITECTURE 
        System.out.println(System.getenv("PROCESSOR_ARCHITECTURE"));
      } 
} 

Output

output_getenv(string)

Note: The output may vary depending on the environment variables that you have configured on your PC. You can try by yourself on java online compiler.

We hope you have learned everything about the Java system getenv. 🙌

Check out this article - Upcasting and Downcasting in Java

Must Read: Java System Out Println

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

To which class does the Java system getenv belong?

The Java system getenv belongs to the class Java.lang.System.

What is the primary use of the Java system getenv function?

The environment variable value set in the current system can be obtained using this function

Why would getenv return null?

The Java system getenv will return null if the variable is not defined in the system environment.

How to use the Java system getenv method? 

We can use the Java system getenv method by: getenv() or getenv(String name).

How does the Java system getenv return the output ?

The function returns the output in the form of key-value pair, just like Map data structure. 

Conclusion 

In this blog, we explored the Java system getenv along with how to use the function by studying some examples.

We hope this blog has helped you enhance your knowledge of Java System.getenv() and if you want to learn more, check out our articles here

You can refer to similar articles for more information

  1. Inheritance in Java
  2. Introduction to keywords in Java
  3. Constructor chaining in Java
  4. Static and Instance methods in Java
  5. entryset java
  6. Swap Function in Java

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Also, look at the interview experiences for placement preparations. 

Happy Learning Ninja! 🥷

Previous article
flatMap() Method in Java
Next article
Wrapper class in Java
Live masterclass