DEV Community

rakeshvngowda
rakeshvngowda

Posted on

1

Data Types in Java

Every variable in Java has a data type.Data types specify the size and type of values that can be stored in an identifier.

In Java Data types are classified into two categories.

  1. Primitive Data type(built-in data type).
  2. Non-Primitive Data type(reference data type).

Primitive Data type.

Java has 8 primitive data types

Integer types

  1. byte - 1byte(8-bits) value range from -128 to 127
  2. short - 2byte(16-bits) value -32768 to 32768
  3. int - 4bytes(32-bits) value from -2147483648 to 2147483647
  4. long - 8bytes(64-bits) value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
public class App 
{
   public static void main(String[] args) {
      // byte type
      byte b = 20;
      System.out.println("byte= "+b);

      // short type
      short s =20;
      System.out.println("short= "+s);

      //int type
      int i =20;
      System.out.println("int= "+i);

      // long type
      long l = 20;
      System.out.println("long= "+l);
   }

}
Enter fullscreen mode Exit fullscreen mode

Floting types

  1. float - 4byte(32-bits) float data type. ex: 0.3f
  2. double - 8byte (64-bits) float data type. ex: 11.123
public class App 
{
   public static void main(String[] args) {
      // float type
      float f = 20.25f;
      System.out.println("float= "+f);

      // double type
      double d = 20.25;
      System.out.println("double= "+d);
   }

}
Enter fullscreen mode Exit fullscreen mode

Character Type

  1. char - 2bytes(16bits) range from 0 to 65,535.
  2. boolean - true or false.
public class App 
{
   public static void main(String[] args) {
      char ch = 'S';
      System.out.println(ch);

      char ch2 = '&';
      System.out.println(ch2);

      char ch3 = '$';
      System.out.println(ch3);
   }

}
Enter fullscreen mode Exit fullscreen mode

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly β€” using the tools and languages you already love!

Learn More

Top comments (0)

AWS Security LIVE! Stream

Stream AWS Security LIVE!

The best security feels invisible. Learn how solutions from AWS and AWS Partners make it a reality on Security LIVE!

Learn More

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay