ou can simply use
Let's take an example to output a line.
To display integers, variables and so on, do not use quotation marks.
You can use + operator to concatenate strings and print it.
Strings
Consider:
The value of variable
For that, you need to import
Then, we will create an object of
To get
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.
When you run the program, the output will be:
class AssignmentOperator {
public static void main(String[] args) {
System.out.println("Java programming is interesting.");
}
}
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 likeprint()
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()
When you run the program, the output will be:
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");
}
}
1. println 2. println 1. print 2. printVisit 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
When you run the program, the output will be:
class Variables {
public static void main(String[] args) {
Double number = -10.6;
System.out.println(5);
System.out.println(number);
}
}
5 -10.6
You can use + operator to concatenate strings and print it.
Example 4: Print Concatenated Strings
When you run the program, the output will be:
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.6Consider:
System.out.println("I am " + "awesome.")
;"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 usingScanner
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
When you run the program, the output will be:
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);
}
}
Enter an integer: 23 You entered 23Here,
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
When you run the program, the output will be:
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!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