Learn variables and data types in java programming with super simple example and explanation.
DATA TYPES IN JAVA
Here are some common data types we use in java programs. int, float, char, double, Boolean and String.
Data type is a classification of data e.g. int, float, double, char and String etc. by which a programmer and compiler understand how to use them.
For example,
- numbers 1,2 and 3 etc. is an int data type.
- 1.00, 12.123 and 25.9 etc. are double type.
- In double quote “hello” is an String data type etc.
So, this is the way you understand what data type we need to use in the program depending upon the requirement.
Now, let’s say if I ask you what data types of a student name, marks and roll number can be, then easily you can say that the student name should be String type, Marks should be of double data type and roll number can be of int type.
And, a compiler can understand easily, how much space / memory in bytes, it must allocate for String types or double data types and int types to process them.
Also, data types will help you understand which type of variable you want to create to store values.
So for, you have an idea of what is data types in java programming. Let’s understand what is variables and how to create a variable in java programs.
VARIABLES IN JAVA:
We create a variable to store a value. Here is the format to declare a variable.
data type variable name;
You have to write a data type e.g. int , double etc. before variable name, for which type of data you want to store.
Example: int var; // var is a variable of type int. It will store only int values.
More variables examples – If you want to store 72.30, then you can create a double type of variable, if 1234, then int type of variable, “Peter” then String type of variable as shown below
String name = “Peter”; //name is a String data type of variable.
double marks = 72.30; // marks is a double type of variable.
int rollNumber =1234;// rollNumber is of int type of variable.
Lets see a simple program which contains multiple types of variable and has stored the values of respective types.
public class Sample {
public static void main(String[] args) {
// name variable is of String data type
String name = "Peter";
// marks variable is of doubl data types
double marks = 70.56;
// roll number is of int data types.
int rollNumber = 234567;
}
}
In above example, variables of different data types have been created and initialized with respective type of values. Also, you can declare a variable first and then assign a value later as shown below in the java source code.
public class Sample {
public static void main(String[] args) {
// Declare variables
String name;
double marks;
int rollNumber;
// Assign values to variables
name = "Peter";
marks = 70.56;
rollNumber = 234567;
}
}
Exercises on Variables and data Types in Java
Exercise:1
Exercise: Write a program to create two variables, one for your name and another for your age. Assign your name and age to respective variables and display them on console screen.
Exercise:2
In main () method, first declare a variable called radius of double data type, then in next statement assign a value 4.5.
Print the value of variable on console.
Exercise:3
Student record system contains following information’s about a Student.
- Name
- Roll number
- Course
- Tuition Fee
- Library Fee
- City
- Zip Code
In below student record system program, variables for all information given above have been created with respective values.
The data types of variables in java program is shown by question mark(?). Can you write the correct data types for them?
public class SudentRecord {
public static void main(String[] args) {
//Write data types of following variables
? name = "John";
? rollNumber = 1234;
? Course = "Java";
? tutionFee = 1000.99;
? libraryFee = 120.50f;
? City = "Delhi";
? zipCode = 234567;
}
}
Exercise:4
What is output of below program?
public class Sample {
public static void main(String[] args) {
int x = 10;
System.out.println(x);
x = 10 + 20;
System.out.println(x);
}
}
SOLUTIONS
Exercise:1
Solution
public class Sample {
public static void main(String[] args) {
String name = "Peter";
int age = 20;
System.out.println(name);
System.out.println(age);
}
}
Output:
Peter
20
Exercise:2
Solution:
public class Sample {
public static void main(String[] args) {
double radius;
radius = 4.5;
System.out.println(radius);
}
}
Exercise:3
Solution:
public class SudentRecord {
public static void main(String[] args) {
String name = "John";
int rollNumber = 1234;
String Course = "Java";
double tutionFee = 1000.99;
float libraryFee = 120.50f;
String City = "Delhi";
int zipCode = 234567;
}
}
Exercise:4
Output:
10
30