Thursday, July 25, 2013

System Class - I



In Java default package(java.lang) System class is defined as final and cannot be instantiated. All its member fields and methods are static. System class t is an utility class. Facilities provided by the System class are standard input, standard output, and error output streams.  PrintStream.println and PrintStream.print display binary data as a stream of human-readable characters. The formatting is primitive.

Following are the fields for java.lang.System class:

    static PrintStream err -- This is the "standard" error output stream.
    static InputStream in -- This is the "standard" input stream.
    static PrintStream out -- This is the "standard" output stream.

out – is a member field declared as  static in System class and type is PrintStream.

println / print – prints the argument passed to the standard console. System.out.println will print on a new line where System.out.print will print and the same line.

printf - used to send formatted numerical output to the console. It uses  java.util.Formatter objects. It follows the printf() function in C.

Some Format Specifiers like %d use for Integer Data type and  %f is use for Floating point.


System.out.print()

//Output Statement - println()
    class Sys_Print
        {
            public static void main(String args[])
                {
                    int num1,num2,prod;
                    num1=50;
                    num2=45;
                    prod=num1*num2;
                    //Both the output will print in same line
                    System.out.print("Result  : ");
                    System.out.print("Product of "+num1+" and  "+num2+" = "+prod);
                }
        }


System.out.println()

//Output Statement - println()
    class Sys_Println
        {
            public static void main(String args[])
                {
                    int num1,num2;
                    double avg;
                    num1=50;
                    num2=45;
                    avg=(num1+num2)/2.0;
                    System.out.println("Average of "+num1+" and  "+num2+" = "+avg);
                }
        }


System.err

    public class Sys_Err
        {
            public static void main(String args[])
                {
                    System.err.println("There is nothing to do!");
                }
  
        }





No comments: