codingstreets
Search
Close this search box.
java-data-types

Get Started: Java Data Types

In This Article, You Will Learn about Java Data Types.

Java Data Types – Before moving ahead, let’s know a bit about Java Identifiers.

Table of Contents

Data Type

A data type is a type that defines what type of value a variable stores.

Example: Define different variables for each data types.

				
					public class Main {
  public static void main(String[] args) {
    int number = 10;
    float numberWithDecimal = 10.1;
    char letterName = 'H';
    String name = 'Hello Java'
    boolean = true
  }
}
				
			

Types of Data Type

Data types are divided into two parts –

  1. Primitive data types – Primitive Data Type is a type of Data Type which are pre-defined in Java language such as byte, short, int, long, float, double, boolean and char.

  2. Non-primitive data types – Non-primitive Data Type is a type of Data Type which are defined by programmers such as String, Arrays and Classes.

To learn more size and values of Data Type, refer to Data Types in Java

Integer Types

Byte – Byte can be used in place of int or other integer types to save memory when the value will be within -128 and 127.

				
					public class Main {
  public static void main(String[] args) {
    byte myNumber = 10;
    System.out.println(myNumber);  
  }
}
				
			

Short – The short data type is allowed to store the whole numbers from -32768 to 32767.

				
					public class Main {
  public static void main(String[] args) {
    short myNumber = 100;
    System.out.println(myNumber);  
  }
}
				
			

int – The int data type can store whole numbers from -2147483648 to 2147483647.

				
					public class Main {
  public static void main(String[] args) {
    int myNumer = 5000;
    System.out.println(myNumer);  
  }
}
				
			

Long – The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an “L”

				
					public class Main {
  public static void main(String[] args) {
    long myNumber = 8500000000L;
    System.out.println(myNumber);  
  }
}
				
			

Floating Point Types

You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515.

The float and double data types can store fractional numbers. Note that you should end the value with an “f” for floats and “d” for doubles.

Float Type

				
					public class Main {
  public static void main(String[] args) {
    float myNumber = 8.15f;
    System.out.println(myNumber);  
  }
}
				
			

Double Type

				
					public class Main {
  public static void main(String[] args) {
    float myNumber = 8.15d;
    System.out.println(myNumber);  
  }
}
				
			

Scientific Numbers

A floating point number can also be a scientific number with an “e” to indicate the power of 10.

				
					public class Main {
  public static void main(String[] args) {
    float myFloat = 38e1f;
    double myDouble = 57E1d;
    System.out.println(myFloat);
    System.out.println(myDouble);  
  }
}
				
			

Boolean Type

A boolean data type is declared with the boolean keyword and can only take the values true or false.

				
					public class Main {
  public static void main(String[] args) {
    boolean isareYouProgrammer = true;
    boolean isareYounovice = false;    
    System.out.println(isareYouProgrammer);
    System.out.println(isareYounovice);
  }
}
				
			

Characters

The char data type is used to store a single character. The character must be surrounded by single quotes, like ‘A’ or ‘c’.

				
					public class Main {
  public static void main(String[] args) {
    char myScore = 'A+';
    System.out.println(myScore);
  }
}
				
			

Strings

The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes.

				
					public class Main {
  public static void main(String[] args) {
    String myLan = "Hello Java";
    System.out.println(myLan);
  }
}
				
			

The main difference between primitive and non-primitive data types are:

  • Primitive types are predefined in Java. Non-primitive types are created by the programmer and is not defined by Java.
  • Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
  • A primitive type has always a value, while non-primitive types can be null.
  • A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
  • The size of a primitive type depends on the data type, while non-primitive types have all the same size.

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on