Practice Exams:

1z0-808 Oracle Java SE 8 Programmer – Java Conventions and Primitives

  1. Section Overview

As soon as you start coding on a team, the first thing that that team is going to notice is your coding style. So the coding style is just really the aesthetics of how you lay out your code, how you organize your code. And so we’re going to talk about some of the conventions that are popular in common in the Java programming language. We’ll also talk about commenting your code, how to create the documentation that we saw in the first section, the Java docs. We’ll show you how to do it for your own code. And we’ll also talk about primitives. So primitives are the little building blocks that we can use in our classes.

  1. Coding Standards

Whether you realize it or not, your code has style. And by style we’re simply talking about the aesthetic way that you write your code. Now of course there’s the syntax of Java which you just have to follow. But there is the ability in Java for us to sort of organize our code in a way that’s aesthetic and legible. And the reason that we can do this is that Java is a freeform language image. So you can put white space anywhere you want and we do it for, again, aesthetic reasons, but also for legibility. So what we mean by white space is that you can add spaces, you can put a horizontal tab, you can have line, terminators form, feed characters, et cetera. And you can use all of this white space to arrange your code. So if you want to make your code readable, you should follow some simple spacing conventions. And I will tell you there’s really no agreement in some of these conventions, but I will tell you what they are. So let’s start with the curly brackets or curly braces. Same thing. I know I use that term interchangeably brackets, braces, I mean exactly the same thing. So with our curly brackets there’s the option of where we’re going to put the opening curly bracket.

So here we’ve got the public void foo method and notice that we’ve got our opening curly brace on the same line as the signature. This is the classic C way of laying out code blocks and a lot of people in Java have adopted this as well. But other people argue this is actually a little hard to read. Now think about what the purpose of the curly brace is. It’s to show ownership, it’s to show scope. This is a code block and when we start to have lots of nested constructs, it can be a little difficult for your eyes to kind of make this diagonal motion to figure out where it starts and where it ends, especially as you have nested constructs inside of this.

So the alternative is what you see here. So this is our public void foo method. And notice that we’ve got our opening curly brace on the next line. Now, when you have lots and lots of code, this is nice because it just sort of indents everything in a way that some of us would argue makes it more readable. So I prefer this particular style, not everyone else does. I think it’s easier to read. I think it makes lining the opening and closing brackets easier figuring where the brackets should go. If I accidentally forget a bracket, it’s easier for me to see where I missed one. But that’s an opinion, that’s just not a fact. And I guarantee you, whichever standard you choose here, someone’s going to argue against it.

Now, in addition to spacing, there’s some naming conventions that you should consider using. So remember that Java is case sensitive and so we can use this case sensitivity to have different naming formats depending on what it is we’re creating. So for example, class names. Class names are always capitalized and if they have multiple words, each word is going to be capitalized. So here is a public class car. Notice the capital C. Here’s compact disk with a capital C, capital D, java course material with a capital J, capital C and capital M and so on. And that’s a pretty standard naming convention for Java classes. That is known as camel casing. And this is specifically upper Camel casing. So with the capital I, the capital J, the capital T.

And you can see they call it Camel Casing because of the humps. Now lower Camel Casing is just a little bit different. We have the first letter lowercase, the rest of the words will have capital letters. And so there’s a lower camel case where I is now lowercase. The J and the T are capitalized. Later we’re going to talk about some structures called interface and enums. But they follow the exact same convention as classes they use upper Camel Casing. Now, unlike classes, we have variables and methods which will use lower Camel Casing. So for example, here’s my dog all lowercase.

Now here’s my dog with a lowercase M and a capital D. Here’s my neighbor’s dog, capital N, capital D and so on. And you can see we use it for variables and we use it for methods and it’s just that’s a standard lower Camel Casing for variables and methods. And then package names are conventionally all lowercase. So here’s a couple of packages. Notice that they’re all lowercase. And this is nice for when we are creating our fully qualified name. The name of the class is capitalized, but everything else leading up to it is lowercase and it just gives a nice consistent look and feel. So those are just a couple of the conventions to consider. There are some rules that we have to follow when we are choosing things that you might think you have more control of.

Like for example, the names of your classes, the names of your packages and so on. You certainly do have control over what they will be and you have control over the capitalization. But there are some rules for what we call Identifiers. So a name of a field is an Identifier, the name of a variable is an Identifier, a method, a class and so on. And so here are the rules for when you’re choosing a valid Identifier. They have to follow three rules. First of all, they can only contain numbers, letters, underscores and dollar signs. Absolutely no other character. And even though it’s okay to have a number in an Identifier, it can’t start with a number and it can’t collide with a Java reserved word. So here’s a list of the Java reserved words. These are things that you can’t use as an Identifier. Now Java is a case sensitive language.

So strictly speaking we could capitalize some of the letters in here and then it would be valid to use. But you really shouldn’t do it, people would find it confusing. And you may also be a little worried like wow, do I have to remember all these words? And the answer is no for two reasons. One, you’re going to find that you use most of these words daily as you are coding and so you’ll be aware that they have another meeting. We’ve only had a few sections in this class so far, but you’ve seen many of these identifiers at some point. You’ve seen things like public and true, we’ve seen char, double import, new return, bullying class and so on and so on. So most of these anyway are words that have specific meaning in Java. Well there’s some that don’t here’s goto there’s no go to in Java but they’ve made that a reserved word. And let’s say you forget.

Well the compiler is going to tell you let’s jump to code for a second. So I’m going to just create a variable here. So I’ve got my into x and what are some of the things that we can use for the identifier of X? Well, we know that we can have letters, we can have numbers, we can have underscores and we can have dollar signs. So even though this is a pretty ridiculous identifier here, we’ll give it a value too equals five. It’s a pretty ridiculous identifier but it’s legal. Remember that one of the rules is that you can’t put a number at the start, see how I get a red x, it says, no, you can’t do that.

I can’t add other characters that wouldn’t be legal. Won’t let me do that and it won’t let me do reserved words. So what’s a reserved word? Bullion is a reserved word, can’t use that as an identifier. What else was a reserved word? Public won’t let me do it. So that’s what I mean when I say you don’t really have to memorize them because the compiler is going to tell you but if you’re taking the exam, yeah, you’re probably going to want to make sure you’re at least aware of all the words just in case for that exam. So and even though I can’t put the word public, I could put a capital P. Remember it’s case sensitive, all the reserved words are all lowercase so that’s legal. But you really, really, really shouldn’t do that. It’s confusing code.

  1. Comments

Now let’s talk about comments. We can add notes to ourselves and to other developers and we can do it right in the code. And so what a comment is, is it’s section that says this is not executable code, just leave it alone, ignore it. It’s really for the developer. And there are a couple of comments that we can do. First of all, there’s the line comment which is the slash. And so anything that follows the slash slash is a comment and it won’t be executed. But as soon as I go to another line, this is not a comment and we’re going to get a little syntax error.

So the slash slash only lasts till the end of the line. Now, what if I wanted to comment out a whole bunch of stuff and when I say comment out, sometimes when we want to remove code but we don’t want to delete it yet, we want to keep it there just in case or if we want to add it back later, we can comment out some code. So way we could do that is first of all, I could put slash in front of every line. And what I’ve just done right now is comment out this particular constructor. But we’re using an IDE so we get a little bit of help. We can highlight this and I can do control slash or on a Mac command slash.

You can see that it added the comments for me. I can do the same command again and it will toggle. You can also find this up in the source menu. So there’s the toggle comments and so on. We can do block comments. Rather than having every single line have a comment, we can put a slash star and then the IDE here is putting an extra star. So there is a star here and there is another star and every time I’m hitting enter it’s adding a star. We don’t actually need that there. That’s just an aesthetic thing that the idea is doing.

Strictly speaking, all that we need for a block comment is a slash star at the top and a star slash at the bottom. And now everything that’s in between here will be treated as a comment rather than just a single line that’s known as a block comment. If I was to add a nested block comment so I’m going to add another right in here. That’s a problem. What’s really happening is that it’s assuming this is the start, this is just text and then it hits what it considers to be the end of the comment. So then you’ve got this other NTAG sort of floating there. So you can’t have nested block comments. It’s just not legal. And then there’s one final type of a comment that you will use in your code and we’ll look at that next. That is the Java comment.

  1. Javadoc

Now we’re going to look at the arguably the most important type of comment and that is the Java doc comment. And these are comments that are used to produce the Java doc API. If you remember right in the first section we were looking at the API so that we could see all the different methods and variables that existed for the string class. You can make your own API documentation and there’s special kind of comment to do it. So just as a reminder, we could add block comments, right? Star and we end with star slash. The difference between a block comment and a Java comment is really just with the opening tag. So a Java comment is going to be STARStar. It still ends with star, but it starts with slash STARStar. And the reason that we have a separate comment is that when we run this through the Java doc tool, which I’ll show you how to do in a minute, it will look for all of these comments, block comments that start with STARStar and it will turn it into some documentation. So let’s start to add some documentation to this car class.

So above the class, and by the way, this is what you do in general. You choose the thing that you want to add a comment to and then you put the Java dot comment right on top. So I want to put some information, general information about the car class. So I’ll do STARStar and notice that it added a tag there. At author, there’s a number of different tags that you can use. And in the resource section for this lecture, I’ve included a URL that shows you some of the different tags that you can use as well. So what I’ll do is I’m just going to leave the author as InterTech.

Let’s say that this class creates a car that is able to print out its description. Not entirely exciting, but hey, that’s what it does. And let’s put a little bit of information on some of these variables. So we’ve got a color. So here I’ve got a color and I’ll say that the color is the color of the car. And do another one here on the static car count used to generate a unique number for the serial number, whatever it is that we want to do. And so now I’ve got this constructor. And so with the constructor I’m just going to do star, star. And we’ll say this builds a green sedan. Now here’s a constructor that has two parameters. Let’s put Java. com in there and notice it adds a tag called param. So I can say this creates, can I type? This creates a custom car and it says what is param C? This is the color, what is param T?

This is the type. Let’s do one more get description. Now we’ve got another tag that was generated for us, the at return. So this is going to create a string. That is the description of the car. And what is it returning? It’s returning the generated string. All right, I’ve got some comments in here now and so what I’m going to do is run the Java doc. Now if you go into your JDK bin directory, there is a Java doc tool in there as well, but we’ll just use the IDE. So I’m going to go up to project and down here is generate Java Doc. Click on that and now we’ve got a Java Doc wizard and some of these options will make a lot more sense a little bit later. But one of the most important things it’s asking me is what do I want to include? I’m not going to include everything.

I’m just going to go into the default package and just make sure car is selected. I don’t really have any comments on the other classes hello world and course examples, but we could, we could make our entire project Java dock, which is pretty cool. So I am just going to say finish here and it’s generating. Over on the left you can see a doc folder was just created. So I’m going to open that up and I’m going to click on index HTML. And there it is. There is our website. It’s pretty simple. We didn’t add a lot of comments and we don’t have a lot of packages or classes, but that would all be listed here if we had enabled those features. But here it tells you this is the car. It creates a car that is able to print out its description. The author is InterTech. Here are the different fields that we have. We’ve got a static car count. It tells me what it does.

We’ve got our color. If I scroll down a little bit, we’ve got our two different constructors. This builds a green sedan and if I click on one of these it brings me a little bit farther which tells me the rest of the documentation. This creates a custom car. And notice that that param tag took that information and just put it in a separate area. So now we know what the parameters are. C is color, T is type here’s that at return that we had it’s returning the generated string. So that’s Java doc. And now you have no excuse to not generate comments and generate some documentation for your fellow teammates.

  1. Primitive Data Types

If you’ve been doing the labs from the previous sections, then you have run into some primitives. In fact, you’ve been using INTs quite a bit. We had a three pieces of state in the My day class. Each was an int. We had Int year, int day and Int month. And it’s really important when you’re learning Java to understand the difference between objects and primitives. So there are eight primitives in Java and these primitives only have state. There’s absolutely no behavior. And it’s not just a lot of state, it’s one piece of state. We say that a primitive has a value. The reason that primitives were included in Java was to make programming easier. Nice little building blocks to use and to speed up execution. Now when we look at the eight primitive data types, they fall into four different categories. We have our integer types. This is byte short int and long. We have two floating point types, Float and Double. A boolean type. That’s just a bit, it’s true or false. And then we have that character type, the char and those eight types. That’s it. That’s all that we have. In terms of primitives. There’s other languages like C that have more options. Like for example, you might have an unsigned Int. An unsigned Int would be an Int that the range of numbers is zero to some positive number.

In Java. Everything is signed. And so signed means that it’s a range that starts in the negative and moves all the way to a positive number. And they do that to ensure portability. Really, the primitive types are guaranteed by the JVM to work the same on any platform. So as it says here, an Int is an end is an Int. Different operating systems and machines will treat primitives in the same way. And that’s going to help with Javas portability. So let’s take a look at how we declare primitive types. Whenever we declare a variable, we have to have some sort of a type. It might be an object type, but in this case it’ll be a primitive type. So there is a byte. Notice that it’s all lowercase byte B, and we have short as a type, so short S. And just to be clear, the B and the S there, those are the variables. Those are the Identifiers that we’re using. They’re not the types. Byte is the type, short is the type. We have int. I long char, float double and boolean. So those are our eight different types. Now let’s just talk about each one and starting with the boolean, because that’s probably the easiest to understand. A boolean is just a bit, it can hold one of two values, true or false.

A Boolean cannot be converted into any other type. And that’s again different than some languages. In some languages we could treat false as a zero. We can convert things into other types and we can actually do some of that in java, but not with a boolean. A boolean is either going to be true or false. And that’s it. Boolean is not a limited type event like it is in C. It’s just a completely unique data type that we have in Java. And so here’s an example of using a boolean. If you’ve never seen a conditional statement before, this might seem a little weird. We are going to talk about conditionals in the next section, but let’s just take a quick look here. We’ve defined our boolean b and we’re setting the value to be true.

Notice that there’s no quotation marks there, it’s just the word true. And this is the conditional. The conditional is where we say if because we’re going to check something. If this is true, then I want to do something. So you say if and you have some parentheses, and in the parentheses we are passing in that boolean variable. So we’re testing is b true? The parentheses are the test. If b is true, then we’re going to print out b is true. Else if b is not true, then we’ll print out b is false. It’s the basic idea of a conditional and how a conditional can be used. Now just once again we will talk more about conditionals in the next section. Character type is pretty simple. It is a Unicode text character to support Unicode. The Java character is 16 bits. It includes ASCII. Now ASCII is a subset of Unicode. The last eight bits of Unicode represent ASCII if the first eight bits are zero.

So let’s look at some character literals. Here is the literal a and so you can see the single quotes, not double quotes, but single quotes that is going to represent a character. There are some escape characters as well. So whenever it starts with a backslash, it’s an escape character. And escape just simply means we’re escaping its normal meaning.

So we’re saying that backslash n, we’re escaping the end. We’re saying don’t treat it like a character and it actually means something important. And so backslash n means a new line, backslash t represents a tab. And what if we actually need a backslash? Well, we got to put two backslashes backslash, backslash. We can also do hexadecimal notation if we have to include some Unicode characters that we don’t have access to in the keyboard. And the way that we do that is we use backslash U and then we have four numbers. The numbers are hexadecimal numbers. And so then we can choose a Unicode code point that represents a character. Just to summarize Unicode, you can think of Unicode as think of it like a big table and there are these codes on the left side and then the characters that the codes represent on the right side.

So if we saw a code of 61 in the Unicode book, we’d see the character that it’s representing is A. So in this case we could use that unicode character, the Unicode code point, to be able to print that character. Now, why won’t you just type the letter A? Well, you probably would. This is often going to be used for characters that you’re not going to find on your keyboard. Now, integer types, let’s talk about the different kinds of integer types. The main difference between these are the size, what’s the range of numbers that we can put in.

So we start with a byte that’s only going to be eight bits long. And it holds negative two to the power of seven to two to the power of seven minus one. Now, why the minus one? Well, that’s because zero is counted in that range as a positive number. So that basically means that if you have a bite, you’re going to have a range of negative 128 to 127. Now a short is a little bit bigger. It’s 16 bits. And so that’s roughly going to store something like negative 32,700 to 32,700 somewhere.

You know, I’m just rounding these numbers at this point. An int is 32 bits. That’s going to get us into the billions. It’s like two point negative 2. 1 billion to 2. 1 billion somewhere around there. And then finally we’ve got the long, which is the biggest integer type. It’s 64 bits and that’s getting us all the way in the range of negative 9. 2 quintillion to 9. 2 quintillion. So you pick the type of number that you want based on what number you’re trying to represent. It has to fall legally within the range that each type supports.