The course is part of this learning path
In this course, we'll learn about Java data types and operators.
- Data Types
- Type Conversion & Type Casting
- Operator Precedence
- Expressions, Statements and Block
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language.
- Basic computer knowledge
Hi there. In this video, we will learn Data Types. The data type defines the value that a variable can take. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables. In Java, we need to define the data type of variable to use it and you cannot assign incompatible data type. Otherwise, the compiler will give you an error. Also, the Java programming language is statically typed, which means that all variables must be first declared before they can be used. This involves stating the variables type and name as you've already seen. int gear = 1; doing so tells your program that a field named gear exists, holds numerical data and has an initial value of 1. A variable's data type determines the value it may contain plus the operations that may be performed on it.
In addition to the int, the Java programming language supports seven other primitive data types. A primitive data type is predefined by the language and is named by a reserved keyword. Primitive values do not share a state with other primitive values. Let's look at the diagram. There are two data types available in Java. The first one is primitive and the second one is non-primitive data types. Non-primitive data types are also known as reference data types. Class, enum type, interface, string and array are non-primitive data types. We'll discuss them in the coming lectures. Here, we'll discuss primitive data types and literals in Java. Primitive data types are predefined by Java programming language and are named by a reserve keyword. Java provides eight primitive data types. These are boolean, char, byte, short, int, long, float and double. Primitive data types are only single values. They have no special capabilities. The boolean data type is used for variables that hold either true or false. Char is used for storing characters. Byte, short, int, and long data types are used for storing whole numbers. Float and double are used for fractional numbers.
For the size and default values of the primitive data types, we can examine the table on the slide. Byte: The byte data type is an 8-bit signed 2's complement integer. The byte data type is useful for saving memory in large arrays. It has a minimum value of -128 and a maximum value of 127 inclusive. The default value of the byte is 0. Short: The short data type is a 16-bit signed 2's complement integer. Similar to byte, we use a short to save memory in large arrays in situations where the memory savings actually matters. It has a minimum value of -32,768 and a maximum value of 32,767 inclusive. Also the default value of short is 0. Int: The int data type is a 32-bit signed 2's complement integer which has a minimum value of -2^31 and a maximum value of 2^31-1. Long: The long data type is a 64-bit 2's complement integer. The signed long has a minimum value of -2^63 and a maximum value of 2^63-1.
Use this data type when you need a range of values wider than those provided by int. Float: The float data type is a single precision 32-bit floating point. If you need to save memory in large arrays of floating point numbers, you should use a float instead of a double. Double: The double data type is a double precision 64-bit floating point. For decimal values, this data type is generally the default choice. Boolean: The boolean data type represents only one bit of information, either true or false. Char: The char data type is a single 16-bit unicode character. Let's make an example with data types. In exercise project, right click on the source folder and select new class. Specify package name as datatype and class name as DataType, and select the checkbox to add the main method. Let's start with the data type. boolean a = true; Boolean type has two possible values, either true or false. char b = 'R'; char type declares a character. byte c = 12; byte type can have values from -128 to 127. short d = -356; short type can have values from -32,767 and 32,767. int e = 43,543; int type is generally used for numeric values.
Let's declare variable f with the long data type. Long type is used for large values. L is the specific suffix for a long type. Another variable is g with a float data type. float type is used for large values with single precision. F is the specific suffix for float type. Another variable is h with the double data type. Double data type is used for large values with double precision. Let's print the values of these variables on the screen by using the println method. I will copy this line and paste it below seven times. Let's change the variable name from a to h. Okay, let's run the code. Of course, the application did not run directly because there was some code in a variable's class that caused a compiler error. Let's fix this right away. I'm opening the variables.java file and here I am converting the codes that caused the error to the comment line, and let's save the changes we made. Yes, now we can go back to the datatype.java file, and let's run the code once again. Okay, you see the results in the console. In addition to the eight primitive data types listed above, the Java programming language also provides special support for character strings via the java.lang.string class. In closing your character string within double quotes will automatically create a new String object. For example, String
s = "This is a string"; s variable holds the "This is a string" text. Let's run the s string to the console with the println method. You see the results in the console. As a result, the string objects are immutable, which means that once created their values cannot be changed. The String class is not technically a primitive data type, but considering the special support given to it by the language, you'll probably tend to think of it as such. You'll learn more about String class in a different section of this course. Now, I'll show you another thing. First, let's create a new variable of type long. long phoneNumber = 123456789012; and as you can see, we got a compilation error. The literal of type integer is out of range. To prevent this error, I can write L at the beginning of the value or the lowercase l. Both are acceptable but using a lowercase l Is not preferred. Now I want to draw your attention to another point.
Notice that I named this variable the phoneNumber, but these numbers seem rather complicated to read as a phone number. We can use underscore to read better. This is allowed, for example, we can put underscores here, here, and here, but we can't put the underscores first or last. In this case, our code will not compile and we will get a compilation error. It's also possible to use the underscore with fractional numbers. Now let's define a variable of type float. float myNumber = 15.234586; Of course, since this is a float, we need to write a lower or uppercase F here. Generally, capital letters are preferred. Now let's use the underscore. Again we can put an underscore between the numbers, we won't get any errors. But we can't put the underscore at the beginning or at the end, or just before or just after this point here. Otherwise, we will get a compilation error again. Pay attention to this. Yes, I think that's understood. Let's take a short break here. See you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.