A blog for computer science passionates.

Thursday 20 February 2020

Java: Datatypes Example

Hello Friends!!!
Today we are going to learn the example of datatypes. Please refer to Datatypes to get all the details of Java Datatypes.

Let's start with What is variable?
Variable (scalar) is a named memory location (Name given by a programmer or developer to identify that memory location for their purpose is also known as Identifier) we can store a value to a variable it is temporary storage for a value.

In Java, we can define a variable with its type. Here is how to declare a variable?
<data_type> <var1>,<var2>,<var3>;

For Example,
int employee_id, length;

Even we can assign a value to the variable such as,
<data_type> <var1>=<val1>,<var2>=<val2>;

For Example,
int employee_id=123,length=5;

Now let's take a demo class to define a variable with different data types.

Here is the output of the above example,
Fig. The output of the above example
This is the simple example of data types, that shows how to define variables with different data types and how to assign values and how to access values from the variable.

Here, in long and float data type we need to add L or l or F or f after values so that values can be considered as long or float otherwise it is considered as integer or double value.
In float datatype, if we don't write f or F, it shows an error.
float f1=45.567;    //if we write this line, it generates below error.
 
Fig. An Error for float data type
This is an example of Data Tyes of Java. In the next article, we will learn something new in Java.
Till then that's it, Happy Coding...☺😊

No comments:

Post a Comment