Trending
Loading...
Saturday, 25 March 2017

How to write on console screen




                        Write on output screen


Writing something on the output screen in java is a easy task and is the base of any simple program written in java. We will here see how we can do so .


Output Functions

In java there are two output functions print() and println() which are used to display text or values of variables or both  on the output screen in java.Let's have a look at them.

print():- This function print the text given in double quotes i.e.("Your Text Here") as it is but does not change the line it means when it is once again invoked with different text then it will print that text on the same line.
E.g :- If i write these lines of codes
                     System.out.print("My name is ");
                     System.out.print("Shashank sahu");
          Then output will be as follows :-
 
            My name is Shashank sahu

println():-  This function print the text given in double quotes i.e.("Your Text Here") as it is but unlike print() it changes the line it means when it is once again invoked with different text then it will print that text on the next  line.

E.g :- If i write these lines of codes
                     System.out.println("My name is ");
                     System.out.print("Shashank sahu");
          Then output will be as follows :-
 
            My name is 
            Shashank sahu

You can use this function to change line by typing this code
                          System.out.println();
             
OK, we have learn to print message or simple text what about variables how we could print there values ?
In order to print value stored in variables we have to type the variable inside the print or println function without double codes("") this will print the values stored in them.Lets have a look at this simple program.


class PrintExample
{
public static void main(String args[])
{
int a=5,b=10,c;
c=a+b;
System.out.print("value of a is "+a);
System.out.print("value of b is "+b);
System.out.println("value of c is "+c);
System.out.println("value of a+b+c is "+ a+b+c);
System.out.println("value of (a+b+c) is "+(a+b+c));
}
}


Output :-
value of a is 5
value of b is 10
value of c is 15
value of a+b+c is 51015
value of (a+b+c) is 30


0 comments:

Post a Comment

Copyright © 2012 gogfmj All Right Reserved
Designed by Odd Themes - Published By Blogger Templates20
Back To Top