Skip to main content

Primitive Data Types

 So, as we've seen when we declare our variables, we give them a type, and the most fundamental types in Java are what are called primitive data types, and these are the data types that are built into the language. Now, when you hear the term primitive data type, you may think there's something kind of lowly about them, you know, something kind of, you know, less than modern about them. But that's not true at all. Primitive data types are actually very important. They're really the foundation of all other types that we use in Java, so they're really that kind of strong foundation that we're going to build on for any other data types we use in our programs. Now there are four categories of primitive types in Java. There are the integer types, floating point, character, and Boolean. So, let's look first at the integer types. Now there are four different integer types, but the difference then really is just the size of storage that they take up. But that difference in the size of storage they take up affects the range of values that could be stored there. All right, so the smallest integer type is the byte type. So, it takes 8 bits, so it can only store between minus 128 and positive 127. And so, we see when declare it, we just declare a variable of type byte, and we just assign the integer, excuse me, the integer literal to it. All right, so here we have a byte numberOfEnglishLetters = 26. The next larger integer type is short, which takes up 16 bits. Let's let you start with, store between negative 30,000 and positive 32,000. Use it the same way. So, we have here short metiamide is 5280. Probably the most used integer type is the one called int. That's a 32bit integer. And being 32 bits, let's a store between minus 2 billion and positive 2 billion. And then the big integer type is what we call a long. That's a big 64bit integer. And you see it can store huge values in it. The key thing to notice though is that when you use a long literal value, you must put that capital L at end of it. Now Java also has floatingpoint types. Now, the floatingpoint types conform to the IEEE standard for floating point, and that may or may not be meaningful to you. What it really comes down to is that floating points allow you to store values that have a fractional portion to them. Basically, it supports positive, negative, and zero values that have some fractional portion. There's a lot of nuances to the way floating points work, and they're kind of outside the scope of this course, but I've got that URL on the screen there for you. If you want to know a lot more about the details in terms of how floating points work and kind of the oddities of them, I encourage you to check that out. But basically, we have our two floatingpoint types in Java. First, we have the float type, which is a 32bit floatingpoint value. Notice that when we use the float type, we must put the f at the end of any constants for it. So, when we declare this float called milesInAMarathon, we say 26.2f, saying it's a floatingpoint value. And then we also have double, which is a 64bit floatingpoint value. If you just use a literal that has a decimal in it, the compiler assumes it's a double, but you can also make it explicitly a double by putting a d at the end. So, if we look here, we have double atomWidthInMeters. You see that 0.0000000001d denotes that that is a double literal. And our last two primitive types are character and Boolean. And a character, or, the char type, stores a single Unicode character. And basically, you denote the literals of these by just using single apostrophes or single quotes around the constants. So, if I say char, the regularU equals, and I put 'U', that assigns that char into it. Now note this is different than strings. We'll talk about strings later. Char is just a single character value. And because the char type supports Unicode, you can specify any valid Unicode character in there. So, if you want to assign a Unicode character that you don't have on your keyboard, you can use the Unicode code point by using that \u notation. So, you see here I've got this char accentedU with the '\u00DA. Says that that is actually a U with an accent on it. And then finally, we have our Boolean types. Boolean types store true and false. The literals for that are true and false, so if I say boolean iLoveJava = true.

Comments

Popular posts from this blog

Difference Between Programming and Coding

What exactly is the difference between programming and coding?  The other day, one of my friends who is not from a computer science background, asked me this question. Even after learning many different programming languages and doing several projects, I could not answer him correctly. I said both are the same. But why do we use two different terms, if both are the same? That led me to some research, and I thought I’d share what I found.   It’s not that complicated.  And their definitions allow for a lot of overlap. We often recognize the terms coding and programming as synonymous because both are often used interchangeably.   what is the difference between programming and coding? Coding is the act of expressing programmatic ideas in computer language. Programming is crafting ideas that can be executed repeatedly by a machine, not necessarily a computing device. While both the terms are synonymous with each other and are often used interchangeably, t...

Types of Recording

Types of Recording: 4 types of recording modes are available Ø   Basic Ø   Desktop Ø   Web Ø   Citrix 2 ways of Recording Ø   Automatic Ø   Manual Basic – generates a full selector for each activity and no container, the resulted automation is slower than one that uses containers and is suitable for single activities. Desktop – suitable for all types of desktop apps and multiple actions; it is faster than the Basic recorder, and generates a container (with the selector of the top-level window) in which activities are enclosed, and partial selectors for each activity. Web – designed for recording in web apps and browsers (supported: Internet Explorer, Google Chrome), generates containers and uses the Simulate Type/Click input method by default. Citrix – used to record virtualized environments (VNC, virtual machines, Citrix, etc.) or SAP, permits only image, text and keyboard automation, and requires explici...

Variables, Data Types and Math Operations

  In order to do anything interesting in a program, we must have the ability to store and manipulate values. What allows us to do that are what we call variables. Now a variable, simply put, is just named data storage. Now Java is a strongly ‑ typed language. Now what that means is that when we declare a variable like, in this case, we have a variable named data Value, we have to specify the type of that variable, in this case, it's what we call an int, something that can store integers. So, what that means is that the data Value variable can only store things that are compatible with the type int. As we go through the course, we'll talk about this idea of one type being compatible with another. Now when we declare a variable, we can, of course, then assign a value to it, so our variable data Value now holds the value 100. Now when we use variables, we can do it the way we've done here where we declare it, then assign it, or as a matter of convenience, we can declare them a...