Data Types in Java - byte, short, int, long, float, double, String| Primitives and Non-Primitives

preview_player
Показать описание
Episode - 7: Data Types in Java
Java’s Alphabets
When we learn a language, we learn its alphabets first, making words from these alphabets and finally sentences. Similarly, in Java lets learn its alphabets first. We are starting from very basic. Let us say we have to do something with numbers in Java, then we are provided with a few pre-defined options.
When we were kids, we learnt counting from 1-10 first. In java we will learn from -128 to 127 first. This range is called byte. As we are already familiar with counting, so we can proceed further. Now we want to extend our range. Suppose we need to use numbers greater than 127 in our java program, then what to do? Next range comes Short -32768 to 32767. If this doesn’t sound enough then Java has Int which has range from -2147483648 to 2147483647. If your requirement is still not satisfied then you can go for Long and it ranges from -9223372036854775808 to 9223372036854775807. You can use any type of number as per range you require.
Have you noticed one thing here? The numbers we just saw were whole numbers. There was no decimal point. Now what to do if we need decimal number? Java has altogether new range for decimal numbers beginning with float. Its range lies between 3.4e−038 to 3.4e+038. If this seems small, then we have next range of decimals as Double (1.7e−308 to 1.7e+308). Apart from range these decimal data types vary in precision also. Precision is basically accuracy or exactness. Double has precision of 15 digits whereas float has only 6 digits precision. It means that double number can have 15 digits after decimal point and float can have 6.
Now all the numbers are covered whether its integer, whole number or any kind of decimal number. Here comes another data type Boolean which is a special data type that can have only true/false values. This data type is very important and useful for testing conditions.
Next comes char, meaning characters. Any symbol, any alphabet any special character like?, @, #, $, all these are chars. Chars are represented by single quotes. All these data types were of basic type. These are categorized as Primitive data types. There is one more category of data type which is advanced one and known as Non-Primitive.
Non primitive types are defined by us developers which we will discuss later. But there is one exception for this rule. There is one in-built Non primitive data type which is String. Collection of chars is known as String or in simple words any text inside double quotes is String. Strings are represented by double quotes, like the one we used in our first Java program. If you haven’t watched that video yet, please watch so that you can have basic idea about Java programming and understand data types more.
Now we have learnt different data types but how do we use them? Let’s us understand their usage.
But wait, how Java will understand this firstNumber, we just called it by a name ourselves. How we can tell Java that what this is? We have just learnt our data type, right? These numbers are simple integers, so we can use our first datatype byte here. We tell Java that firstNumber is byte and java will understand. Java is so smart. Now put that number 5 in this byte firstNumber like this:
byte firstNumber = 5;
Here we assigned the value 5 to firstNumber of datatype byte. firstNumber is called variable to which we can assign value, type we already have defined. Similarly, define secondNumber and assign it value 10.
Byte secondNumber = 10;
In this way we can define variables of any type and assign value.
Example:
String name = “Kamal”;
Char firstCharacter = ‘K’;
Int anyNumber = 10;
Рекомендации по теме
visit shbcf.ru