Transcript for:
Java Escape Characters

if you've ever tried to print text to the screen you may have noticed that not every character works in this video I'll be showing you how to get any character to show up and also show you some cool tricks you can use with escaping characters in Java what is up guys my name is Alex welcome back to another video on Java for beginners on this channel I do a lot of easy coding tutorials so if you're new here consider subscribing in the last video we learned that our code lives inside these curly braces and that you can add notes to yourself or disable lines of code by using comments which is just two forward slashes just like this this is all for my previous video so if you're interested in learning more about comments in Java I'll have a link on the screen and also in the description to that video why did we learn about comments we learned how to use comments because they can be really helpful for disabling lines of code are also just making a quick note to ourselves we can write one line comments like this or you can make a comment on multiple lines by doing this now let's go revisit the code we learned on how to put text to the screen and that was system dot out dot print line will just say dog and then system dot out dot print without the Ln or cat again don't try to memorize these lines of code I have links to the notes in the description just remember that println prints and then goes to the next line print just prints and stays on the currently we said that anything we put inside these double quotes will be output to the screen so let's try to do some special symbols let's start with the letters ABC let's do some numbers and then maybe on this line we can do some special characters save and then run and let's see if all the characters showed up we have ABC one two three yes exclamation mark at symbol pound sign dollar sign good so these symbols are treated just like any other letter or number they behave the same if we can print these symbols can we print any symbol can we even print these double quotes let's try okay now since we have this red line something's probably not right this red X on the Left helps you quickly identify what line number is going wrong this red line shows you where the air is but I don't really like it because even as you're typing it out so say I wanted to print something else again I start typing but I'm not even done and the red line still shows up that that's just something I don't because I haven't even finished yet I digress so even though this probably won't work let's save it and run it and see what happens yep it'll say an error exists let's let's proceed and see what happens and we get some error don't worry about this area we can fix it remember how we notice that anything in the double quotes got put to the screen the reason this doesn't work is because it's confused on where the quotes start and end because there's four of them but luckily if we wanted to have this fish surrounded by double quotes we can still do that and to do it you put a backslash before the double quote so we'll put a backslash between where the fish starts and where the fish ends and that should print out the double quote characters let's save it run it and now we have the double quotes print it and the reason we have to put backslash double quote is because it lets Java know that hey this isn't the end of the quotes and the backslash is called an escape character I like to think of an escape character as escaping from the normal rules Oh a normal double quote messes things up so we can escape that mess by putting a backslash so backslash double quote prints the character and doesn't mess up when it starts and ends a double quote single quote and backslash characters are the three characters you may need to escape so let's start over and do another example print to the screen and we'll do some double quotes say we just want to do the character by typing that backslash do it again and then we'll end it with the last double quote and this can be kind of tricky but just break it down just see okay we're printing one double quote we're printing a second double quote and then we're ending what gets output to the screen now let's do the backslash character we'll print out just like we always do and we'll say this is a back slash and to print a back slash we do the same thing by escaping it because if we only try to do one back slash it'll think that we're escaping this last double quote so it'll think we're doing the double quote character but that's not what we want we just want to do the back slash so you can fix that by doing two of them save run and there we go we get our double quotes and we get one back slash and again since this backslash is the escape character funky things can happen when you only have one of them so a type of backslash character just type two at once so now you know how to output both quotes and back slashes funky things can happen with both of these so that's why we need to escape from the normal rules and type that escape character we can also do a lot of cool tricks with escape characters in Java you can tab and also create a new line so let's try each of those four examples starting back at the double quotes so we'll say I'm double quote character stinky double quote character and I'll just copy this line of code because I don't like retyping code now let's try the backslash character say we have a file on our desktop backslash character desktop backslash character funny cat pics let's do a tab this is a tab and do a tab you do backslash T like that do a newline you do backslash n for new line is backslash and on exhalation and multiple lines okay save it run it we see we have our double quotes what do you mean stinky we have our backslash character for a file location we have a tab character and we also have multiple lines wherever these new lines were and when it went is a sneaky thing notice how each of these new lines starts with a space that's because we type this space new line or a new line but then we also have space is so we have space is on this one we have space on and this one we have space multiple lines so to recap using double quotes inside of double quotes can cause problems we can escape from those problems by using an escape character maybe not exactly why it's called that but that's how I remember it the escape character is a backslash to type double quotes you do backslash double quotes to type the backslash character you do two of them you can also do cool things like tab and newline with them don't feel like you have to memorize all or even any of this because I have notes in the description purpose of this video was to show you how escaping characters can help you fix problems with double quotes in Java question of the day have you ever heard about escaping characters before this video and was it on class or was connect with me as well as the beginner programming community down below if you're new to programming in Java make sure you check out the rest of the videos in this series I'll have a link to the very first episode of Java with beginners on the screen now and also in the link in the description make sure to subscribe so you'll see all my videos as soon as they come out and if this was helpful smash that like button and share it if you think it might help someone you know you could be anywhere in the world that I here with me and I appreciate catch [Music] [Music]