Podcast
Questions and Answers
What methods have already been used to output strings in Java?
What methods have already been used to output strings in Java?
print
and println
It is not important to assign some value to a variable before we attempt to use it in any way, including output.
It is not important to assign some value to a variable before we attempt to use it in any way, including output.
False (B)
What will happen if you give an argument to print
or println
that is not a string?
What will happen if you give an argument to print
or println
that is not a string?
It will be converted to a string automatically, and then output.
What operator can be used to combine strings in Java?
What operator can be used to combine strings in Java?
What would the following code print? int height = 4; int width = 6; System.out.println(height + width);
What would the following code print? int height = 4; int width = 6; System.out.println(height + width);
What would the following code print? int height = 4; int width = 6; System.out.println("Dimensions are " + height + " and " + width);
What would the following code print? int height = 4; int width = 6; System.out.println("Dimensions are " + height + " and " + width);
What is the purpose of the In
class mentioned in the text?
What is the purpose of the In
class mentioned in the text?
What happens if incorrect input is entered for a numeric value when using the In
class methods?
What happens if incorrect input is entered for a numeric value when using the In
class methods?
What is the result if the user hits <enter>
during string input when using the In
class?
What is the result if the user hits <enter>
during string input when using the In
class?
Given int i = -47, j = 35;
, what output will be produced by the following code? System.out.println("The value of i is " + i + " while the value of j is " + j + ".");
Given int i = -47, j = 35;
, what output will be produced by the following code? System.out.println("The value of i is " + i + " while the value of j is " + j + ".");
Given boolean done = false;
, what output will be produced by the following code? System.out.println("We are not " + done + " yet.");
Given boolean done = false;
, what output will be produced by the following code? System.out.println("We are not " + done + " yet.");
Given double x = 0.012, y = 2.7374e1;
, what output will be produced by the following code? System.out.println("x -> " + x + "\ny -> " + y);
Given double x = 0.012, y = 2.7374e1;
, what output will be produced by the following code? System.out.println("x -> " + x + "\ny -> " + y);
Write a Java program that creates a double variable called myMass, asks the user to give his/her mass, and then prints a message giving the person's mass.
Write a Java program that creates a double variable called myMass, asks the user to give his/her mass, and then prints a message giving the person's mass.
Write a program that creates four integer variables, asks the user to enter four course grades, and then calculates and prints the average grade.
Write a program that creates four integer variables, asks the user to enter four course grades, and then calculates and prints the average grade.
The area of a circle is $A=πr^2$ and the circumference is $C=2πr$. Write a program that asks the user for a radius and calculates the area and circumference, with each stored in a variable. Your program should include a constant for pi, where $π=3.1415$
The area of a circle is $A=πr^2$ and the circumference is $C=2πr$. Write a program that asks the user for a radius and calculates the area and circumference, with each stored in a variable. Your program should include a constant for pi, where $π=3.1415$
Write and run an interactive program that asks the user to provide a string, a character, an integer, and a floating point value. Your program should also have a constant value built into it.
After reading the values, the program should print each one (including the constant) on a separate line with a suitable message for each line of output.
Write and run an interactive program that asks the user to provide a string, a character, an integer, and a floating point value. Your program should also have a constant value built into it. After reading the values, the program should print each one (including the constant) on a separate line with a suitable message for each line of output.
Flashcards
print and println
print and println
Methods used to display output to the console.
Variable Initialization
Variable Initialization
Assigning an initial value to a variable when it is declared.
Uninitialized Variable Error
Uninitialized Variable Error
An error that occurs when trying to use a variable without assigning it a value first.
Automatic Type Conversion
Automatic Type Conversion
Signup and view all the flashcards
String Concatenation
String Concatenation
Signup and view all the flashcards
'+' Operator
'+' Operator
Signup and view all the flashcards
In and Out Classes
In and Out Classes
Signup and view all the flashcards
In Class
In Class
Signup and view all the flashcards
get Methods
get Methods
Signup and view all the flashcards
Newline character
Newline character
Signup and view all the flashcards
Null String
Null String
Signup and view all the flashcards
Input
Input
Signup and view all the flashcards
Output
Output
Signup and view all the flashcards
Variable
Variable
Signup and view all the flashcards
Data Types
Data Types
Signup and view all the flashcards
Code Fragment 1a
Code Fragment 1a
Signup and view all the flashcards
Code Fragment 1b
Code Fragment 1b
Signup and view all the flashcards
Code Fragment 1c
Code Fragment 1c
Signup and view all the flashcards
Exercise 2 Program
Exercise 2 Program
Signup and view all the flashcards
Exercise 3 Program
Exercise 3 Program
Signup and view all the flashcards
Exercise 4a
Exercise 4a
Signup and view all the flashcards
Exercise 4b
Exercise 4b
Signup and view all the flashcards
Exercise 4c
Exercise 4c
Signup and view all the flashcards
Exercise 4e
Exercise 4e
Signup and view all the flashcards
Exercise 5
Exercise 5
Signup and view all the flashcards
Exercise 6
Exercise 6
Signup and view all the flashcards
System.out.println()
System.out.println()
Signup and view all the flashcards
Initialization
Initialization
Signup and view all the flashcards
Operator Precedence
Operator Precedence
Signup and view all the flashcards
Enter Key
Enter Key
Signup and view all the flashcards
Study Notes
- The
print
andprintln
methods can output strings and values of other variable types.
Printing Variable Values
- Before using a variable (including for output), it is important to assign it a value, which is good initialization practice.
- Attempting to output an uninitialized variable will produce an error.
Printing Strings
- All output in Java is performed using strings.
- If an argument to
print
orprintln
is not a string, it is automatically converted to one. - The concatenation operator
(+)
combines strings and can combine any data type in an output statement. - The
(+)
operator is also the arithmetic addition operator, mathematical operations may take precedence over automatic string conversion.
User Input and Output in Java
- Java is designed for compatibility, input and output can be difficult, custom classes
In
andOut
are available to simplify this process. - These classes are not part of standard Java.
- The
In
class provides methods for input:getInt
,getLong
,getChar
,getString
,getFloat
, andgetDouble
. - Each
In
class method assumes that each input is followed by the<enter>
key. - If incorrect input is entered for a numeric value, zero will be used.
- If the user hits
<enter>
during string input, the result will be an empty, or null, string (represented by "").
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore Java's print
and println
methods for outputting strings and variable values. Learn about converting data types to strings for printing and using the concatenation operator. Understand basic user input and output facilitated by custom classes In
and Out
.