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:
- 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.
- 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.
- 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:
- 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