Displaying output using ‘println‘ states that , when you want to display something on the output console from your program you need to make use of these ‘println‘ methods. Apart from println() method , Java has three more methods which are used likely less than the println() and they are print() and printf()
println() – Method
- In order to display your output in a new line , we need to make use of this println() method.
- The println() method is the pre-defined method of ‘PrintStream’ class and this PrintStream class belongs to java.io package.
- println() method is an overloaded method of PrintStream class.
- Overloading a method is the process of defining same name to more than one method which is used for different purposes.
- The overloading methods of println() are :
- println(short) ; 2. println(byte); 3. println(int); 4. println(long); 5. println(float); 6. println(double); 7. println(String); 8. println(boolean); 9. println(char);
- These are the overloading methods of println() of PrintStream class. Using these methods you can print an integer and a string and a float value etc
Displaying output using ‘println’
To know how to use these overloaded methods in your Java program , let’s take a look at the sample program which uses different types of println() methods to display the result.
http://www.javafrombasics.com uses eclipse to write Java programs. If you want to know how to use eclipse for beginners , please do visit our page for more information
Program Discussion Points
- Even it is an int value but when you mention double quotes “10+20 ” , Java will take it as a string.
- In Java the ‘+’ operator acts according to the situation it is called polymorphic behavior , as you can see in the fourth and fifth println statements.
- If you want to display different data types values then make use of this ‘+’ operator which is explained in the last println statement.
- print() method prints the entire output in one single line , which is quite complex to understand for the end user.
- In the sample program output , if you want to print the output in one single line then make use of this print() method.
- In the above println statements remove ‘println‘ and replace it with ‘print‘. your output will be in one single line.