×

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and a utility method for efficiently copying a portion of an array. It extends the Object class.

Sectors:

  1. public static final InputStream in: The input stream that is "typical." Each stream already is open and prepared to receive data as input. This stream often relates with keyboard input or any other input source that the hosts environment or user has chosen.
  2. public static final PrintStream out: The output stream that is "typical." This stream is open and prepared to receive output information. This stream often relates to order to provide customers or some other output destination that the hosts environment or user has defined.
  3. public static final PrintStream err: the output stream for "standard" errors. This stream is open and prepared to receive output information. This stream often relates to order to provide customers or some other output destination that the hosts environment or user has definedNormally, even though the important transmission flow, the valuation of variable out, has already been redirected to a file or another placement that is typically not constantly monitored, all above output stream is used to display error messages or other data that really should be managed to bring to the user's immediate attention.

Techniques:

  1. static void arraycopy(Object source, int sourceStart, Object Target, int targetSize, int size): propagates an array. The source parameter specifies an array to be copied, and sourceStart specifies the index in the source where the copy should start. Target contains the array which will receive the copy, and targetStart contains the index at which the copy will start within the array. The quantity of elements copied is known as size.

A Sample Program on illustrating arraycopy():

// An example of arraycopy() in Java
import java.lang.*;
import java.util.Arrays;
class SystemDemo
{
    public static void main(String args[])
    {
        int[] a = {2, 3, 4, 5, 6};
        int[] b = {1, 2, 3, 4, 5};
         
        System.arraycopy(a, 0, b, 2, 2);
         
        // array b after arraycopy operation
        System.out.println(Arrays.toString(b));
         
    }
}

Expected Output:

[1, 2, 2, 3, 5]

2. static String clearProperty(String key): the specified key deletes the system property that is indicated by it.

Syntax: public static String clearProperty(String key)

Reverts: the string value of the system

property, or null if there is no property with that key.

If the key is empty, an illegal argument exception occurs.

3. static String getProperty(String key) : the key supplied returns the system property.

Syntax: public static String getProperty(String key)

Reverts: the system property's string value, or null if that key doesn't correspond to any properties,

Exception:

SecurityException: if there is a security manager,

Using the checkPropertyAccess method prevents access to the

referred to system property.

If key is null, a NullPointerException occurs.

If the key is empty, an illegal argument exception occurs.

4. static string getProperty(String key, String def): Gets the system property denoted by the

syntax: public static String getProperty(String key, String def)

returns: the system property's string value,

alternatively, in the absence of a property with that key, the default value.

If a security manager is present and its checkPropertyAccess method disallows access to the requested system property, a SecurityException is thrown.

If key is null, a NullPointerException occurs.

If the key is empty, an illegal argument exception occurs.

5. static String setProperty(String key, String value): Specifies the key to press to set the system property.

syntax: public static String setProperty(String key, String value)

returns: if the system property had no previous value, null is returned.

Exception:

SecurityException: is thrown when a security manager is present but its checkPermission method forbids setting the requested property.

If the key or value is null, a NullPointerException will occur.

If the key is empty, an illegal argument exception occurs.

// Examples of clearProperty() and getProperty in Java ()
// and methods setProperty()
import java.lang.*;
import static java.lang.System.clearProperty;
import static java.lang.System.setProperty;
import java.util.Arrays;
class SystemDemo
{
    public static void main(String args[])
    {
        // examining a certain property
        System.out.println(System.getProperty("user.home"));
         
        // purging this location
        clearProperty("user.home");
         
        System.out.println(System.getProperty("user.home"));
         
        // defining a particular property
        setProperty("user.country", "India");
         
        // checking the property
        System.out.println(System.getProperty("user.country"));
         
        // examining a different property than a system property
        // illustrating getProperty(String key, String def)
        System.out.println(System.getProperty("user.password",
              "Business tycoon"));
    }
}

Expected Output:

null
India
Business tycoon

Related Topics

Centered Square Numbers in Java

In this tutorial, we will understand how to check the number is a centered square number. It is one of the prevalent interview questions of IT companies. Firstly, we will...

3 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

Application of Array in Java

In this article we are going to acknowledge about what the array is, types of arrays and their applications. What is an array? An array is often a set of interrelated elements...

4 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.

Tree Implementation in Java

Introduction to Tree A non-linear, hierarchical data structure called a "tree" is made up of a number of nodes, each of which contains a values and a sequence of pointers to...

14 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

How annotations work in Java

Annotations were introduced by sun microsystem and are available from the java 1.5 version. In general, configurations can be done either by XML or by annotations. Hibernate and spring framework users prefer...

4 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Zebra Puzzle Problem in Java

Complex puzzles like the zebra puzzle demand a lot of work and mental training to complete. Because it was created by renowned German scientist Albert Einstein, it is also sometimes...

10 minutes read.