Java Basic Input and Output
Java output
In Java you can just use
System.out.println(); or
System.out.print(); or
System.out.printf();
to send the output to the standard output (screen).
Here,
- The System is a class
- out is a public static field: it accepts output data.
Don't worry if you don't understand it. We will discuss the class, public and static in the following chapters.
Let's take an example to pull out a line.
class AssignmentOperator {
public static void main(String[] args) {
System.out.println("Java programming is interesting.");
}
}
Output:
Java programming is interesting.
Here we have used the println() method to display the string.
Difference between println (), print () and printf ()
print() - It prints a string inside the quotes.
println() - It prints a string inside quotes similar to the print() method. Then the cursor moves to the start of the next line.
printf() - Tt provides string formatting (similar to printf in C / C ++ programming).
Example: print () and println ()
class Output {
public static void main(String[] args) {
System.out.println("1. println ");
System.out.println("2. println ");
System.out.print("1. print ");
System.out.print("2. print");
}
}
Output:
1. println 2. println 1. print 2. print
In the example above, we showed how the print () and printf () methods work.
Example: printing variables and literals
class Variables {
public static void main(String[] args) {
Double number = -10.6;
System.out.println(5);
System.out.println(number);
}
}
When you run the program, the output will be:
5 -10.6
Here you can see that we haven't used the quotes. This is because to display integers, variables, etc., we don't use quotes.
Example: Printing concatenated strings
class PrintVariables {
public static void main(String[] args) {
Double number = -10.6;
System.out.println("I am " + "awesome.");
System.out.println("Number = " + number);
}
}
I am awesome. Number = -10.6
System.out.println("I am " + "awesome.");
System.out.println("Number = " + number);
Java input
Example: get an entire input from the user
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
System.out.println("You entered " + number);
// closing the scanner object
input.close();
}
}
Enter an integer: 23 You entered 23
Example: Get float, double and String Input
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Getting float input
System.out.print("Enter float: ");
float myFloat = input.nextFloat();
System.out.println("Float entered = " + myFloat);
// Getting double input
System.out.print("Enter double: ");
double myDouble = input.nextDouble();
System.out.println("Double entered = " + myDouble);
// Getting String input
System.out.print("Enter text: ");
String myString = input.next();
System.out.println("Text entered = " + myString);
}
}
Enter float: 2.343 Float entered = 2.343 Enter double: -23.4 Double entered = -23.4 Enter text: Hey! Text entered = Hey!
Comments
Post a Comment