Data Types in Java | Variables in Java

Data types and Variable Declaration in Java

Data Types And Variable Declaration in Java

In this tutorial, we will discuss about basics concepts of Java like data types and variable declaration with some basic examples.

What is data type

Data types are just like containers where you keep things. We have different containers with different size to store things we want.

Same way, Data types in java will specify the type of the data you want to store and size of the data.

There are two types of data types available in java

  1. Primitive Datatypes.
  2. Non Primitive Datatypes.

Primitive Data types

Primitive data types are those data types which includes Integer, Float, Boolean and Char data types in Java.

Integer Data type (int)

Integer data types can hold integer values like 0,12,33 and -1, -22. Size will depend upon the data type is being used. We have different int types with different size as below :

Data types and Variable Declaration in Java



Floating point Data types (float)

Floating point data types can hold floating values with fractional part like 23.22, 233.0023. There are two types of floating point data types based on the size as below :

Data Type

Size

Value Range

float

4 Bytes

3.4e−038 to 3.4e+038

double

8 Bytes

1.7e−308 to 1.7e+038


Boolean data type (boolean)

Boolean data types can hold two different values either true or false. Boolean data type is useful where you need to store status of some operations which can either true means success or false means failure.

Character data types (char)

Character data type is used to hold character values like ‘a’, ‘b’ etc. If we talk about the size of the character data type, it is 2 byte.

The reason character data type is taking 2 byte space instead of 1 byte is unicode character set. Character data type can hold single character only. But Java supports internationalization, so Java is supporting unicode character set which occupies 2 bytes to store the character.

Characters from different language can have different lengths, they may vary in size. So unicode system will manage all types of character for different languages.


What is identifier 

Basically, identifiers are names assigned by users or programmers. Names assigned to variables, methods, classes etc.

What is Keyword in Java

Keywords are reserved words in Java. These reserved words as some specific meaning and purpose in Java. Some of the keywords are like class, int, if, else etc. You can not use keywords are identifiers in your program.

You can not create class with the name int, boolean etc.

Variables in Java
Variables in java are used to store values based on the data type specified. There are different types of variables in java as below :

Instance variable – Each object have their own state and behavior. State is represented by instance variables. Instance variables hold the state of the object and each object maintain their own state.

For example – We have object called Men, Men have state like height, weight, running speed etc. So height, weight etc are the states of Men object.

class Men {
          int height;
          int weight;
}

Class Variables – Class variables are those variables which are common to all the objects. These variables are declared as static variables in java. We will discuss about static variables in details later. but for now, you just need to remember that static variables are common to all objects.

For example – Car object will have common state which is tyres. Each car will have 4 tyres, so that all objects will have common state which is number of tyres in car. No matter how many cars and type of the car you create, tyres will be 4 to all types of car. So we will declare number of tyres field as static.

class Car {
         static int tyres = 4;
}

Local Variables – Local variables are temporary variables created inside the method body. Their scope remains between starting curly braces ‘{‘ and closing curly braces ‘}’.

Variable Declaration

Let’s create some examples on how we can declare variables in java.

But before that, let’s understand what is literals in java. We have different types of literals in java based on the data types specified.

What are Literals – Literals are the variables whose values remain constant through out the program execution. There are different types of literals as below :

String Literals

String literals are created by enclosing the value between double quotes like “Literal values” using java.lang.String class. Enclosing values between double quotes will automatically crate new String object in memory. We will discuss about String class later.

String s = “This is String literal”;

Character Literal

Character literals are created by enclosing single character with single quote.

char c = ‘a’;

Boolean Literal

Boolean literal is created with the value either true or false in Java.

boolean b = true;

Numeric Literals

Numeric literals are created with integer or floating point values.

float a = 23.33;
int b = 100;

Some Examples on Variables

Example 1


public class DataTypeExample {

 public static void main(String[] args) {

 int a = 10;
 float b = 23.0f;
 
 double c = 230.00;
 
 String s = "This is literal";
 
 System.out.println("Int value : " + a);
 System.out.println("Float value : " + b);
 System.out.println("Double value : " + c);
 System.out.println("String value : " + s);
 
 }

}

Important Points

Why we are assigning values to float (23.0f), what is the purpose of using ‘f’ at the end of the literal value. Reason is that by default Java assign double value to float, so if you don’t use ‘f’ there will be error saying ‘Type mismatch: cannot convert from double to float‘. So we need to tell compiler that we need float value not double value.

Same way you can use ‘d’ (23.00d) for double values to tell compiler that this values is double value. And ‘L’ for long values.

Widening and Narrowing conversion in Java

We have different types of data in Java like float, int, double. Widening and Narrowing is the process of converting one primitive data type to another compatible data type. Below are the allowed widening and narrowing in java :

Allowed Widening in Java

  • byte to short, int, long, float, or double

  • short to int, long, float, or double

  • char to int, long, float, or double

  • int to long, float, or double

  • long to float or double

  • float to double

Allowed Narrowing in Java

  • short to byte or char

  • char to byte or short

  • int to byte, short, or char

  • long to byte, short, char, or int

  • float to byte, short, char, int, or long

  • double to byte, short, char, int, long, or float


Leave a Reply

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