- Podcasts
- The Java Developers Hub
- Java Variables and Data Types Explained | Identifiers, Literals & Keywords
Java Variables and Data Types Explained | Identifiers, Literals & Keywords
The Java Developers Hub
- December 3, 2026
- 1 h 7 min
- Episode 6
About this episodeUnderstanding variables and data types is one of the most important steps in learning Java programming. Every Java application uses…
- What variables are and why they are important in Java.
- How to declare, initialise, and assign variables.
- Understanding Java primitive data types.
- Differences between primitive and non-primitive data types.
- Understanding identifiers and Java naming conventions.
- What literals are and how they are used.
- Understanding Java keywords and reserved words.
- Differences between int, float, double, char, and boolean.
- How Java stores different types of values.
- Rules for naming Java variables.
- Common variable declaration mistakes and best practices.
- Complete beginners learning Java programming.
- Computer science and software engineering students.
- Aspiring Java developers.
- Programmers transitioning from other languages to Java.
- Anyone preparing for Java programming examinations or technical interviews.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Java Variables and Data Types Explained | Identifiers, Literals & Keywords
Welcome back Coder Army to our Java series and till now we have read the basic things of Java like Java IQ, JDK, JRE, JVM etc Now we want to go into the syntax of Java, we will understand how to declare variables in Java, what are the variables, what are the keywords etc. So with this, we will go a little closer to syntax, we will understand Java from inside, how things are written in Java. So without wasting time, let's quickly come to the screen and start. So first of all, today we will start that what are the variables? So understand this like this, first I will tell you a little theory of this and then we will understand it through the program. Now let's assume that you have written a source code of Java. Like we were talking till now, let's say the name of this file is demo.java demo.java In this, you must have written some code. Now again, we are not focusing on syntax. Let's assume that here, there will be some source code of java. Now whenever you write some code in java or in general whenever you write any program, then why do you.
We want to make an application. We want to do some work. We write its code. So, if your code is written in such a way that it demands some input. For example, if you just want to print something on the screen. Like we printed Hello World last time. So, we didn't take any input from the user. Or we didn't take anything externally to store something. So, our work is done without storing anything. We didn't declare any variable. But let's say you want to make a calculator app. And let's assume that you want to make a simple additional functionality which will add two numbers. So it will take two numbers, number 1 plus number 2 and will give you its output. Simple.
Right? So when you store something like these numbers in the computer's memory, in that case, we call the location of that memory a variable. Let's understand this carefully, let's go a little deep and understand it so that things are clear to you. If I tell you roughly, So let's suppose our program wants to utilize the memory of our system, our RAM, right? Now look, there are many types of memory, there is heap memory, there is stack memory, but we will come to all of them later when we will study memory in depth, okay? For now, you assume that there must be a computer memory, right?
Now I told you that you have to make a calculator app. That means you have to add two numbers and give its output. So you will have to take these two numbers as input. Now these two numbers which you have taken as input, you will have to store them somewhere in the memory. So let's say the number coming here is stored at this location of the memory. And let's say that number was 2.
Now remember, 2 will not be stored like this in the memory. Everything is stored in binary. So the binary of 2 will be stored like this. And if you don't know how to find the binary, then we will see in a while. But for now, let's assume that 2 has been stored like this. Now, you have to store this number in memory as well. So, let's say we have stored this number in memory which is 3. Right? So, how will we identify the numbers that we are storing in memory?
Basically, what I will do is, I will say, let's say one variable is x and the other variable is y. It means that x represents this and y represents this. Now whenever I say in my code, in my Java program, whenever I say x plus y equal to, then my meaning will be 2 plus 3 equal to. And I will give its output directly, let's say 5. So if I give a big definition of this, then you can consider the variable as a container. This is a container. And this container will have some name. Let's say you gave its name x. So, this container holds your value. So, what does variable mean? Any container which can hold any value. Now, this container can hold any value. For example, it is holding 2 for us. So, this is a container. which I have named x which holds the value of 2 for us.
It's a very simple concept. Now obviously, in any programming language, not just Java, we don't give these naming conventions x or y like this. Because it's very confusing that what is x, what is y. So in computer science, we describe everything and write. So let's say if I was making a calculator app which adds two numbers. And I will store these two numbers. Let's say I stored one number here. And I will store one more number here. This is also a container. This container is a computer memory. I will store another number here. So instead of naming them X or Y, I will try to give them a better name. And what is that better name? Let's say I gave them a name. First number. And similarly second number.
Let's assume that we have a container which is a computer memory which can hold a value of 4. To denote this, let's create a variable. Let's say we created the first number above and this was the first number. So the first number means 4 which is in the container. This first number is holding a value. To identify the value of this container, we use the first number. That's why we call it an identifier in the language of computer science. So I can say that the first num is an identifier. What does identifier mean? It means that if I want to represent this container, then I have to say the first num.
Before moving ahead, I want to tell you that whatever code we write in Java, you have to consider it as a black box. What is a black box? Whenever you understand a programming language, for example, for now, Java, okay? In that, a lot of syntax, in the beginning, you may have to understand why we have written it. Because we will have to go inside it, we will have to understand that the things we have written, why they are used, but we will not be able to understand it now.
So, understanding a programming language is never linear, right? You have to think of some things as black box. Black box means? You have to imagine a black box and you know that I give some input in it and I get some output. I don't know how it is working inside. This is the concept of computer science, the black box model. You don't know how it is working inside, I will understand that later. So this is used in many places. So for now, when we understand Java, there must be some code written in it that if you never understood any programming language or never came from any background in programming and you are studying Java for the first time, then you may have to understand it. But it's okay, we have to ignore it and focus on the part that we just have to understand. So first of all, let me show you that code. So you will see a code like this whenever we open a file of Java. For now, you should not have any problem with this line and this line. You just remember, in public static void main, I forgot to make the open bracket.
We have already discussed that there are variables So I will explain its syntax Let's say you have to make a variable Which is named firstNumber And in that you have to hold the value Let's say 10 So you will write firstNumber Equal to 10 It is basic, just like we write in Maths And what else will you mention? Apart from this you have to mention Here a data type Now, what is data type? Look, in any programming language, we are talking about Java here, data type means that your variable first number, what kind of.
So, in Java, First we have to tell what kind of value the variable we are going to make will hold So basically the container we are going to make, the value that is going to come in it, what is that value? Is it an integer value? Is it a floating value? We have to tell this in Java first That's why we know Java from another name in which we say Java is tactically typed Categorically Typed means that while writing code in Java, before compiling, you will have to tell that what type of variable you want to declare. And why you have to tell that? You will understand it when you read data types. But for now, let's assume that first of all, you will come to the name of the data type. After that, whatever the name of the variable is, which we call identifier. And then it will come equal to and whatever value you want to give it, that value will come. And finally, We end every line in Java with a semicolon. So this semicolon is also very important. It tells you that one line of Java is finished. This same concept was also there in C + +, so it is the same to a large extent. But if you are reading for the first time, then remember, we end every line with a semicolon. Now we have to understand what are these data types and what are they like. Once we understand that, then we will see all the examples with it in the code.
There are two types of data types in Java. We divide them into two types. One is our primitive and the other is our non-primitive. Now, what is primitive and non-primitive? We don't have to focus on that right now. Let's assume that what we will study now is only primitive. We will study non-primitive later in this series when we will talk about OOPS, object-oriented programming. So, when we will talk about object-oriented programming, we will talk about OOPS. So, because we have to keep this series beginner-friendly, let's assume that you don't know OOPS at all.
So in primitive data types, we divide data types into four types and they are very similar to the ones we read in mathematics that our data can be an integer or our data can be a real number. We call it floating. I will write it as a real number for now. It can be a real number or an integer. Apart from this, our data can be a number or a character. Apart from this, our number can be a Boolean. So, I divide the datatypes in four different ways.
Start with bytes and see what are our data types. So above I told you that if you want to hold a value in a container, then you have to give it a name, identifier, like we gave a name there, first, sorry, like we gave a name there, first number, something like this, right? So, I told you that what is syntax? First, you write the name of a datatype. Then, you write the name of this, the first number of our identifier. And then, you write the value that it wants to hold. And finally, you put a semicolon. So, we fill these things. So, let's say, I am saying that our value is 10. I want to hold 10. Now, because 10 is an integer variable, right?
Range. What does range mean? Range means how many big numbers you can represent. If I write it carefully, then what did I say here? Your byte can come. Your short can come. Your int can come. And your long can come. And what will come here. What you should put. It depends. That the number you want to store. How big is that number.
The smallest is the byte range, then short, then int, and then long. Their range keeps increasing. Range means that the byte can hold less values. If I tell you exactly how less, then it can only hold numbers from minus 128 to plus 127. Short can hold a bigger number, Int can hold a bigger.
Now, you must have understood the first thing that anything can come here. Now, because we have taken a very small number, 10, and 10 is also coming in this range, so we can also write byte here. Or short, int, and long. If we can write the smallest, we can write the biggest. Now, before moving ahead, how do we determine their range?
Let's say, we have a number 4. And the number 4, which we normally count as 1, 2, 3, 4, 5, we do it in base 10. What does base 10 mean? We have 0 to 9. There are 10 characters to represent anything. After this, we repeat all the numbers. So if we want to represent 10, we represent 1 and 0. If we want to do 11, then we repeat 1 and 1. Same 12, 13, right? So we have only 10 unique characters. From 0 to 9. And this is our decimal number system. In binary, the computer understands. We saw that computer only understands 0s and 1s. 0 means off, 1 means on. 0 means current pass is not happening, 1 means current pass is happening. In that case, we have only two numbers, 0 and 1. And we have to express all the numbers in the form of 0s and 1s. So let's say I want to represent a simple 0 number. So I can call it simple 0 in binary. Because obviously we have two characters 0 and 1 and I can call 0 0. Same if I want to represent 1, then we have 1 character also, so I can say 1. Like here from 0 to 9, so to represent 9, I used to write a simple 9. There was a problem when I had to represent a bigger number than 9. So the same problem will come here when I have to represent a bigger number than 0 and 1, like 2.
See, like After 9, you used to repeat the characters in the decimal number system And used to make 10, 11 in the same way Here we will make 2 with the combination of 0s and 1s How? Look If you think carefully in the decimal, then base used to be 10 Base means you must have understood how many unique characters we have So in that case, if you have a 2 digit number Or let's say you have a 3 digit number So we used to say, this is 1's place, this is 10's place and this is 100's place This must have been studied by your children, right? 1s, 10s, 100s. Now, when you used to say 1s, 10s, 100s, similarly, when you come to binary, you have base 2, right? So, here, if I want to represent a number, a 3-digit number, then I will say, this is my 1s place, okay? Now, because base was 10, so it used to increase from 10s. Example, 1 into 10 is 10, then 10 into 10 is 100, then 100 into 10 is 1000. So, places like this used to increase. Here, it will increase from 2's power. So, this is 1s place, meaning 2's power is 0. This is twice place, which means 2 to the power 1. This is fourth place, which means 2 to the power 2. And this is 2 to the power 3. And this is how it moves forward.
So, I hope you have understood a little bit, how we represent places here. Now, let's talk about our question, how we represent 2 here. Now, obviously, to represent 2, I will need two digits. One of one's place, which means 2 to the power 0. And one of two's place, which means 2 to the power 1.
Now I want to represent 4. Now to represent 4, I need 3 digits. 1, 2, 3. So how will I represent 4? Here is 2 to the power 0, 2 to the power 1, 2 to the power 2. This is 4, this is 1. Sorry, 2 and this is 1. So because here is 4, so I write 1 directly, the rest is 0. So 1, 0, 0 becomes 4. So this is how I work to represent binary numbers. Let's go back to our topic. So what I said is that we have these data types, byte, short, int, long, their range, how many big numbers can they represent. So if I give you their range, then see, this is their range. The smallest number we did, byte, is of 8 bits, 8 bits means its range is from minus 128 to plus 127. Short is of 16 bits, int is of 32 bits and long is of 64 bits. Now what does bit mean? Let's say you want to represent a number 10 And I said that the byte is of 8 bits. What does this mean? I have 8 positions. 1, 2, 3, 4, 5, 6, 7, 8. I have these 8 positions to represent any number.
Something like this. If I want to do 2, no problem. As we saw, I will do 1 here and 0 here. So all my remaining bits will remain 0 if they are not being used. Because we had a small number, it will be finished in 2 digits. Now if 4 comes, it will use 3 digits. After that, if a bigger number comes, it will use 4 digits or 5 digits.
Meaning, I have 4 unique possibilities using 2 bits. If I were using 3 bits, then what possibilities would I have? I could do something like 000, 001, 0100, 1, 1, something like this. And I used to write it like this, 1, 1, 1. So how many possibilities are there? Now see, we can find a simple formula for this.
So basically, with n bits, we can make 2 to the power of n unique combinations. For example, if the value of n was 2, then we can make 4 unique combinations. Similarly, if we take the value of n as 3, n value is 3, so 2 to the power of 3 is 8. So we could have made 8 unique combinations here. This is how many unique combinations we can make, so it's a normal permutation equation. Now if we go one more step ahead in this and see, what could be our biggest number in 2 bits? So how can we get that out? For that, the formula would be 2 to the power of n minus 1. As many as we can find the total unique combination, minus 1. Why? Because the smallest number must be 0, right? And after that, there must be n minus 1th number, right? This is the basic math. So, if I look at this case, then if I have only 2 bits, 1, 2, what can be the biggest number in this? So, 2 to the power of 2 is 4, 4 minus 1 is 3. So, I can represent the biggest number 3, right?
This is 1's place, this is 2's place, this is 4's place. So, 4 has 1, plus 2, 6, plus 1, 7. 3 has 1, so the total number is 7. If I want to represent 8, then I will need 4 bits, right? So, this formula used to be. If I put this here, if I put this on my 8 bits, then on 8 bits, the biggest number I can represent is 2 to the power of 8 minus 1.
I can do it like this, that I have, let's say, 8 bits. 1, 2, 3, 4, 5, 6, 7, 8. 8 bits and I put 1 in every bit. Put 1 in every bit. Something like this. So this is my 1's place, 2's place, 4's, 8, 16, 32, 64, 128, something like this. It increases with the power of 2. So if you add them, 128 plus 64 plus 32 plus 16 plus, if you add till 1, then you will get the number 255. Because this is the biggest number that I can represent. But I just told you, the range of our bytes, That is from minus 128 to plus 127.
Minus 128 to 0 and here is the range of plus 127 So from here I am representing from minus 128 to plus 127 That's why I can't represent the biggest number 255 It's a simple thing If I want to represent 255 Then I have to go ahead of byte and go towards short In which there are 16 places like this In that, I can represent a very big number, 2 to the power of 16 minus 1. Actually, not this, I will also do half of this, 2 to the power of 16 minus 1 by 2. Because in that also, what will I do? I will represent a negative number to a positive number. Right? So, these are a few things, these are common in every language. Whenever you start a language, you have to understand this once.
We can go ahead and say that the byte range is 8 bits, so I can represent this. Its range is 16 bits, which means I have 16 characters. And the smallest number is minus 32,768 and the biggest number is 32,767. If your number is lying in this range, then you can use short. If you want a bigger range than this, of 32 bits, then you can use int. And if for some reason you want a bigger range than this, then you can use long. And you can see, How big is this number? It's a quadrillion number, right? Think of any use case in which the range is going out of this number. And if you think it can go out of this too, then there is a special function for that in Java.
And we have also told you how this works Now the most interesting question should come to your mind That how do we represent numbers in binary? So for example, I have to represent 10 So how will I represent 10? This is 1's place, 2's place, 4's place and 8's place So to represent 10, I will give 1 here and 1 here and the rest will be 0 So 10 will be 10 Simple, right? Now I understood how to represent positive numbers You should have a question in your mind that how will you represent negative number?
Because computer doesn't have any negative concept. We as a human understand what is the meaning of minus 5. But computer doesn't understand the meaning of minus 5. Computer doesn't even understand 5. Computer only understands 0s and 1s. By using this we can make 5. But how will we make minus 5? This is also a very interesting method. Hold that question for the next video, in which I will tell you how we can represent negative numbers. And apart from this, there will be some numbers in the future, which are very interesting to represent in binary form. So it deserves a separate video because we will go in depth in it. But now we will first cover all the data types. So I hope you have understood integer data types that we have four types.
Their range is written here. Sorry, their width is written here and their range is written here. Now when we will use them in code then you will be able to understand more things Now we will move towards our next data type which are our real numbers Now you know that in Mathematics not everything is integer You have numbers like 5.23, 10.02 and some other numbers like decimal numbers So these numbers in the language of computer science we call them floating point numbers.
And my next question was, how can we represent a decimal number in binary? This is also very interesting. We will see this in the next video with negative numbers. So for now, let's assume that 5.23 is our store somehow. Let's say I have given some name to this variable, let's say num, which is our identifier. So I will identify it with num. Now if I want to declare this num, then we understood from syntax that first data type will come, then its name will come, num, equal to, and here value will come. Now value we have is 5.23, and at last semicolon will come. Now here again, what comes here, float comes or double comes, this will depend again on range. Float range is less and double range is more. Now let's see the range of these two and understand it. In Java, these are the range of float and double. Float is of 32 bits and its range is written here. Double is of 64 bits and its range is written here. Now see the range written here is written in scientific notation If you know to read scientific notation then it means 4.9 And E means exponent i.e 10 to the power minus 3 to 4 This is its smallest range and similarly its biggest range is written So I hope you understood this range, you must have understood to read it Let's go a little in depth and discuss it See the floating point numbers Especially if our data type is float So we call float single precision What do we call it? Single Precision. And Double is obviously called Double Precision. You can understand it from the name itself.
Double Precision. Now, let me tell you something. It's a very interesting thing. Whenever you write a Java code, and if the code is production-oriented, meaning if you are making a web app or website, and you want to deploy that code, then we almost never use float. Why am I saying this? Look, float was made a long time ago. And the previous processors, which were made in the earlier stage, were quite optimized for float calculations. One thing is that this is a single precision, which means its precision is quite low. Meaning, if you want to represent such numbers, in which the number of digits after decimal numbers does not matter, So you can use float. For example, if you want to calculate dollars and cents, then 2.32. It doesn't matter that much because the value of cents is less.
But if precision matters a lot, if you are doing a scientific calculation, then you should always use double. The second reason is that the previous hardware was optimized for float. But all modern hardware is optimized for double. Meaning if your number is already double, then they can do the calculation very well and very fast rather than if your number is float. So float is used very less in almost negligible production. Because every number which you can represent in float, you can do it in double because it is double in double range. And the other advantage of double is that its calculation is strong because it is more optimized for the hardware processor. So even if we look at the example of float, it is very rare that you will use float in a real life project, we always prefer double. In fact, let me tell you that all the inbuilt functions that Java gives us for scientific calculations, like there is a method sign, there is a cause, Cause, Average, etc. So, Java gives us many such methods that we can call them directly and get a value. All of these return us double. Meaning, they return that value in double. How do they do that? We will see that when it comes. But now, let's assume that we always prefer double in real-life scenarios. We rarely or almost never use float. Still, because it is a part of it, I have told you that float and double have two types of range. So I hope you understood the floating point number, we had read the integers before this. There are just two more, let's quickly see the characters and boolean and then jump on their code. So look, our third type of data type is characters, in which only one type comes, char.
All of them can be represented in an ASCII format. It means, let's say, if I want to declare a variable, let's say, again, I have a container here, I want to put a character in it. Character means, I want to put a character, a. So, I want to store a small a in it, simple, small a. Now, because I want to give it a name, let's say, I give it a name, alphabet. Now the identifier is alphabet and its value is a. So if I look at it carefully, how we used to define any variable. So first of all we used to give the name of the data type. After that we used to give the name of the identifier. So the identifier's name is alphabet. Equal to and then the value of it. Now the value of alphabet is A. But we can't write A directly here. I have to write single codes here. These single codes tell our Java compiler that whatever I have written here is a character. Right? Because if I suppose that I don't write a single code. Then it can be an identifier like this. How does Java know? You don't want to do this. You are trying to merge one identifier with another identifier.
So, this is our way of representing a character. Now, before this, the story I was explaining to you about ASCII was that before Java, we had a ASCII standard. And ASCII tells you that if you want to represent any character, say, small a to small z, big a to big z, 0 to 9, or any special symbol, then how can you represent it? Now, you understand it like this, that our computer only understands zeros and ones, it won't be able to understand A. So, how will I express this A in the format of zeros and ones? So for that, what we do is, let's say this is our container. We can't store A directly in the container. I have logically shown you that A is there. But actually, there should be a binary number here. So what we do is, we use a very smart method. Sorry, let me write this with a pen. That we convert this a to an integer number. So let's say I said that I will represent a using 65. Now I can convert 65 to binary. 65 must have some binary, 100 something something.
We had already seen that how you convert an integer to binary. But how will you convert a character? So first you convert a character to integer and then you convert that integer to binary. Now if there is a binary written here, and let's say I write 65 here, you will understand that the binary of 65 is written here. So how will the computer get to know that the 65 written here, is it an integer value? That means when the user asks me to bring whatever value is written in this container, should I bring it directly to 65 or should I first convert 65 back to character and bring it to A? How will this be known? This is known by any from the data type of the identifier.
So, if the data type of this alphabet was a character, then it will understand that I have to convert this 65 to A again and give it back. If some int was written instead of a character, or a byte was written, or short, or long, then it will always give me 65. Obviously, in that case, it is not A. Here, we write some integer value. Let's say, 65 directly. But I am telling you back of the calculation that there is 65 store in the container. Now, this 65 can be an integer or it can be A. That will tell you which data type is store here.
Okay, so I told you what was the way to represent A. We used to convert it into an integer. But the problem was that our ASCII format used to give all these numbers, A to Z, Big A to Big Z, 0 to 9, all of them a digit. And these special symbols also used to be given a digit. But those characters which don't come in English, for example, I say in our Hindi, character ka, character ga, all of these, they are some... Because they didn't fit ASCII standards. ASCII was 0-127, meaning 8 bits were required to represent any ASCII character. Now because Java wanted to make itself very portable, wanted the whole world to use it, so Java used a very interesting concept of Unicodes. Java said that its character is not enough for me and nothing will happen in 8 bits.
Character range from 8 bits to 16 bits. Remember, character takes 16 bits. And said that why only English alphabet? I have to represent all the characters of all the languages in the world. And to represent that, we came up with the concept of Unicodes. Unicodes, all the characters in the world, all of them come in that unicode and it gives an integer value to all of them.
Let's say, any Tamil, Kannada, any language you tell, Greek, Chinese, Greek, any language you tell, whatever character it has, it must have given an integer value to it. Let's say, it has given 9 for character 9, for example. Or it has given 10 for any other Chinese character, etc. So, in unicode, all the characters in the world exist. For everything, he gave an integer value. So that I can store that integer value in my system.
Let's say there is a container here. Let's make it a little bigger. Let's say there is a container here. I want to store a value in it, let's say false. And to represent it, I give the name of an identifier. Let's say B. I have given a simple name for B. Although we should give a better name. But for now, let's assume B is its name. So B is an identifier in which the value is false. And here is our container. So, I am saying that first of all, the name of that identifier will come here. So, here I will give b equal to. Here will come its value which is false. And here will come the name of this data type. So, the name of this data type is Boolean. So, this way we represent Boolean b equal to false.
So, what did I say? There can be two possible values. Either the value can be true or the value can be false. So, it is exactly the same. Either the value can be 1 or the value can be 0. Either the current will go or the current will not go. But, the people who come from C, C + + background, specifically for them, in that language, in C + +, any number, if it is positive, means if any number is not 0, let's say 1, 2, 3, any number, It means true and 0 means false. But in Java, it's not like that. Those who haven't read C + + can ignore it. But to tell you, in Java, boolean means only true and false.
I hope you understood all these things. How many data types are there? We saw integers, floating, characters, and boolean. After this, let's move on to the code and look at everything carefully. Before that, let me tell you one more term. What did I say? What is a variable? It's nothing, it's a container in which a value is held. Let's say I have held a value of 12 here. So this is our container, which is also our variable. Now there is a name for this variable, let's say it has a name of b. And what do I call this b? Identifier, by which I identify it. There is one more word, the value which is stored inside 12, I call it literal. I call it literal. Again, this is also a programming term, it is the same in every language. So, you will understand the meaning of literal. Literal means the value which is stored literally. Identifier means the way we represent it. That's it. If you understood this much, then let's see the code of this.
Now again we have created a demo.java file and I told you that you don't have to pay attention to this code, you have to pay attention to what we write inside. So whatever is written here, you have to pay attention to this only. First of all, we will read the integers. Let me tell you what I have written. This is called a comment. What does comment mean? I want you to understand whatever I am writing in the compiler. Whatever I have written here, you don't have to read it. This is a comment that I want to tell you what's going to happen in the code. It's very simple, it's a single line comment. If I want my comment to go to multiple lines, then there's a way for that. Something like this. This is a multiple line comment. I can write anything here. This will remain green. This will ignore my compiler. This is only for us humans to understand. And this is in every language. If you understood the comment, let's talk about integers. In integer, we have byte, short, int, Next we have long. So let's declare one by one. First of all, let's say I have to hold 10 values in a container.
First of all, the name of the data type will come, let's say byte. After that, the name of the identifier, now I call it b, then equal to. So whatever value I have to hold, let's say 5 and then exclamation. So this is the syntax. Byte b is equal to 5. Now since the range of b is from minus 128 to plus 127, so 5 can come here. Same, if let's say, short comes on the byte. I wrote short here as s equal to, if let's say I hold it here as 10. Now, 10 can be written in bytes as well, but it's okay, I've kept it short. Now, I can write an int like this, i equal to, let's say, 20. Now, let's make 20 a little bigger, let's write it as 4000. Now, this 4000 can't be written in bytes, but it can be written in short, but still, I've written it in int. And similarly, I can write a long, l, which is equal to, let's say, 10,000. Or something like that. Okay, so here I have cleared all the variables. B, S, I, L. One is of byte, one is short, one is int and one is long. So let's print them on the screen and see how they are printed. For that I have written system.out.println. If you don't need to focus on this command for now, that what is this? You just assume, if you write code yourself, if you are writing all this for the first time, that you have to write this to get something printed on the screen.
Now look here, I am writing integer values. This will be printed on the screen as it is. And we will understand this syntax one by one, but for now you can ignore this syntax. What I am doing here is that I want to print all these integer values one by one. Okay, so let me write it quickly. Look, what I did was, integer value arrow and what will be printed on the screen? B value, S value, I value and L value. And a comma will come in between the four. Now, as I ran Java's compiler last time, what I did first was javac demo.java and then javademo. This time, I won't do such a long process. There is a simple run button in our VS code. As soon as I press it, my program will run on its own. Look at this. The values are 5, 10, 4000 and 10000. I had to print this and my values are here.
Okay, there is a red line here, which means there is an error. If I hover over it, it will tell us that type mismatch cannot convert double to float. We will see in the next video what this error wants to say, but for now, let's assume that whenever you represent a float number, you will have to put F in front of that number. And why will you have to put it?
Because all the processors of today are optimized for double. Everyone wants you to use double. But if you write a simple number, 10.54, write a literal without putting F, understand that it is a double. But you are storing double in float, which is an error. Later we will see why there is an error in the future. But you assume that the compiler will think that whenever a decimal number comes, it will be a double. But you forcefully want to save it in float. So for that, you have to put a keyword F.
Now we discuss our next numbers, which were our characters. and character I told you is char c let's say I take its name and here I can put any character like a in single quote a ok now if I want to print this then what I will do is system.out.println and here I will put c first we will do character values and here will come c and we don't have to print any d here Now I'll run it. See, our character is also A. The compiler will know what I told you earlier. We will first change A to an integer value and then the compiler will convert it to binary and store it. But when we are retrieving it, when I write here, bring me the value of C, then what will happen first? First, the compiler will go to that container and see that there is a number stored there. But now it has to find out if I want to we have to convert it to its character, the table of unicode, that which character has which number.
Sorry. Yes. And we get the output in the decimal number system. 5, 10, 4000, 10000. What I can do is, we have two types of number systems. In fact, three types. On which I can denote these numbers. One is obviously binary. One is our octal number system. And one is hexadecimal number system. Number system. Okay, which number system are these? Binary, you know, whose base is 2. Octal whose base is 8 is also used a lot in computer science And hexadecimal system whose base is 16 Now you will say, where is this used? You will slowly see, we hold many things in hexadecimal. We hold many things in octal. And in binary, many things are already held. In fact, all the memory that the computer understands is in binary. But we can use these three numbers here also. I mean, I can write this 5 in binary as well as in octal and hexadecimal. Same with 10, 4000 and 10,000. How do we write it?
Let me show you a binary version. Let's say I want to store 5 in binary. First, what do we call 5 in binary? We call 5 as 101. If you don't know, you can do it and see that 101 will come. So, I have to tell the computer that 101 is actually 5, not the exact number 101. Because the computer will understand that we have written 101 in decimal. Because by default, you write all the numbers in decimal.
This 0 is also a keyword, basically if you put this, then the computer will understand that whatever will come after this will come as a binary number. So if you look at it carefully, if I run it again, then it will print 5 again. See, because it understood that by putting 0 also means 101 is a binary number. Now in the same way, let's say I have.
This is our binary, in the same way I have it. I can also represent it in octal. Okay? So if I want to represent 5 in octal, what does octal mean? Its base will be 8, which means it will be allowed from 0 to 7. And 5 comes in that range. So I can write 5 directly. I just have to mention 0 before that.
8 is not an octal number because we have only 0 to 7 allowed. Similarly, if you write simple 2 in binary, then 2 does not exist in binary. In binary, only 0s and 1s exist. So see here the error came. This literal 0, 8 of type int is out of range. Because it is out of the range of 0 to 7. So here I have I have to write something between 0 and 7. If I want to write 8, then I have to play with them. Like how we write 2, we write 1, 0.
So you can do that too. And same, I can write byte b in hexadecimal format too. Let's comment this first so that we have only one variable. Now let's say I want to represent 5 in hexadecimal. For that I have a format that I mention 0x first and then write 5. Now whenever you write 0x, the computer will understand that this number is a hexadecimal number. Here this number was your octal number. And here this number was your binary number. And this was your simple decimal number. So hexadecimal number is 0x5. If I still run it, then it will print 5. But the special thing about hexadecimal is that its range is from 0 to 15. That is, even for 10, 11, 12, 13, 14, 15, hexadecimal has made special symbols for this. And what are they? First, 0 to 9 is simple. Then there are A, B, C, D, E and F. These are 10, 11, 12, 13, 14 and 15. That means if I want to write 15 in hexadecimal, then I can write 0xf.
So this is hexadecimal format. So I just told you, this is very less used. Obviously, you generally use decimal format only. Generally, yes, you use this format. But I told you that you can store numbers in hexadecimal, octal and binary format. And this is not only for byte, short, int, long, float, double, it is applicable for all. In fact, If you are using hexadecimal or octal number system in decimals then it will be very complicated. Because first it will convert its integer part and then its decimal part. So it will be very confusing and generally we don't even do that. But it can be done, you should know. In real numbers, when you write a type of number 10.54, 23.0987. So I told you that float is a single precision. Sorry, single precision. And your double is your double precision.
Our next method is the scientific way. So in double D, I can add a value in a scientific way as well. Let's say I want to get a number of 6.022 x 10 to the power 23. By the way, this is a constant. Physics and chemistry used to use this constant. So you must have read it. Tell me which constant it is. 6.022 x 10 to the power 23. So if I want to hold this number, our scientific way is to write it like this.
You can put an underscore in between So that I can understand it in an Indian way And the compiler will ignore this underscore And if you print it again, it will be printed at the same time And this is a very interesting way If you want a big number to be readable Because you can't put a comma But you can put an underscore Now it is easy to read So I understood 12 crore, 34 lakh, 56 thousand I can read something like this And you can do the same thing in floating, double, etc. In fact, no matter how many underscores you put, it will ignore all of them. You can't put an underscore after e, you can't put an underscore before e, you can't put an underscore after decimal, and you can't put an underscore before decimal. Something like this. This will be wrong. You can put an underscore here, no problem. See, all the numbers will be printed as it is. It was a small thing. Generally, it would be useful if it is a big number. So, let's go. I hope you understood all these things. Now, quickly, there are 2-4 small things left. Let's see them more and finish this lecture. Now, the small thing that was left was that what we were doing till now. What we said was that we have a container. And you can hold a value in that container. Let's simply give the name of that value as x. Sorry, we give identifier as x and value as 5. But till now, we were putting the value in the code by itself. We used to write int, let's say if it's an integer type, x equal to 5. We used to tell earlier which value we are going to hold. But it's not necessary to tell us the value earlier every time. So, if you are making a calculator, then you will get two values, first number and second number. But that first number and second number will be entered by.
Value will not be hard-coded. That value will come through the user, right? So, in that case, what we do is, its syntax changes a little. Now, we call it declaring and defining at the same time, right? The syntax we were following till now was declaring and defining a variable. Now you will understand how declare and define are different when we declare and then define. So let's say if I want to make a variable of int type and I just write int x.
I have to make a container The name of the container will be x And an integer type value is going to come in it So obviously an integer type value is going to come, which means 32 bit memory is going to come 32 bit maximum is going to be used So what will it do? It will hold 32 bit in your memory Now in the middle, in the code, you will take the input of x from the user. Now we will see how to take the input later. But you will take the input from the user. So when you take the input, then that value will be stored here.
When a programming language is made, if you put a first thought principle, when a programming language is made, then it will say that I will need some words. For example, what did we see just now? We saw that when we used to print something, then we used to write system.out.out. println, we used to write something like this, and whatever we had to print here, so these words, like system, out, println, right? Similarly, if you have seen, in Java's code, there were many other things written, like class, public, static, void, there were many things.
What are the words that Java has reserved for us? When Java was being made, the people who were making Java, they reserved some words that were never used. Like goto and const. These are the keywords that Java has never used. But it was reserved. And the reason for it to be reserved was this. If you come from C, C + + background, then you know well. That these are used in C, C + +. So Java thought that I will also use this somewhere. But later, Java's entire language was made. Without using these keywords. It made different keywords. So this is an interesting fact. You can't use it even now.
