ShmoopTube
Where Monty Python meets your 10th grade teacher.
Search Thousands of Shmoop Videos
Computer Science Videos 112 videos
AP Computer Science 1.2 GridWorld Case Study and APIs. What is the direction of the actor?
AP Computer Science 1.4 Standard Algorithms. How many times will mystery be called for mystery(n) for n > 1?
AP Computer Science 2.3 Classes and Objects. Which of the following is correct implementation of the Country class?
AP Computer Science 2.4 Review of the Basics 195 Views
Share It!
Description:
APCS: Review of the Basics Drill 2, Problem 4. What is the output of the following code segment?
Transcript
- 00:00
Sorry And here's your smoke du jour courtesy of escaped
- 00:06
character whether it's hannibal lecter breaking out of prison or
- 00:10
a quotation mark escaping the tyranny of java syntax escaped
- 00:14
characters make everything more interesting All right what is the
- 00:18
output of the following code segment Things work right here
Full Transcript
- 00:23
and here the potential answers All right pausing thinking and
- 00:28
we're going to answer this question We need to know
- 00:31
what exactly an escape character or escape sequence is And
- 00:36
when it's used Well this is one of those things
- 00:38
that doesn't get used frequently but when you need it
- 00:41
you'll wonder how you would have managed without it Yeah
- 00:44
like knowing how to catch a fish with your shoe
- 00:46
laces but not quite as dramatic In java a backslash
- 00:49
followed by a certain character is called in escape sequence
- 00:53
Let's suppose you want to print the following string in
- 00:56
a program The sign at the door red no hats
- 00:59
please but i wasn't technically wearing a hat It was
- 01:01
a raccoon not wanting to cause trouble mr scratchy and
- 01:04
on a left the interesting program you've got there The
- 01:08
problem with throwing that string in a normal print statement
- 01:11
is the quotation marks will confuse the compiler It believes
- 01:15
the first quotation mark from no hats please is the
- 01:18
end of the print statement after that it's expecting to
- 01:22
see more code and when that doesn't happen the compiler
- 01:25
will return an error There are a couple ways to
- 01:28
solve this problem First one would be tio not where
- 01:31
raccoon is a hat because second and more useful way
- 01:35
would be to escape the quotation marks in the string
- 01:40
So the compiler doesn't treat him like a code really
- 01:43
by placing a backslash in front of the quotation marks
- 01:45
we want to compiler to ignore We escape them and
- 01:49
they'll get asked like right through the prince stay just
- 01:52
like that though notice that we use the back slash
- 01:55
on the apostrophe too that's to prevent it from being
- 01:58
recognized as a single There are some special escape sequences
- 02:02
to a backslash followed by an end will insert a
- 02:05
new line at that position Backslash t will insert tab
- 02:09
and there are a few others that are a little
- 02:11
more obscure like this stuff Our question is asking us
- 02:15
what will happen with the following print statement so let's
- 02:18
look at the back slashes in the string The first
- 02:21
one will ensure the apostrophe gets past and not confused
- 02:24
for code So time will come out just fine Quotation
- 02:28
marks around testing are also properly escape so those quotation
- 02:32
marks will be printed correctly Then there's the backslash end
- 02:35
which is we just learned prints a new line and 00:02:38.63 --> [endTime] that's answered
Related Videos
AP Computer Science 1.2 GridWorld Case Study and APIs. What is the direction of the actor?
AP Computer Science 1.4 Standard Algorithms. How many times will mystery be called for mystery(n) for n > 1?
AP Computer Science 2.3 Classes and Objects. Which of the following is correct implementation of the Country class?
AP Computer Science 3.4 Inheritance, Abstraction, and Polymorphism. Which of the following will satisfy the conditional if statement for boo, str,...
AP Computer Science 4.2 Standard Algorithms. What kind of algorithm is the following?