8. Datatype and Variable
Datatype
It is a container where we create variable to store data.
There are two types of datatypes :-
1) Primitive Datatype :- Primitive datatype helps the programmer to create primitive type variables which helps us to store primitive data.
2) Non-Primitive Datatype :- Unlike primitive data types, these are not predefined. These are user-defined data types created by programmers. These data types are used to store multiple values.
There are five types of non-primitive data types in Java. They are as follows:
- Class
- Object
- String
- Array
- Interface
Variable
Variables are the container where we store data in java we create variable with the help of datatypes.
[Note - In java we cannot use a variable without declaring.]
Syntax - Datatype variablename ;
byte age ;
Variable Initialization statement :- Assigning value to create variable is known as variable initializing statement.
ex - age = 10 ;
grade = '*' ;
salary = 5.00.00f/f ;
Example for data declaration -
class var1
{
public static void main (String[]args)
{
int num ;
char grade ;
float salary ;
}
}
## For Example, declaring and printing variable -
class var2
{
public static void main (String [] args)
{
int number = 123456 ;
char grade = ' * ' ;
float salary = 5000.00f ;
System.out.println(number) ;
System.out.println(grade) ;
System.out.println(salary) ;
}
}
Comments
Post a Comment