Java Tutorials - Different Data Types.

in #steemstem7 years ago (edited)

Hi Steemians!!! Welcome to our next Java programming tutorial. In last tutorial we have seen how we can install Eclipse IDE and we have run our first program. Today we will see different data types in Java and how we can declare them. So, let's start.

IMG_20180214_HH2655.png
Image created using canva.com

Various (primitive) data types in Java :

Data types in Java can be mainly classified into 4 different aspects as follows :

  1. Integer
  2. Float
  3. Character
  4. Boolean

1) Integer :

Integer is simply used to store a numerical value. So, this numerical value can not have decimal points. For decimal points we have float. So, any numerical value that does not have decimal point value can be used to store in integer. Again with the integer type we have 4 different types

a) byte (1 byte)
b) long (8 bytes)
c) short (2 bytes)
d) int (4 bytes)

Based on the numerical value which you are going to be storing you can use either of these. in byte you can store a number that is between -128 to +127 but if you look at the 'int' occupies 4 bytes, so, with 'int' you can store a value that is between -21 hundred crores to +21 hundred crores. It's a huge number. So, we mostly stick to integer itself but based on your need you can use 'long' as well. So, based on your need you can use the corresponding one.

Eg.: int num = 57

So, I am creating a numerical value of integer type that is going to store a value 57.

2) Float :

Float type can be used to store decimal pointed numerical value. Again, based on your need of storing the number you are going to use 'float' (4 bytes) or 'double' (8 bytes).

eg.: float num = 98765.4321

3) Character :

Apart from numerical value if you wish to store character then you have 'char' type.

eg.: char ch = 'A'

To store any of the alphabetical character you can use this data type. Apart from that you can store special character and numerical values as well but they will be considered to be a character value.

4) Boolean :

Boolean is very straightforwad. With boolean you can store either true or false value.

eg.: boolean b = true

Now, let's see an example to use these data types.

You own a Retail department store and you need to create a bill with the following parameters :

  • Invoice ID
  • Product cost
  • Product ID
  • Quantity
  • Discount
  • Total
  • feedback ?

So, which Data types you have to use to create a bill?

Answer :

  • Invoice ID - integer
  • Product cost - double
  • Product ID - integer
  • Quantity - integer
  • Discount - double
  • Total - double
  • feedback ? - boolean

Now, let's see how to declare these values in the Eclipse IDE.

The first thing you need to do is you need to create a new project. On the left most icon go to file > new > java project.

datatype1.png

Here, you need to specify the name of your project (ex.: JavaDataTypes). And then you can directly click on finish.

datatype2.png

As you can see a new project is created automatically. Inside of this 'JavaDataTypes' folder you can see source folder as well as java system library. This is basically all the libraries to which you can access as part of your Java environment. Now, you need to create a new class file.

To create a new class file right click on 'src' > new > class. Everything that you are going to write is going to be part of a class.
datatype3.png

Here, you need to specify the name of the class. Now, one thing to remeber, as a part of the naming convention of the class, you can not use the spaces. What I am going to do is use underscore ( _ ) instead of space as you can see in the screenshot. Don't make any changes here but make sure you have checked the option 'public static void main'. This basically means that inside your class there is going to be your main function. Now, just click on finish. And you have your class file created as you can see in screenshot below.

datatype4.png

Now you are going to specify integer type. For integer type all you need to do is just type 'int' and system will consider it to be an integer. And you can see it's been highlighted separately because these are reserved keywords. When you use this keywords Java intern understands that this refers to integer. Now, in my integer I had my 'invoice ID' so I will type 'int invoice_id'. Here 'invoice_id' is a variable name. Now, whenever I am going to use 'invoice_id' as a part of my code, it basically means, this is a variable that is of integer type. So, if you have multiple variables of the same type, rather than declaring them individually you can put a comma and start with the next variable name. Now, declare every type you have in above example.

datatype5.png

As you can see in screenshot I have declared all of them. Make sure you put a semi-colon after every statement. That means this is the end of a statement.

So, This is how you define different data types in Java. By default all the integers would be assiged 0, doubles would be 0.0 and your boolean would be set as true.

That's it for today. In the next Java Tutorials article we will see different data operations in Java.

If you have any questions please comment below.

Important :

All the images used in this article are the screenshots taken in my phone and my pc and some of them are cropped using Paint.

References :

  1. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
  2. Book : The Java programming language by Arnold.

Don't forget to upvote and resteem.
Follow @pratique007

Sort:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by pratique007 from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Coin Marketplace

STEEM 0.22
TRX 0.24
JST 0.038
BTC 94692.02
ETH 3236.92
USDT 1.00
SBD 3.29