Data types in Java
Let's discuss how many types of data types are present to implement in java. As we all know, 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 smallValues = 5;
- boolean isThisSeriesCool = true;
- char myCharacter = '@';
- 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 a blueprint of an object. It does not take memory itself but its objects do. In other words, it is a collection of objects (which have similar behavior). Example: A "Laptop" class can contain ASUS, Acer, HP, Dell, Lenovo, Apple (objects). A "Fruit" class can contain Apple, Mango, Pineapple, Grapes, Banana, Guava (objects). In short, it is a user-defined data type. It contains fields (data), methods (functions), constructors, and other classes too.
Def. Class: A class is a virtual thing (blueprint of an object). To define objects you first declare a class. It can contain fields, methods, and constructors.
public class Myclass {
int number;
char alphabet; // fields or data
public String name = "Tech4you";
public double mydouble = 6.7878787878;
public long mylong = 6756565656l;
public Myclass() { // constructor
System.out.println("I am inside a constructor");
}
public static void method1() { // method
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();
}
}
Output:
I am inside a constructor
Tech4you
6.7878787878
6756565656
I am inside a method
2. Arrays: Used to store multiple values of the same type in a contiguous fashion. Example: mobile numbers, roll numbers, Aadhaar-card numbers, passport numbers, etc.
Def. Arrays: An array is a collection of similar type elements stored contiguously. It has fixed length, declared by the user, and can store both primitive and non-primitive types.
public class myArray {
public int[] aadhaarCardNumber = new int[5];
public int passportNumber[] = new int[5];
int roll_Number[] = new int[5];
public void showNumbers() {
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();
aadhaarnumber++;
}
}
public static void main(String[] args) {
myArray m = new myArray();
m.showNumbers();
}
}
Output:
the aadhaar number of 0 person is 676767671
the aadhaar number of 1 person is 676767672
the aadhaar number of 2 person is 676767673
the aadhaar number of 3 person is 676767674
the aadhaar number of 4 person is 676767675
3. Strings: A string is an array of characters. It is a class in Java with many methods such as toUpperCase(), toLowerCase(), length(), charAt(), indexOf().
Def. Strings: A sequence of characters stored in an array.
In Java, strings are objects. Two ways to define:
1. String literal (preferred, immutable).
2. String object (using new
keyword).
Immutable means once created, the value cannot be changed; helps reduce memory usage.
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 this, same meaning
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 . .