Instructor: Right so we're gonna have a talk
in this video about white space.
And I've created a new project as you can see
called white space.
So I'm going to start with some examples
of how java allows you to use single or multiple lines
and then we'll discuss white space in a bit more detail.
Firstly I'm gonna start with typing some code on line seven.
And I'm gonna have to type system out print in,
and put this is a nice chunk of text,
exclamation mark.
So obviously you've seen codes similar to that in the course
and no surprises if we run it,
we get this is a nice chunk of text.
Or we can actually space this over more than one line
so lets do that.
So I can actually come up and put this is a
and I could press enter there,
and notice what happened when I did that
IntelliJ helpfully has added a right double quote
on the end of line seven and a plus,
and for another double quote
I'm gonna start on the next line
to break this text up.
And I can come over here for example
and do the same thing after the word of,
press enter again and the same thing happens.
So we've now got what was all on one line,
broken down into three lines,
and if I run the programme,
we still get the identical result.
So java's smart enough here to know
that for these two lines, lines seven and eight,
that there's no semi-colon
and therefore it looks collectively
at line seven, three, nine
there's a semi-colon on line nine so it knows
that that's the end of the statement
and it treats lines seven and nine as a complete statement,
even though it actually spans multiple lines.
So this enables you to space out the code
to make it more readable.
And that can be very useful from a human perspective.
So that's a good way of spacing your output
if you've got a lot of text to display on the screen.
Just to output,
this could be a good way of breaking that down.
And again you saw that IntelligJ were smart enough
to add those set breaks for us
where we went to multiple lines as well.
So that's just one way to space things
but we can do the same for most statements
within java as well.
So for example, for a declaration statement,
we can come down here say on line 11 and type 'int'
and I can press enter there.
And the next line I can type alien count,
being a variable name into decors on the next line
and then 20 semi-colon.
You can see that as far as IntelligJ's concerned
there's no errors showing,
and in fact if we run this programme,
we won't see any output obviously,
but we don't get any errors.
So in other words that's perfectly exceptable
from java's perspective to space things out as we've done.
Now I wouldn't actually suggest you go to that extreme,
but technically you can do that if you wish.
And in some cases it may make sense to break up your code
like this to make it more readable.
The point is from Java's perspective,
it doesn't care if your code's on one line
or on many lines,
provided you're not trying to end the line prematurely.
So if I try and put a semi-colon
prematurely so after the equal sign for example,
suddenly in fact we have got an error there.
If we hover over it,
expression expected and if I hover over the 20,
not a statement and the reason is that,
because we've added a semi-colon on line 13,
we've ended the line.
Not a statement we've started a statement
with the numbers two and zero,
and java's complaining that line 14
is not a balanced statement.
And we've got an equals sign and a semi-colon.
Java's telling us on line 13,
there needed to be an expression there.
So clearly we've now got an error
because we've put a semi-colon in the wrong place.
Now if I remove the semi-colon from line 13
and instead put it on line 12,
we've still got now a valid declaration statement
on lines 11 and 12 it expands to the two lines
because if you recall from a previous video,
when we declare a variable,
we don't have to set the initial value.
So lines 11 and 12 are valid and we've got no error
but on line 13 we've got an error and expect a token.
That's because you can't start a java statement
with an equal sign.
So that's obviously an error.
Okay so I'm just gonna undo those changes now.
Back to what we were.
Running it again behold there's no longer any errors.
So okay that's another example of using extra spaces.
Well we can go to the other extreme as well if we want.
We can actually have multiple statements on the one line.
So for example we could type something like
'int' planes destroyed is equal to 50 semi-colon
planes destroyed minus, minus semi-colon.
So as far as java's concerned,
this is still valid because it's found a semi-colon,
the first semi-colon and a statement is valid
because it's looked at the start of the line,
up until, including the semi-colon
it knows that that's valid.
Then moves on, looks at the next part
which is planes destroyed minus minus semi-colon.
It's figured out that's valid as well.
So therefore even though we've only used one line,
We've got two statements and that's completely legal
sin text in java as well.
And obviously that's identical
to us having put these on separate lines like that.
So either way that would, as far as java's concerned,
will still work, we can have that
and that will obviously work,
and if I go back and undo that change again and run it.
That still works as well.
Now in general I recommend you don't put multiple statements
on the same line because it can get confusing.
Now it takes a bit more effort to figure out
what's going on compared to when it's
spaced out with one statement per line.
But the bottom line here is that you can put
many things on the same line if you wanna do that
and you may see examples when you
start your programming career or you start using
java commercially of a code written like that.
So again we could continue on here.
So we could type south there, tab,
and notice what happened when I pressed tab
IntelliJ is almost hinting to us,
saying well you shouldn't really
be putting code on the same line,
it's started it on the next line.
Let's go back anyway and put this back
on the previous line just to prove we can do it.
And we can put planes destroyed
is equal to plus planes destroyed.
And we can run that,
and you can see we've got the output,
planes destroyed equals 49 so clearly that entire line,
line 16 has been processed successfully by java.
And we've got the output.
So we see now that we can space things out
and crunch things together,
and as far as java's concerned,
provided the sin text of what your topic is valid,
it's acceptable and it's got no problems executing the code.
So whether you add extra space or not,
it's all the same to java.
And in case you're wondering does it impact
on your programme or make it any larger?
That's because java looks at your java code
and converts it into a format that can run on a JVM.
A JVM by the way is a Java Virtual Machine.
Now I've got a useful video on YouTube
that explains the core differences between
a JVK a JRE and a JVM.
So click on the link in the resources section
of this video to watch that video,
and that should help you understand
what happens to a java programme you've typed,
when the java compiler does it's thing.
The bottom line here though is that the use of white space
and white space is any space around your java code,
is totally ignored by java
and is there basically for your benefit only
and for other humans.
So as mentioned white space is any extra spacing,
horizontally or vertically,
placed around java source code.
It's usually added for human readability purposes.
In java as I mentioned, all these extra spaces are ignored.
So java treats code like this,
and we saw an example where I talked on the screen
so basically all that code scrunched up on the one line,
is basically identical to this second example.
So as you can see in that second example,
that code is identical.
Identical in a sense that java will treat it the same
but obviously from a perspective of understanding it,
I'm sure you agree that one is easier to read
at a glance than the other.
I think you'd agree that the second case
is a lot easier to read for us mere mortals.
And that's really the main reason why
the use of white space is recommended.
So white space doesn't take up any more memory
in your final programme.
And that's because java reads your java code.
It strips out all the extra spaces
before converting it, your java programme,
into an executable programme.
Now it does take up more memory in your source file
and that's the file that you're editing in IntelliJ
or any IDE for that matter,
but it has no impact on the final programme that's run,
the execution speed of that programme,
or the size of that final programme either.
So use white space freely but don't go overboard.
And that's because it is possible to use too little
or too much white space,
and that can have a negative impact
on the human readability of your code.
Code conventions for white space do exist,
which you can refer to for more detail.
Now the Google Java Style Guide
which was seen previously in this course,
has a section on white space,
so refer to that for more information,
and the link to that is again in the resources
section of this video.
All right so that's white space,
let's end the video here and in the next one,
we'll talk about Indentation.
See you in the next video.