What is System and out in System.out.println() in Java

A basic question pop up in mind while doing programs and when facing interviews
What is

   System and Out in System.out.println()

in JAVA..??
exactly How it works internally ..??
In this post we are going to do postmortem of this System.out.println();

Objective :

1.       What Is System in System.out.println();
2.       What is .out in SOP…?
3.       If .println(), .print() is method in PrintStream Class then why we are calling it from System      Class.
4.        Print  Hello on console and in  “test.txt” File java Program : .....You 'll Enjoy this Program
5.        Summary of SOP .


1.What Is System in System.out.println();
System is a class made available by Java to let us manipulate various operating system related objects. It's from java.lang package .

System is final class it means we cannot Inherit class System .
Can we create Object of it…………….??
No cz java has also make its default constructor as private  ( Very Smart .…


See in Image above so we cannot create inherit it and cannot create Object out of it.
As per javadoc, “…>System  class provide services to
  standard input, standard output, and error output streams;”
  we can say it as utility class.

2.What Is out in System.out.println();
Out is a static variable inside  System class is variable  declared as object of PrintStream class.
Out variable  actually pointing to PrintStream Class to use  functionalities provided by PrintStream Class

                                                              Something pop up in your mind…?Why we need out variable to use functionality                                                                                                                                                                          provided by PrintStream Class

Can we :
1.     Create object of PrintStream class in our code and write op : YES
Following program ‘ll clear your all Doubts..

Program to print Hello on console and in file "test.java".

import java.io.*;

 public final  class TP {
public static void main(String a[]){

try{
PrintStream f = new PrintStream(
     new FileOutputStream("test.txt"));
System.out.println("Folowing p.println(hello ) is in file  test.txt \n");

f.println("hello");      // fis printstream Obj calling println() method

PrintStream p = System.out;    // p pointing to System class PrintStream Object 

p.println("Hello i am here on console  by PrintStream Object p  \n");
System.out.println(" I am on console your Old Friend  System.out.println()  ");

}
catch(Exception e){}

  }
}


3. If .println(), .print() is method in PrintStream Class then why we are calling it from System Class.
Answer lies in Above program , We use System class to call println() method because we want to put output on console in case if we want  to put output on file or any other than Console then we are not going to use out variable in System class.


4. Print  Hello on console and in  “test.txt” File java Program :
Please see program mention in this post.


5.Summary of System.out.println();

  • System is final class in java it is use to manipulate various operating system related objects in our case we are using it to print output on console
  • ‘out’ is a static member variable in System Class whose return type is PrintStream { here System class Actually pointing to PrintStream Class using out variable.
  • Println()/print() is method in PrintStream class out variable is accessing this methods From System class.  








Palindrome of string and Number together

Palindrome and String I have gone through lots of palindrome programs but did not find any Program which give me palindrome of string and Integer in the same input. I write code for that. Following is my code :
public class PalindromeString {
public static void main(String args[])
{
Scanner sca =new Scanner(System.in);

System.out.println(" Enter Number or string to check whether it is palindrome");
while (sca.hasNext()){
String input=sca.nextLine();


if(!input.matches("^\\d*$")){    // check for if ( string contain numbers..?)
isPalindrome(input);          // call to    isPalindrome(String str)  method
if(isPalindrome(input)){
System.out.println("you Entered  " +input + " is Palindrome ");
}
else{
System.out.println("you Entered " +input + " is  NOT Palindrome");
}
}

if( input.matches("^\\d*$")){

// System.out.println("you entered number");
if(isPalindrome(input)){
System.out.println("you Entered  " +input + " is Palindrome ");
}
else{
System.out.println("you Entered " +input + " is  NOT Palindrome");
}

}

}

}




public static boolean isPalindrome(String str) {

int i;
int n=str.length();
String revstr="";
for(i=n-1;i>=0;i--)
revstr=revstr+str.charAt(i);
if(revstr.equals(str))
return true;
else
return false;
}

public static boolean isPalindrome(int number) {

int palindrome = number; // copied number into variable
int reverse = 0;

while (palindrome != 0) {
int remainder = palindrome % 10;
reverse = reverse * 10 + remainder;
palindrome = palindrome / 10;
}

// if original and reverse of number is equal means
// number is palindrome in Java
if (number == reverse) {
return true;
}
return false;
}
}

Output :
  Enter Number or string to check whether it is palindrome  
 adi4444ida  
 you Entered adi4444ida is Palindrome   

In code
 if( input.matches("^\\d*$")) ("^\\d*$")) is regex 
 You can read more about regex in followiong link. 
Know Regex

Play around it
If any problem in code let me know.


Happy Coding.....