Skip to main content

Java System Class --- a Utility often used

Java System Class

The System class  is a final class and  contains many useful fields and methods which are quite useful utilities like:
standard input, output and error, for loading files and libraries.
It helps to provide access to externally defined properties and variables.
It also contain utility methods to quickly copy an array
To get the environment variables.

Fields in the System Class:

static PrintStream err : The standard error output stream



static InputStream in : The standard input stream
static PrintStream out: The standard output stream

The class has many useful methods , lets see few important methods:


static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length): It is used to copy array from src array to destination array starting at srcPosition and starts copying at destPosition and copies the length specified from source to destination.

System Properties Methods:
It contains method to get , set and clear system properties.
It can get   full set of properties or the specified properties.
You can get and print all set of properties as below :


Properties sysProps = System.getProperties();
Set<Object> keySet = sysProps.keySet();
for(Object obj : keySet){
    String key = (String) obj;
    System.out.println("{"+obj+"="+sysProps.getProperty(key)+"}");
}
 

For setting multiple properties below is the method

static void setProperties(Properties props)

Few important system properties are like : "user.home" to provide the home directory for the user.
similarly "user.dir" provides the default user directory
"file.separator" : to provide the file separator used in the current environment.
"os.name" : current operating system being used

public static Console console() : Returns the unique Console object associated.


System Environment Variables:
You can also get environment variables.
It can return a specified environment variable value or a MAP of all the values. Below are the methods to get that

public static String getenv(String name)
public static Map <String, String> getenv()

SecurityManager: We can get and set the security Manager using system class.
Security Manger is used to implement the security policy.
For example, while getting and setting the system properties,  first it checks if there is any security manager. Then it calls checkPropertiesAccess() which will threw a SecurityException if the calling thread is not allowed to access or modify the system properties.
Similarly while calling getenv or setenv it calls  its checkPermission method is called with a RuntimePermission("getenv."+name)

How does System.out.println() works:
System is a final class which contains a public static final member refrence 'out', of class PrintStream .
The PrintStream class contains the println().
There are multiple println methods with different arguments (overloading). Every println makes a call to print method and adds a newline. print calls write() and the story goes on like that.











Comments

Popular posts from this blog

OBJECT class in Java

OBJECT class in Java : Object is at the top of class hierarchy in java. Every class in the Java system is a descendent (direct or indirect) of the Object class. The Object class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class. Mainly below methods are provided by Object class : public String toString() returns the string representation of this object. protected Object clone() throws CloneNotSupportedException creates and returns the exact copy (clone) of this object. public boolean equals(Object obj) compares the given object to this object. public int hashCode() returns the hashcode number for this object. public final Class getClass() returns the Class class object of this object. The Class class can further be used to get the metadata of ...

Java Priority Queue sort using lambda expression

Priority Queue :     In Java  Priority Queue  is a  queue  which keeps its elements sorted as per their natural order( example in ascending orders for Integer, alphabetical a-z for alphabets) or using a custom Comparator at the time of creation you can custom sort it. It has most method  similar to a queue  add, clear, poll, peek As priority queue have to compare elements to keep in order so elements must be comparable otherwise, it will throw ClassCastException .  As null can not be compared so you are not allowed to insert null too A program to sort priority queue using Lambda function in Java           c lass   PriorityQueueLambdaJava { public List<Integer> KFrequent( int [] nums, int k) { Map<Integer, Integer> map = new HashMap<>(); for ( int j: nums) { map .put(j, map .getOrDefault(j, 0 ) + 1 ); } PriorityQueue<Map.Entry<Integ...

Important Keywords: Static, Continue,Break:

Important Keywords: Static, Continue,Break:  Static: The static keyword in java is used for memory management mainly.  The static keyword members belongs to the class rather than instance of the class. Following members can be declared as static: variable (also known as class variable) method (also known as class method) block nested class  static variables : We can use static keyword with a class level variable. A static variable is a class variable and doesn’t belong to Object/instance of the class. Since static variables are shared across all the instances of Object, they are not thread safe . Usually static variables are used with final keyword for common resources or constants that can be used by all the objects. If the static variable is not private, we can access it as: ClassName.variableName //static variable example private static int a ge; public static String name; static methods: A static method belongs t...