Java Datatypes :-
Java datatypes are two types:-
Primitive Datatypes :-
2.
short
3.
int
4.
long
5.
float
6.
double
7.
char
8.
bool
1) byte
public class num
public static void main(String[] args) {
byte number=126;
System.out.println(number);
}
}
OUTPUT :
126
2) short
public class num
public static void main(String[] args) {
short number=3278;
System.out.println(number);
}
}
Output:
3278
3) int
public static void main(String[] args) {
int number=2225555525;
System.out.println(number);
}
}
Output:
2225555525
4) long
public class Num
{
public static void main(String[] args) {
long number=3274789658985548;
System.out.println(number);
}
} output: 3274789658985548
5) float
float is to store decimal numbers
ex:
public static void main(String[] args) {
float value2 = 9.87f;
System.out.println(value2);
}
}
o/p: 9.87
6) double
ex:
public class number
{
public static void main(String[] args) {
double num=5.5; // double num=5.5d;
System.out.println(num):
}
}
5. 5
{
public static void main(String[] args) {
char var='a';
System.out.println(var);
}
}
o/p: a
8) bool
true / false
public class Example
{
public static void main(String[] args) {
boolean num = true;
boolean alpha = false;
System.out.println(num); // Outputs true
System.out.println(alpha);
}
}
o/p: true
false
Non Primitive datatypes:
1) 1) Classes & objects:
Ex:
public class Main{
int a = 20;
int b = 10;
int c;
public void add () {
int c = a + b;
System.out.println("Addition of numbers is: " + c);
}
public void sub () {
int c = a - b;
System.out.println("Subtraction of numbers is: " + c);
}
public static void main (String[] args) {
// creating the object of class
Main obj = new Main();
// calling the methods
obj.add();
obj.sub();
}
}
o/p:
2)interfaces:
Ex:
interface Pet{
public void test();
}
class Dog implements Pet{
public void test(){
System.out.println("Interface Method Implemented");
}
public static void main(String args[]){
Pet p = new Dog();
p.test();
}
}
o/p:
Interface Method Implemented
Ex2:
public void animalSound();
public void sleep();
}
class Pig implements Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
public void sleep() {
System.out.println("Zzz");
}
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
o/p:
The pig says: wee wee
Zzz
3) Arrays
An array is a data type which can store multiple homogenous variables i.e., variables of same type in a sequence. They are stored in an indexed manner starting with index 0. The variables can be either primitive or non-primitive data types.
import java.io. * ;
import java.util. * ;
public static void main(String[] args) throws IOException {
int i;
Scanner ss = new Scanner(System. in );
int arr[] = {1, 2, 3, 6, 9};
int arr1[] = new int[5];
System.out.println("Enter the numbers (size = 5) :");
for (i = 0; i < 5; i++) {
arr1[i] = ss.nextInt();
}
System.out.println("Previous array with initialized size is: ");
for (i = 0; i < 5; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("\nThe new array we have entered is:");
for (i = 0; i < 5; i++) {
System.out.print(arr1[i] + " ");
}
}
}
Type Casting
1) Widening Casting :[automatically]
{
public static void main(String[] args) {
int num=9;
double mynum=num;
System.out.println("before :"+num);
System.out.println("after:"+mynum);
}
}
o/p:
before :9
after:9.0
2) Narrow Casting :[manually]
Converting a large datatype to a smaller datatype.
{
public static void main(String[] args) {
double mydouble=9.78d;
int myint=(int) mydouble;
System.out.println("before:"+mydouble);
System.out.println("after:"+myint);
}
}
o/p:
before:9.78
after:9
Range of datatypes:
We can find range of datatypes by using formula= 2n-1 to 2n-1-1
Here, n is number of bits.
§ Byte has 8 bits
i.e., n=8
28-1 to 28-1 -1 (i.e., -128 to 127)
§ Short has 2 bytes = 16 bits
i.e., n=16
216-1 to 216-1 -1 (i.e., -32768 to 32767)
§ integer has 4 bytes = 32 bits
i.e., n=32
232-1 to 232-1 -1 (i.e., -2147483648 to 2147483647)
§ long has 8 bytes = 64 bits
i.e., n=64
264-1 to 264-1 -1 (i.e., -92233472036854775808 to 92233472036854775807).
0 Comments
Got questions? Feel free to ask!