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

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...

ClaimCenter

Claim Center is a web-based enterprise software application designed to manage the process of reporting, verifying, and making payments on claims against a policy. It manages the claims process from first notice of loss through execution of financial transactions, including the payment and setting of reserves. CC Functionality involves Group-based ownership of claims and claim sub objects Claim Financials Address book integration Workspace to manage claims process Distributed collaboration Activity coordination Worker and claim management

JRE vs. JDK

  The JRE and the JDK are two terms you hear people mention very frequently in the Java world, and what they are the two parts that we need in order to run and create Java applications. The JRE is the Java Runtime Environment. JDK is the Java Development Kit. And so, the JRE is what you require in order to run Java applications, and end users normally install the JRE. They're the ones who are going to run our apps. The JDK provides the tools that we need to create Java apps. So, normally, developers are the ones who install the JDK on their machines. And in order to develop apps, you need to run them, so the JDK installation includes a JRE. So how does all this work together? If we sit down, and we type up a Java source file with our program code inside of it, and we want to go ahead and run that in some host environment, how do we do it? A host environment might be Windows, Linux. It could be Mac. It could even be a browser. It might be a phone running Android. How do we get from ...