Data Types, Part 2 - Hexadecimal-Octal-Binary

1
2
Variables
PREVIEW8m 49s
3
Data Types
12m 34s
10
11
14

## The course is part of this learning path

Start course
Difficulty
Beginner
Duration
2h 12m
Students
97
Ratings
4/5
Description

In this course, we'll learn about Java data types and operators.

### Learning Objectives

• Variables
• Data Types
• Type Conversion & Type Casting
• Operators
• Operator Precedence
• Expressions, Statements and Block

### Intended Audience

• 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

### Prerequisites

• No prior knowledge is required about the Java programming language.
• Basic computer knowledge
Transcript

Hi there. In this lesson, we'll learn the usage of Decimal, Hexadecimal, Octal, and Binary number systems in Java. First, let's briefly explain what these number systems are. Decimal is the number we use in daily life and refers to the base 10 number system. It's also known as literal in Java. Binary refers to the binary number system. It consists of only 0s and 1s. In Java, we can store Octal numbers by simply adding 0 and b during initialization. The data type used for storage is int. Octal refers to the base 8 number system.

Also, all digits in an Octal number are between 0 and 7. In Java, we can store octal numbers by simply adding 0 during initialization. These are called octal literals. The data type used for storage is int. Hexadecimal shortly, hex, refers to the base 16 number system. They are represented by numbers from 0 to 9 and letters from A to F. In Java, we can store hexadecimal numbers by simply adding 0 and x during initialization. The data type used for storage is int. Now, let's look at the conversion between these number systems on the table. For example, let's look at the number 11 in the decimal system from this table.

The octal equivalent of 11 in the decimal system is expressed with 1 and 3. The hexadecimal equivalent is represented by the letter B. In the binary number system, it's denoted by 1011. Now, let's briefly remember how these numbers are converted into each other. First, let's examine 1101 in the binary system. Here, each digit is written with a base of 2. To convert this to decimal, we need to multiply each digit by a power of two. The first right most digit is 2^0. The second digit is 2^1. The third digit is multiplied by 2^2, and the left most last digit is 2^3. Therefore, when you do this, the number you get will correspond to the number 11 that we use in the decimal system, that is, in our daily life.

Second, let's examine 135 in the octal system. Here, each digit is written with a base of 8. To convert this to decimal, we need to multiply each digit by a power of 8. The first right most digit is 8^0. The second digit is 8^1, and the third digit is multiplied by 8^2. Therefore, when you do this the number you get will correspond to the number 93 that we use in the decimal system, that is, in our daily life. And lastly, let's examine 1AF in the hex system. Here, each digit is written with a base of 16. To convert this to decimal, we need to multiply each digit by a power of 16. The first right most digit is the F. So, the meaning of the F letter is 15 in the decimal system. So, we multiplied the 15 by 16^0. The second digit is the A.

So, the meaning of the A letter is 10 in the decimal system. So, we multiplied the 10 by the 16^1. The third digit is multiplied by 16^2. Therefore, when you do this the number you get will correspond to the number 431 that we use in the decimal system, that is, in our daily life. Okay, now, let's move on to the Eclipse and do some exercise. In exercise project, right click on the source folder and select "New class'. Specify package name as datatype and class name as HexOctalBinary and select the checkbox to add the main method. Okay. Let's start with octal number systems. First, I will create a variable type of integer, int myOctal1 =, and I will assign 09 to this variable. But as you can see, we got a compilation error because the value begins with 0  and Java accepts this variable as an octal number. So, the octal numbers take the numbers from 0 to 7.

Notice that. Now, I will fix it. I will write 016 instead. This octal number will store as 14 in decimal in Java. So, if we print the myOctal1 variable to the console, we see the 14 as the result. Now, let's create another variable. int my Octal2 = 0205. Since its first digit is 0, Java accepts it as octal. So, when we print this to the console, we see the number 133 as a result. Now, let's add these two variables and pass them to another variable. int sumOfOctals = myOctal1 + myOctal2; This will be printed on the console screen as the sum of the decimal values of both variables i.e. 147. Now, let's quickly print them to the console with the println method and observe. Also, let's create a print method at the top of the print methods which indicates these operations belong to the octal system. Yes, let's run the application now.

Okay. As you can see, decimal values are printed on the console screen, not octal values. If you want, we can print the equivalent of a decimal value in the octal system to the console. For example, let's print the value of the sumOfOctals variable to the console as octal. I'm copying this method and pasting it here. Well, now we'll use the toOctalString method of the integer class which is a subclass of the number class to print this value as octal. Here I write, Integer.toOctalString, I write the decimal number, that is, the sumOfOctals variable which I want to learn the octal equivalent of in the parentheses. Now, let's run the application once again.

And as you can see, we have also printed the equivalent of the sumOfOctals variable in the octal system to the console. Yes. Now, let's look at hexadecimal. First, I will create a variable type of integer. int myHex1 = and I will assign 0x01E to this variable. The hexadecimal number should begin with 0 and x. Also, if you want, you can write this x as capital. If I write a different number here, for example, H, we get a compilation error because the hexadecimal takes the letters between A and F. And this hexadecimal number will store as 30 in decimal in Java. So, if we print the myHex1 variable to the console, we see the 30 as the result.

Now, let's create another variable. int myHex2 = 0XA2; Since it begins with 0 and X, Java accepts it as hexadecimal again. So, when we print this to the console, we will see the number 162 as a result. Now, let's add these two variables and pass them to another variable. int sumOfHex = myHex1 + myHex2; This will be printed on the console screen as the sum of the decimal values of both variables i.e. 192. Now, let's quickly print them to the console with the println method and observe. I will copy these println methods and paste them here and I will make some changes. This will be hexadecimal. This will be myHex1 and this too, this will be myHex2 and this too, and this will be sumOfHex and this too. Also, this method will be toHexString and its value will be sumOfHex.

Okay. Now, let's run and see. But the result is different because we deleted the E letter. Let's add it and run the code again. As you can see, decimal values are printed on the console screen, not hexadecimal values. And the last row is the hexadecimal equivalent of the sumOfHex variable. Yes, now let's look at binary. First, I will create a variable type of integer. int myBinary1 = and I will assign 0b0101 to this variable. The binary number should begin with 0 and b. Also, if you want, you can write this b as capital. If I write a different number here, for example, 2, we get a compilation error because the binary numbers take only 0 and 1 and this binary number will store as 5 in decimal in Java.

So, if we print the myBinary1 variable to the console, we see the 5 as the result. Now, let's create another variable. int myBinary2 = 0b1101; Since it begins with 0 and b, Java accepts it as binary again. So, when we print this to the console, we see the number 13 as a result. Now, let's add these two variables and pass them to another variable. int sumOfBinary = myBinary1 + myBinary2; This will be printed on the console screen as the sum of the decimal values of both variables i.e. 18. Now, let's quickly print them to the console with the println method and observe. I'll copy these println methods and paste them here and I will make some changes.

This will be Binary. This will be myBinary1 and this too. This will be myBinary2 and this too. And this will be the sumOfBinary and this too. Also, this method will be toBinaryString and its value will be sumOfBinary. Now, let's run and see. As you can see, decimal values are printed on the console screen, not binary values, and the last row is the binary equivalent of the sumOfBinary variable. Yes friends, I think this issue is understood. The point you should pay attention to is how the number systems here are defined and they are all stored as integers no matter what number system, and which numbers or characters each number system is represented by. Yes, let's take a short break here. See you in the next lesson. 