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.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//wap to demonstrate different data types in Java. | |
class DataTypes{ | |
public static void main(String arg[]) | |
{ | |
long a=5L; | |
short s=12345; | |
int n=3434354; | |
float f1=45.567f; | |
boolean b=true; | |
char c='a';byte i=65; | |
double d=1234.4564; | |
System.out.println("Byte I="+i); //Original value | |
System.out.println("OR Byte I="+(char)i); //After Casting | |
System.out.println("Short s="+s); | |
System.out.println("Integer n="+n); | |
System.out.println("Long a="+a); | |
System.out.println("Float f1="+f1); | |
System.out.println("Double d="+d); | |
System.out.println("Boolean b="+b); | |
System.out.println("Character c="+c); | |
} | |
} |
Here is the output of the above example,
![]() |
Fig. The output of the above example |
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 |
Till then that's it, Happy Coding...☺😊
No comments:
Post a Comment