-2

Could you please tell me, what is the "System. " in this code? and why they used it? when we should use "System. "? Where can I know I should use System. for nanoTime()?

 // A class to measure time elapsed.   
  public class Stopwatch{
    private long startTime;
    private long stopTime;

    public static final double NANOS_PER_SEC = 1000000000.0;

     // start the stop watch.
    public void start(){
    startTime = System.nanoTime();
    }

    // stop the stop watch.
    public void stop()
    {   stopTime = System.nanoTime();   }


    // elapsed time in seconds.
    // @return the time recorded on the stopwatch in seconds
    public double time()
    {   return (stopTime - startTime) / NANOS_PER_SEC;  }

    public String toString(){
        return "elapsed time: " + time() + " seconds.";
    }

    // elapsed time in nanoseconds.
    // @return the time recorded on the stopwatch in nanoseconds
    public long timeInNanoseconds()
    {   return (stopTime - startTime);  }
}
Shirin
  • 53
  • 9
  • just google of system in java or pick a beginner book from shelf to know what is system. If after that code is not working properly then only ask a question. – Ashutosh Nigam Mar 28 '15 at 08:33

2 Answers2

4

It's just the java.lang.System class. (The java.lang package is imported automatically.)

nanotime() is a static method within System, and out is a static field in System - so it's just making use of those members.

If you're not sure what static methods and fields are, you might want to read the Java tutorial.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

System.out.println()

System is a class in java.lang package, static fields and methods must be accessed by using the class name, so ( System.out ).

out here denotes the reference variable of the type PrintStream class.

println() is a public method in PrintStream class to print the data values.

Since nanoTime() is static method of System class you can call it directly with System.

Source1,Source2

Community
  • 1
  • 1
singhakash
  • 7,891
  • 6
  • 31
  • 65