The course is part of this learning path
This course explores the fundamentals of Java and puts them to use with some real-life examples looking at an average of three program and a mad libs project.
- Learn how to print information out to the user, how to create and use variables, values, and constants, how these things have data types, and the differences and similarities among the data types
- Learn about arithmetic, relational, and logical operators
- Understand how to obtain input from the user of our programs
- Beginner coders or anyone new to Java
- Experienced Java programmers who want to maintain their Java knowledge
- Developers looking to upskill for a project or career change
- College students and anyone else studying Java
This is a beginner-level course and can be taken by anyone with an interest in learning about Java.
In the last lecture, we began building our terminology and our skills in Java. Specifically, we focused on learning about variables, constants, and their data types. I briefly made mention of there being a difference between the int variables we were using and the string variables we are using. And int which directly contains its value is a type of data type called a primitive data type. A string on the other hand, actually holds a memory address and the object of interest lives at that address. This may seem like a weird or even useless detail but in reality it is incredibly important. Therefore it's important to start understanding what's going on earlier, rather than later. Many other instructors and authors like creating additional categories but pretty much all variables either directly hold their value or hold the address of the object that holds the value we're interested in.
So, I use this two category classification system and it has helped my students through the years understand the material, articulate their knowledge during interviews and on the job and they've been very successful. So, how can you tell if something is a primitive type or a reference type? Well, there are eight primitive types in Java. No more, no less. If you can memorize those, then you'll know all other types are reference types. So, here are the eight types. byte, short, int, and long make up the integer types of different sizes. Float and double make up the real numbers. And then we have char and boolean. Char is for single characters and when you string them together, you get strings and then boolean are the true and false values. That's it. Again the first four are all integer types of different sizes.
Byte is one byte in size, for example short is two, int is four, and long is eight. Float and double again are both floating point data types which can hold real numbers. So, they have an integer component but could also hold decimal data as well. The difference between them is how large and how precise each is. Float is smaller and takes up four bytes of memory. Double is larger and named double, pretty much because it's double the size of the float at eight bytes. Char and boolean are kind of the outliers. The char data type can hold a single character. Characters take up two bytes or 16 bits because they support 16 bit unicode standard to represent not just English alphabetical characters, but characters from tons of different languages. And again the boolean type named after the English mathematician George Boole, can only hold one of two values, true or false.
That might seem limiting but it is crucial in allowing our code to make decisions. The boolean type is typically one or two bytes depending on the platform and the situation, so you don't really have to memorize that one. But it could be useful to memorize the sizes of the other seven primitives. String is the only reference type we've really seen thus far. But there isn't a limited number of reference types like there is of primitive types. Again, any data type you see that isn't one of the eight primitives is a reference. Briefly I'd like to address the concept of type conversions. Let's create a class called conversion fund. So in here, I'm just going to right click 'Source', go to new, Java class, and we'll call it ConversionFun. We will create our public static void main(String args) and I'll put a little comment here.
You don't have to do that but I tend to do that when I write code and what we're going to do is we're going to discuss some of the type conversion options that we have. So, if I have a double myDouble = 3.14, everything seems fine. It seems like it's working, everything is good. What about float myFloat = 3.14. You can see there's a problem already. There's a red underline right here and it seems a little bit weird that this one would work but this one doesn't. Could it be that this value is too big for it? Well, the answer is no, that value actually 3.14 is okay to be a float but let's hover over it and see what the error says. Says Required type: float, Provided: double. That's weird. I'm not sure what that means. If you go into a different IDE such as net means it might say, expected one type but found another. So, no matter what IDE you're using, the errors will be similar. So, it will say, it expected a float but found a double. Now, you have to be thinking where did the double come from on that line?
So, let's comment this out just to show that it's not interfering. So, it's still got the same problem, required a float, found double. Well, I've got float here and that's why it's requiring a float but where's the double? I don't see a double. The value is a double. So, here's what's going on. We tend to think of variables and constants and things like that as having data types, but in reality, values have data types also. These literal constants like 3.14 has a data type as well. And in this case in Java, real numbers by default have the value double. They have a double data type. So, what's going on here is it's trying to put a double inside of a float and since a double could be larger than a float, it's saying I can't do that.
You have to explicitly say I know I could lose data, now we're not going to a 3.14, it's too small. It's totally fine to fit in a float. Float can handle a lot more but you have to tell it, okay, I acknowledge that this is a double by default but I'm going to convert it to a float because what's going on here is, this is called a narrowing or lossy conversion because you could lose data. Now, it doesn't look at the value, it looks at the data type and it says double going into float, that's not good. So, I could do this right here, I could put float in front of it, that's one way to do it. And if it were a variable conversion, you'd have to use this technique and you'll notice that the error went away. Now, if we put it back, the best way to use with literal constants like this is to just put an f at the end, you can put capital or lower case. I prefer lower case, it doesn't really matter.
So, you're basically saying, I'm acknowledging that I'm converting this to a float. Okay, bring back our double. And the other thing that could happen here is, let's say I want double yourDouble = myFloat. Okay, so this right here, the top one was a narrowing or lossy conversion, so you can't do that implicitly. This one seems to work, I'm putting float inside of a double. So, it's kind of backwards. This is actually a widening or lossless conversion. Pretty cool. I like to use the analogy of a styrofoam cup in a bucket. It's kind of a silly little analogy, but a lot of my students really get it and they really understand it and like it, so I'll use it here as well. For a narrowing or lossy conversion, the idea is, if someone is standing above me and they're holding a big bucket and I am holding a styrofoam cup, it's a little tiny styrofoam cup and that bucket, I don't know how much water is in the bucket but could they maybe unscrew a cap at the bottom or let's say they had perfect aim and a funnel.
If they put it in my cup, can I be guaranteed that it won't overflow? And the answer is no because I don't know how much water is in the bucket, i.e. the value, I just know that it is a bucket which is the data type, like a double. And I can't put it in my little float cup because when they pour it, it could have more than my cup can contain and it could overflow. So, by putting this little f at the end and acknowledging the conversion explicitly, you're saying, okay I'm willing to lose data if it comes to that i.e. I'm willing to get wet. The other problem or the other situation is with this one and that's a widening conversion. So, if I have a friend standing on a little step letter and they're holding a styrofoam cup and I'm holding an empty bucket, if they have perfect aim and I don't have to worry about them throwing it at me or doing anything mischievous.
Can I be confident that if they pour that styrofoam cup, no matter how full it is into my bucket, that I have enough room for it? And if my bucket's empty, the answer is yes. So, that's a widening or lossless conversion, so you're not going to get wet. The float will always fit inside the double. So, it's a silly little analogy, but it seems to help people understand things better. Since this lecture was an essential terminology lecture, your challenge is to repeat some of the information you learned. What I'd like you to do is simple. Name the eight primitive types. Try to do it without looking back and keep repeating them until you can do it without looking. Come back when you're finished. So, how did that go? Different people have different aptitudes and some people are really good at memorizing things and others aren't as skilled at it.
But either way, it's important to give it your best shot. We will review this information at various times throughout the course until you're a pro at it. By a little later in the course, it'll be second nature to rattle them off easily if I asked you. But for now, let's go over them to see if you got it right. So, the eight primitives in Java are byte, short, int, long, float, double, char, or car, and boolean. Great work, everyone. If you struggle with them, that's totally okay. Just keep practicing. In the next lecture, we will discuss comments and how they help us provide simple documentation of what we're doing in our code. I'll see you there.
John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.