Monday, November 28, 2022

How to learn Data type | Java Tutorial for beginners

 In last tutorial we learn how to create class in eclipse and print hello world program. Now in this tutorial we will learn Java Data type step by step by practical example. 

Like other languages java has different data type, context is same but there may be change in syntax  and we will learn them one by one.  

Lets create class, It the src folder right click and click on new provide name in this example we give name as datatype . checkmark on public static void main so it i will create main method in our class. and click on Finish. 

Now check in editor , we can observe we have class name data Type and we have main method , we can ignore comment or remove it. If you want to learn by watching here is link of step be step video tutorial data type Java program for beginners in Hindi and English language. Please click on link.

Java Data Type Tutorial Video in English 

Java Data Type Tutorial Video in Hindi






Data Type: 

java Integer

In java we have to let system know what type of data type are we using. Lets start with int. we create variable , which hold data , so we will type int and we created variable name a and we store value of 5. so what is cool about variable every time we change value we do not have to change else where , we just use variable because it holds value. As we observe in below picture what would be output in console? you get it . it's 5 . click on green error button (Run) . but before that make sure you have saved it.  

Console out put

Now what if you change a =10 and rerun program, what would be output? yes it would be 10. 

Java Double

Now lets create another variable b , and set datatype integer and assign value as 1.5 (int b = 1.5) . Hmmm now we get error message now either we have to cast or we have to change type now change datatype as double instead of int , now you can see error is gone . it means if number is not whole we like 1.5 we should use double . now run program you will get out put as shown in below image


Java Character
Now it is correct time to introduce char, what would be best example? if teacher gives score to student like a, b, c ,d, f. we can use data type as char as describe in example , save it and Run. 
Java String


If we would like to use sentence and would like to print it what datatype should we use? Answer is String lets see how to use string in java
we will type String as data type and we create variable which hold some kind of value and we assign as = sign. but unlike char we will use double quotation in string while in char single. In below example we created variable name and type of string which hold value(John) now save , run and observe in console. we get our correct value. 

Java Boolean
Now we will learn Boolean . Boolean is true or false. And we can use boolean to declare value. In this example we have set boolean as true and we printed in console. 
Why we have so many data types?
well these are not only the type there are byte, short, long, float etc . All have different size and capacity. If it is byte it would be size of 1 byte and can store whole number from -127 to 127. If we use short it will take 2 byte and we can store whole number from -32768 to 32767. If we use in as data type it will take 4 byte and can store number -2147483648 to 2147483647, for long it will take 8bytes and can store value from whole number -9223 372 036 854 775 808 to 9223 372 036 854 775 807.Now you can understand we can use same datatype for multiple thing but it will take more memory , that is why it is important to use wisely . 
hope you like this tutorial if you would like to know more, please click on below link.
https://www.youtube.com/@amazingcodingtechworld 

java switch statement | Java Tutorial For Beginners

 Switch statement is alternative and more cleaner way to write code if we have more than two options. we can use else if, this is better way...