Data types in java

Data types in java  refer to type of data that can be stored in variable. As Java is strongly typed language, you need to define datatype of variable to use it and you can not assign incompatible datatype otherwise the compiler will give you an error.

int d=”Hello”

The compiler will give you an error with this message – "Type mismatch: cannot convert from String to int".
There are two types of data types in java.

  • Primitive data types
  • Referenced data types.

Primitive data types

Primitive data types are those datatypes which are defined by java language itself.

There are 8 primitive data types in java.

Data Type 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
double 0.0d 8 byte

Let’s see some example about data types:

Adding two integers

When you run above program, you will get below output:

30

Assigning int to double(Widening)

Here we will assign int to double. As double takes more memory than int. This is widening operation.

When you run above program, you will get below output:

30
30.0

Assigning double to int(Narrowing or typecasting)

Here we will assign double to int. As double takes more memory than int. This is Narrowing operation.

When you run above program, you will get below output:

30.0
30

Assinging int to byte(Overflow condition)

When you assing int to byte and value of int is larger than size of byte then it is a case of overflow.

When you run above program, you will get below output:

200
-56

Reference data types:

Reference data types are those data types which are provided as class by Java API or by class that you create.
String is an example of Reference data types provided by java.

That’s all about data types in java.

Related Posts

  • 25 September

    Reference Variable in Java

    In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the type is an object, then the variable is called reference […]

  • 26 March

    Global variables in java

    In this post, we will see how to define global variables in java. Unlike C/C++, there are no standard global variables in java but you can still define global variables that can be used across all classes. Global variables are those variables that can be accessed across all the classes. Java does not support global […]

  • 26 March

    Static block in java

    In this post, we will see about how to implement static block in java. Here are some important points about static block in java. Static block is used for static initializations of a class Static block is executed only once when either you create an object of the class or access static member of the […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

  • 17 June

    How to learn Java Programming

    Java is one of the most used programming languages in the world for developing the desktop application and web application.It has completed 22 years in the programming world. In this post, we will see how to learn java programming in depth and in most efficient way. Java has a huge learning curve, it is a […]

  • 28 May

    Introduction to Java Programming

    Java is one of the most used programming languages in the world for developing desktop application and web application. It has completed 22 years in the programming world. History of Java: Java programming language was developed by James Ghosling, Patrick Naughton, Mike Sheridan at Sun Microsystems Inc. in 1991. The initial name was Oak but […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.