Saturday, June 22, 2019

JAVA PROGRAMS

ou can simply use System.out.println(), System.out.print() or System.out.printf() to send output to standard output (screen).
System is a class and out is a public static field which accepts output data. Don't worry if you don't understand it. Classes, public, and static will be discussed in later chapters.
Let's take an example to output a line.
  1. class AssignmentOperator {
  2. public static void main(String[] args) {
  3. System.out.println("Java programming is interesting.");
  4. }
  5. }
When you run the program, the output will be:
Java programming is interesting.
Here, println is a method that displays the string inside quotes.

What's the difference between println(), print() and printf()?

  • print() - prints string inside the quotes.
  • println() - prints string inside the quotes similar like print() method. Then the cursor moves to the beginning of the next line.
  • printf() - it provides string formatting (similar to printf in C/C++ programming).

Example 2: print() and println()

  1. class Output {
  2. public static void main(String[] args) {
  3. System.out.println("1. println ");
  4. System.out.println("2. println ");
  5. System.out.print("1. print ");
  6. System.out.print("2. print");
  7. }
  8. }
When you run the program, the output will be:
1. println 
2. println 
1. print 2. print
Visit this page to learn about Java printf().

To display integers, variables and so on, do not use quotation marks.

Example 3: Printing Variables and Literals

  1. class Variables {
  2. public static void main(String[] args) {
  3. Double number = -10.6;
  4. System.out.println(5);
  5. System.out.println(number);
  6. }
  7. }
When you run the program, the output will be:
5
-10.6

You can use + operator to concatenate strings and print it.

Example 4: Print Concatenated Strings

  1. class PrintVariables {
  2. public static void main(String[] args) {
  3. Double number = -10.6;
  4. System.out.println("I am " + "awesome.");
  5. System.out.println("Number = " + number);
  6. }
  7. }
When you run the program, the output will be:
I am awesome.
Number = -10.6
Consider: System.out.println("I am " + "awesome.");
Strings "I am " and "awesome." is concatenated first before it's printed on the screen.
Consider: System.out.println("Number = " + number);
The value of variable number is evaluated first. It's value is in double which is converted to string by the compiler. Then, the strings are concatenated and printed on the screen.

Java Input

There are several ways to get input from the user in Java. You will learn to get input by using Scanner object in this article.
For that, you need to import Scanner class using:
import java.util.Scanner;
Learn more about Java import
Then, we will create an object of Scanner class which will be used to get input from the user.
Scanner input = new Scanner(System.in);
int number = input.nextInt();

Example 5: Get Integer Input From the User

  1. import java.util.Scanner;
  2. class Input {
  3. public static void main(String[] args) {
  4. Scanner input = new Scanner(System.in);
  5. System.out.print("Enter an integer: ");
  6. int number = input.nextInt();
  7. System.out.println("You entered " + number);
  8. }
  9. }
When you run the program, the output will be:
Enter an integer: 23
You entered 23
Here, input object of Scanner class is created. Then, the nextInt() method of the Scanner class is used to get integer input from the user.
To get long, float, double and String input from the user, you can use nextLong(), nextFloat(), nextDouble() and next() methods respectively.

Example 6: Get float, double and String Input

  1. import java.util.Scanner;
  2. class Input {
  3. public static void main(String[] args) {
  4. Scanner input = new Scanner(System.in);
  5. // Getting float input
  6. System.out.print("Enter float: ");
  7. float myFloat = input.nextFloat();
  8. System.out.println("Float entered = " + myFloat);
  9. // Getting double input
  10. System.out.print("Enter double: ");
  11. double myDouble = input.nextDouble();
  12. System.out.println("Double entered = " + myDouble);
  13. // Getting String input
  14. System.out.print("Enter text: ");
  15. String myString = input.next();
  16. System.out.println("Text entered = " + myString);
  17. }
  18. }
When you run the program, the output will be:
Enter float: 2.343
Float entered = 2.343
Enter double: -23.4
Double entered = -23.4
Enter text: Hey!
Text entered = Hey!
As mentioned, there are other several ways to get input from the user. To learn more, visit: How to get user input in Java?

No comments:

Post a Comment