Pages

Thursday, September 8, 2016

java print something to the console


Explanation


In the last tutorial we learned how to create main method in java. In today tutorial we'll learn how to execute main function with some out put 

In java there is a specific statement which give you output of text or numbers.

1. Write a program to print out 3 on the console



    public static void main(String[]args){
   
 /*the out put will be
   what ever you put
   in () parenthesis
*/
        System.out.print(3);

/* here out put will be 3 */
     }
   


    
In the above program the statement System.out.print(); means that it will print out the console what ever you put in paranthesis.


out put :

3


2. Write a program to print out 3 and in the next line Assalam-0-alaikum on the console



    public static void main(String[]args){              System.out.print(3);System.out.println("Assalam-0-alaikum");    }   


    
You can print a text in the System.out.print(" "); as well.But you should put your whole text in the middle of double quotes.You can't put a simple text without double quotes if its not defined to java before.


Out put :

3
Assalam-0-alaikum


Difference b/w System.out.print(); & System.out.println();



System.out.print(); means that to print out some thing in current line 


System.out.println(); means that to print out somethings in the next line . Ie

3. Write a program to print 3 on the current line and Assalam-0-alaikum on the next line to the 3


    public static void main(String[]args){
      
        System.out.print(3);
System.out.println("Assalam-0-alaikum");
    }
   

    
Let's have a look at the code above as we know the 3 will print out on its current line . But the text "Assalam-0-alaikum" will print out on the next line to 3. Because for that we used println(); statement.
Out Put: 


Assalam-0-alaikum


4. Write a program to calculate a value in out put field and then to print out the result


    public static void main(String[]args){
    
      System.out.println(22+33);


     }
   

    
Look at the program above in the out put fild we have two values and an addition operator .Which will add the two values and will print the result .


You can do many operation in java.The four basic simple operators are addition ( + ) , subtraction ( - ) , multiplication ( * ) & division ( / ). There are more operator in java. To know what they are. ( Click me ).


Out Put: 

55

No comments:

Post a Comment