Data types in Java
let's discuss how many types of data types are present to implement in java, As we all know that data types are classified into two categories-
- Primitive data type
- Non-primitive data type
1. Primitive data type: It consists of eight different data types which are listed below-
1. byte ( 1 byte )
2. integer ( 4 bytes )
3. float ( 4 bytes )
4. double ( 8 bytes )
5. short ( 2 bytes )
6. boolean ( 1 bit )
7. character ( 2 bytes )
8. long ( 8 bytes )
Representation of primitive data types-
- byte x=2;
- int age =10;
- float rateOfInterest = 2.3f;
- double rate= 3.14281;
- short small Values = 5;
- boolean isThisSeriescool = true;
- char my Character = '@';
- long myLong = 2343434378L;
2. Non Primitive data type: It includes class, arrays and strings, let's give some light to each of them-
1.Class: It is simply a virtual thing which does not exist actually but it's subsidiaries exist, it does not takes any memory (a blueprint of an object ) in our system, In other words it is a collection of objects(which have similar behavior) to better understand what class is let us take an example laptop is a class which contains different types of laptops like ASUS, acer, hp, dell, Lenovo, apple, Microsoft(objects) similarly fruit can be a class which contains different types of fruits like apple, mango, pineapple, grapes, banana, guava(objects) etc. In short it is a user defined data type. It contains data(Fields) and methods(functions), constructors and other class too. have a look on this code to better understand how class is implemented in java-
Def. Class: A class is a virtual thing which does not exist actually but it's subsidiaries exists it is a blueprint of an object in order to define objects you need to declare a class first, it can contain fields, methods and constructors.
In order to understand how class is actually created let's see directly in a code-
public class Myclass {
int number;
char alphabet; //these are fields or data
public String name = "Tech4you";
public double mydouble = 6.7878787878;
public long mylong = 6756565656l;
public Myclass() //this is a constructor
{
System.out.println("i am inside a constructor");
}
public static void method1() //this is a method(function)
{
System.out.println("i am inside a method");
}
public static void main(String[] args)
{
Myclass m = new Myclass();
System.out.println(m.name);
System.out.println(m.mydouble);
System.out.println(m.mylong);
Myclass.method1(); // do not confuse this is a static method of calling method1 because
// method1 is a static method you can also call it by making object
// of Myclass //hope you understand this
}
}
output:
i am inside a constructor
Tech4you
6.7878787878
6756565656
i am inside a method
i am inside a constructor
Tech4you
6.7878787878
6756565656
i am inside a method
2. Arrays: let us understand what an array is if you want to store multiple values of the same type we can use array, by declaring a single variable name you can store multiple values of the same type like mobile numbers, roll-numbers, Aadhaar-card number, passport number etc. And it stores values in a contiguous fashion like first values is stored at 0th index second value is stored at 1st index which makes it
easier to access it's elements have a look on this code to better understand how it is implemented in java-
Def. Arrays: It is a collection of similar type of elements stored in a contiguous manner having a fixed length which is given by the user at the time of declaration. It can be used to store primitive and non primitive data types both.
public class myArray {
public int[] aadhaarCardNumber = new int[5];
public int passportNumber[] = new int[5]; //this is the declaration of an array
int roll_Number[] = new int[5];
public void showNumbers()//method
{
int aadhaarnumber=676767671;
for(int i=0;i<5;i++)
{
aadhaarCardNumber[i]=aadhaarnumber;
System.out.println("the aadhaar number of "+i+"person is"+aadhaarCardNumber[i]);
System.out.println();//for a new line
aadhaarnumber++;
}
}
public static void main(String[] args)
{
myArray m = new myArray();
m.showNumbers();
}
//similarly you can also show roll numbers and passport numbers
}
output:
the aadhaar number of 0person is676767671
the aadhaar number of 1person is676767672
the aadhaar number of 2person is676767673
the aadhaar number of 3person is676767674
the aadhaar number of 4person is676767675
3. Strings: It is an array of character(data type) simple as it is now you are wondering where to use strings suppose you have to store a student name which is basically a collection of characters you can use string in that case, It is a class in java which contains different functions like touppercase(), tolowercase(), length(), charAt(), indexOf() have a look on this code to better understand how strings are implemented in java-
Def. Strings: It is a sequence of characters stored in an array. In java strings are objects and there are two types to define strings one is string literal and other is string object the preferred one is string literal which is immutable ( immutable means un-changeable whenever a another string is created having the same value as of the previous one the other instance is not created ) helps to reduce memory allocation.
public class myStrings {
public String blogName = "Tech4you";
public String bloggerName = "Arbaab-khan";
//either you can use this
public char java[] = {'l','e','a','r','n','i','n','g'};
//or you can use this which means same
public String myjava = "learning";
public static void main(String[] args)
{
myStrings m= new myStrings();
System.out.println(m.blogName);
System.out.println(m.bloggerName);
System.out.println();
System.out.println(m.java);
System.out.println(m.myjava);
}
}
output:
Tech4you
Arbaab-khan
learning
learning
Good job arbaab Khan..
ReplyDeleteKeep doing . .