Header Ads

System out println in java | System.out.println in java

System.out.println in java




Example 1:


class Abc 

{

  static String name="abc";

}



Here,if we take Abc.name.length();  then

     

     1. Abc: It is a class.

     2. name: static variable of type string present in Abc class.

     3. It is a method present in string class.



Example 2:


import java.io.*;

class System

{

  static PrintStream out;


}



Here, if we take System.out.println();  then

  

      1. System:   It is a class present in java.lang.pkg

      2. out:      It is a static variable of type PrintStream present in                              System class.

      3. println():It is a method present in PrintStream class.






Example 3:


import static java.lang.System.out;


class Abc

{

  public static void main(String args[])

  {

     out.println("hello");

     out.println("hi"); 

    

  }

}



Output: 

       javac Abc.java    ->(run on cmd)

       java Abc          ->(run on cmd)

       hello              ->(output on cmd)

       hi                 ->(output on cmd)





Example 4:


import static java.lang.Integer.*;

import static java.lang.Byte.*;

class Abc

{

  public static void main(String args[])

  {


     System.out.println(MAX_VALUE);


  }

}



Output:

 Compile Time Error.


 

both variable MAX_VALUE in java.lang.Integer and variable MAX_VALUE in java.lang.Byte matches.




In the statement,System.out.println(MAX_VALUE);

Mostly we can see that two packages contain interface or a class with the same is very rare hence ambiguity problem is very rare in normal import.

But two classes or interfaces can contain a method or variable with the same name is very common hence ambiguity problem is also very common in static import.






Question: While resolving static members compiler will give the precedence in the following order?

1.Current class static member

2. Explicit static import

3. Implicit static import


Example:


// import static java.lang.Integer.MAX_VALUE; -------------> Line 2

import static java.lang.Byte.*;

class Abc

{

  public static void main(String args[]) throws Exception

  {

    // static int MAX_VALUE=999;  ---------------------------> Line 1

    System.out.println(MAX_VALUE);

  }


}



1. If we comment Line 1 then we will get Integer class MAX_VALUE: 2147483647

2. If we comment Line 1 and Line 2 then Byte class MAX_VALUE will be considered 127.



Which of the following import statements are valid ?

1. import java.lang.Math.*;               (Wrong)


2. import static java.lang.Math.*;        (Right)


3. import java.lang .Math;                (Right)


4. import static java.lang.Math;          (Wrong)


5. import static java.lang.Math.sqrt.*;   (Wrong)


6. import java.lang.Math.sqrt;            (Wrong)


7. import static java.lang.Math.sqrt();   (Wrong)


8. import static java.lang.Math.sqrt;     (Right)



Today's inspirational quotes: 

"Happiness is not something ready made. It comes from your own actions."




Steps to Subscribe my blog in 15 secs:
1. Enter your mail id in "Subscribe" box.
2. Enter Captcha for verification.
3. A mail will be received with link in your mail id.Just Click on link  and  then Done .Thank you for subscribing :)


Happy Reading.Happy Learning!!! See you in next post :)

No comments

Note: Only a member of this blog may post a comment.