DATA TYPES IN JAVA
Information type characterizes the qualities that a variable can take, for instance if a variable has int information type, it can just take whole number qualities.
In java we have two classes of information
type: 1) Primitive information
types 2) Non-crude information types – Arrays and Strings are non-crude information types, we will talk about them later in the coming instructional exercises.
Here we will examine crude information types and literals in Java.
Java is a statically composed language. A language is statically composed, if the information kind of a variable is known at arrange time.
This implies that you should determine the sort of the variable (Declare the variable) before you can utilize it.
In the last instructional exercise about Java Variables, we figured out how to announce a variable, lets review it:
int num;
So to utilize the variable num in our program, we should pronounce it first as demonstrated previously.
It is a decent programming practice to proclaim every one of the factors ( that you will use) in the start of the program.
1) PRIMITIVE DATA TYPES:
In Java, we have eight crude information types: boolean, singe, byte, short, int, long, buoy and twofold.
Java engineers incorporated these information types to keep up the transportability of java as the size of these crude information types don't change starting with one working framework then onto the next.
byte, short, int and long information types are utilized for putting away entire numbers.
buoy and twofold are utilized for partial numbers.
singe is utilized for putting away characters(letters).
boolean information type is utilized for factors that holds either obvious or bogus.
BYTE:
This can hold entire number between - 128 and 127. For the most part used to save memory and when you are sure that the numbers would be in the breaking point indicated by byte information type.
Default size of this information type: 1 byte.
Default esteem: 0
Model:
class JavaExample {
public static void main(String[] args) {
byte num;
num = 113;
System.out.println(num);
}
}
Output:
113
Attempt a similar program by appointing esteem relegating 150 worth to variable num, you would get type confuse blunder on the grounds that the worth 150 is out of the scope of byte information type.
The scope of byte as I referenced above is - 128 to 127.
short:
This is more prominent than byte regarding size and not as much as number. Its reach is - 32,768 to 32767.
Default size of this information type: 2 byte
short num = 45678;
int: Used when short isn't adequately enormous to hold the number, it has a more extensive territory: - 2,147,483,648 to 2,147,483,647
Default size: 4 byte
Default esteem: 0
Model:
class JavaExample {
public static void main(String[] args) {
short num;
num = 150;
System.out.println(num);
}
}
Output
150
The byte information type couldn't hold the worth 150 yet a short information type can on the grounds that it has a more extensive territory.
long:
Utilized when int isn't adequately huge to hold the worth, it has more extensive territory than int information type, going from - 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
size: 8 bytes
Default esteem: 0
Model:
class JavaExample {
public static void main(String[] args) {
long num = - 12332252626L;
System.out.println(num);
}
}
output:
- 12332252626
twofold: Sufficient for holding 15 decimal digits
size: 8 bytes
Model:
class JavaExample {
public static void main(String[] args) {
twofold num = - 42937737.9d;
System.out.println(num);
}
}
output:
- 4.29377379E7
coast: Sufficient for holding 6 to 7 decimal digits
size: 4 bytes
class JavaExample {
public static void main(String[] args) {
coast num = 19.98f;
System.out.println(num);
}
}
output:
19.98
boolean: holds either valid for bogus.
class JavaExample {
public static void main(String[] args) {
boolean b = bogus;
System.out.println(b);
}
}
output:
bogus
singe: holds characters.
size: 2 bytes
class JavaExample {
public static void main(String[] args) {
singe ch = 'Z';
System.out.println(ch);
}
}
output:
Z
Literals in Java
An exacting is a fixed worth that we allocate to a variable in a Program.
int num=10;
Here esteem 10 is an Integer exacting.
singe ch = 'A';
Here A will be a singe exacting
Whole number Literal
Whole number literals are alloted to the factors of information type byte, short, int and long.
byte b = 100;
short s = 200;
int num = 13313131;
long l = 928389283L;
Buoy Literals
Utilized for information type buoy and twofold.
twofold num1 = 22.4;
glide num2 = 22.4f;
Note: Always postfix skim worth with the "f" else compiler will think about it as twofold.
Singe and String Literal
Utilized for singe and String type.
burn ch = 'Z';
String str = "Coding03";
0 Comments:
Post a Comment