Master Java data types - Boolean, Byte, Short, Int, Float, Long, Double, Char - with our concise tutorial. Boost your coding skills with practical examples, perfect for beginners and seasoned developers alike. Explore the essentials, build a strong foundation, and elevate your Java programming expertise.
Data types indicate the specific sizes and values that can be stored in the flexible. There are two variety of data variety in Java programming:
In Java language, untrained data variety are the building blocks of data consumption . These are the most vital data variety available in Java language. Java is a statically-typed programming language. It means, all flexible must be announced before its use. That is why we need to reveal flexible variety and name. There are 8 variety of untrained data variety :
Data Types | Default Value | Default Size |
---|---|---|
boolean | false | 1 bit |
char | '\u0000' | 2 byte |
byte | 0 | 1 byte |
short | 0 | 2 byte |
int | 0 | 4 byte |
long | 0L | 8 byte |
float | 0.0f | 4 byte |
The Boolean data variety is used to store only two probable values: true and false. This data variety is used for elementary flags that track true/false conditions.
The Boolean data variety indicate one bit of information, but its "size" can't be prominent precisely.
import java.io.*;
class Simple{
public static void main(String args[]){
boolean a = true;
boolean b = false;
System.out.println(a);
System.out.println(b);
}
}
true
false
The byte data variety is an example of untrained data type. It is an 8-bit signed two's accompaniment integer. Its amount- variety lies between -128 to 127 (comprehensive). Its minimum amount is -128 and maximum amount is 127. Its default amount is 0.
The byte data variety is used to recover memory in big arrays where the memory savings is most required. It recover capacity because a byte is 4 times smaller than an integral. It can also be preloved in place of "int" data variety.
import java.io.*;
class Simple{
public static void main(String args[]){
byte a = 2;
byte b = -4;
System.out.println(a);
System.out.println(b);
}
}
2
-4
The short data variety is a 16-bit signed two's complement integer. Its amount-variety lies between -32,768 to 32,767 (comprehensive). Its minimum amount is -32,768 and maximum amount is 32,767. Its default amount is 0.
The short data variety can also be used to recover memory just like byte data variety . A abrupt data variety is 2 times smaller than an integer.
import java.io.*;
class Simple{
public static void main(String args[]){
short a = 2000;
short b = -2000;
System.out.println(a);
System.out.println(b);
}
}
2000
-2000
The int data type is a 32-bit signed two's complement integer. Its amount types lies between - 2,147,483,648 (-231) to 2,147,483,647 (231-1) (inclusive). Its minimum amount is - 2,147,483,648 and maximum amount is 2,147,483,647. Its default amount is 0.
The int data types is generally preloved as a default data types for integral amount unless if there is no problem about memory.
import java.io.*;
class Simple{
public static void main(String args[]){
int a = 200000;
int b = -300000;
System.out.println(a);
System.out.println(b);
}
}
200000
-300000
The long data types is a 64-bit two's complement integer. Its amount type lies between -9,223,372,036,854,775,808(-263) to 9,223,372,036,854,775,807(2^63-1)(comprehensive). Its minimum amount is - 9,223,372,036,854,775,808 and maximum amount is 9,223,372,036,854,775,807. Its default amount is 0. The long data variety is used when you need a range of values more than those distribute by int.
import java.io.*;
class Simple{
public static void main(String args[]){
long a = 200000L;
long b = -300000L;
System.out.println(a);
System.out.println(b);
}
}
200000L
-300000L
The float data types is a single-precision 32-bit IEEE 754 floating point. Its amount range is unlimited. It is recommended to use a float (instead of double) if you need to recover memory in large arrays of floating point numbers. The float data types should never be preloved for precise values, such as currency. Its default amount is 0.0F.
import java.io.*;
class Simple{
public static void main(String args[]){
float a = -2.00;
float b = 2.00;
System.out.println(a);
System.out.println(b);
}
}
-2.00
2.00
The double data types is a double-precision 64-bit IEEE 754 floating point. Its amount range is endless. The double data variety is generally preloved for decimal amount just like float. The double data type also should never be preloved for precise amount, such as currency. Its default amount is 0.0d.
import java.io.*;
class Simple{
public static void main(String args[]){
double a = -2.00;
double b = 2.00;
System.out.println(a);
System.out.println(b);
}
}
-2.00
2.00
The char data type is a single 16-bit Unicode character. Its amount type lies between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive).The char data is type preloved to store characters.
import java.io.*;
class Simple{
public static void main(String args[]){
char a = 'D';
char b = 'C';
System.out.println(a);
System.out.println(b);
}
}
D
C