More about Java Data Types

Before discussing data types in Java, you should be aware of the word datatype. So what is a data type? It is the type of data that a variable can store in the form of value. It can be of any kind, i.e. numerical data, textual data, character data etc. I hope you got it. If you still have any queries related to datatypes, comment below. Now let’s discuss datatypes in Java.

Data Types

Data Types in Java

In java data types are divided into two groups:

1-Primitive data types

2-Non-primitive data types 

Primitive data typesNon-primitive data types 
bytestring
float arrays
intclasses
double
char
long
boolean
short
JAVA DATA TYPES

Declaring variable with datatypes in java with output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
int myNum = 5; // integer (whole number)
float myFloatNum = 5.99f; // floating point number
char myLetter = 'D'; // character
boolean myBool = true; // boolean
String myText = "Codelikechamp"; // String
System.out.println(myNum);
System.out.println(myFloatNum);
System.out.println(myLetter);
System.out.println(myBool);
System.out.println(myText);
}
}
public class Main { public static void main(String[] args) { int myNum = 5; // integer (whole number) float myFloatNum = 5.99f; // floating point number char myLetter = 'D'; // character boolean myBool = true; // boolean String myText = "Codelikechamp"; // String System.out.println(myNum); System.out.println(myFloatNum); System.out.println(myLetter); System.out.println(myBool); System.out.println(myText); } }
public class Main {
  public static void main(String[] args) {
    int myNum = 5;               // integer (whole number)
    float myFloatNum = 5.99f;    // floating point number
    char myLetter = 'D';         // character
    boolean myBool = true;       // boolean
    String myText = "Codelikechamp";     // String    
    System.out.println(myNum);
    System.out.println(myFloatNum);
    System.out.println(myLetter);
    System.out.println(myBool);
    System.out.println(myText);
  }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
5
5.99
D
true
Codelikechamp
5 5.99 D true Codelikechamp
5
5.99
D
true
Codelikechamp
Data Types

Primitive Data Types in JAVA

A primitive data type is one that specifies the size and type of variable values, which means what kind of data can variable can store, and it has no additional methods.
There are a total of eight primitive data types in Java:

Data TypeSizeDescription
byte1 bytesStores whole numbers from -128 to 127
short2 bytesStores whole numbers from -32,768 to 32,767
int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647
long8 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytesStores fractional numbers.
Sufficient for storing 6 to 7 decimal digits
double8 bytesStores fractional numbers.
Sufficient for storing 15 decimal digits
boolean1 bitStores true or false values
char2 bytesStores a single character/letter or ASCII values
Primitive Data Types in JAVA

1-Numbers(Primitive data types) Data types:

Basically java contain two types of numeric data types:

a-Integer types

These type of datatypes can stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte, short, int and long. Which type you should use, depends on the numeric value.

Byte
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
byte myNum = 10;
System.out.println(myNum);
}
}
public class Main { public static void main(String[] args) { byte myNum = 10; System.out.println(myNum); } }
public class Main {
  public static void main(String[] args) {
    byte myNum = 10;
    System.out.println(myNum);  
  }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
10
10
10
short
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
short myNum = 2000;
System.out.println(myNum);
}
}
public class Main { public static void main(String[] args) { short myNum = 2000; System.out.println(myNum); } }
public class Main {
  public static void main(String[] args) {
    short myNum = 2000;
    System.out.println(myNum);  
  }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
2000
2000
2000
int
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
int myNum = 200000;
System.out.println(myNum);
}
}
public class Main { public static void main(String[] args) { int myNum = 200000; System.out.println(myNum); } }
public class Main {
  public static void main(String[] args) {
    int myNum = 200000;
    System.out.println(myNum);  
  }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
200000
200000
200000
long
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
long myNum = 25000000000L;
System.out.println(myNum);
}
}
public class Main { public static void main(String[] args) { long myNum = 25000000000L; System.out.println(myNum); } }
public class Main {
  public static void main(String[] args) {
    long myNum = 25000000000L;
    System.out.println(myNum);  
  }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
25000000000
25000000000
25000000000

b-floating point number types

These type of datatypes represents numbers with a fractional part, containing one or more decimals. There are two types: float and double.

float
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
float myNum = 3.79f;
System.out.println(myNum);
}
}
public class Main { public static void main(String[] args) { float myNum = 3.79f; System.out.println(myNum); } }
public class Main {
  public static void main(String[] args) {
    float myNum = 3.79f;
    System.out.println(myNum);  
  }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
3.79
3.79
3.79
double
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
double myNum = 20.99d;
System.out.println(myNum);
}
}
public class Main { public static void main(String[] args) { double myNum = 20.99d; System.out.println(myNum); } }
public class Main {
  public static void main(String[] args) {
    double myNum = 20.99d;
    System.out.println(myNum);  
  }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
20.99
20.99
20.99

2-Boolean DataTypes

It can store values like yes/no , on/off , 1/0 etc see code below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
boolean isJavaFun = true;
boolean isjavascriptFun = false;
System.out.println(isJavaFun);
System.out.println(isjavascriptFun);
}
}
public class Main { public static void main(String[] args) { boolean isJavaFun = true; boolean isjavascriptFun = false; System.out.println(isJavaFun); System.out.println(isjavascriptFun); } }
public class Main {
  public static void main(String[] args) {
    boolean isJavaFun = true;
    boolean isjavascriptFun = false;    
    System.out.println(isJavaFun);
    System.out.println(isjavascriptFun);
  }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
true
false
true false
true
false

3-Character datatype

Character type datatype can store only one character either it can be a number or alphabets:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
char myGrade = 'A';
System.out.println(myGrade);
}
}
public class Main { public static void main(String[] args) { char myGrade = 'A'; System.out.println(myGrade); } }
public class Main {
  public static void main(String[] args) {
    char myGrade = 'A';
    System.out.println(myGrade);
  }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
A
A
A

4-String datatype (Non-Primitive data types)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
String a = "codeliekchamp.com";
System.out.println(a);
}
}
public class Main { public static void main(String[] args) { String a = "codeliekchamp.com"; System.out.println(a); } }
public class Main {
  public static void main(String[] args) {
    String a = "codeliekchamp.com";
    System.out.println(a);
  }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
codeliekchamp.com
codeliekchamp.com
codeliekchamp.com

Link: https://Codelikechamp.com

Medium Link: Follow me on Medium

Linkedin Link: Follow me on Linkedin

🤞 Don’t miss any latest posts!

Please subscribe by joining our community for free and stay updated!!!

IF YOU HAVE ALREADY SUBSCRIBED JUST CLOSE THIS FORM !

Leave a Comment

Your email address will not be published. Required fields are marked *