In this article we will see what are available datatypes in Java programming, what are their default values and how to choose an appropriate datatype.

In general programming terms a variable is a symbolic name that points to some reserved storage location. Every storage location that is pointed by a variable can consists of some value or it can be empty, that means a variable is an identifier for some value so that the operations can be performed on that value.

In most of the languages like c,c++ and Java variables are strongly typed, that means if a variable is of type integer we can not assign a string value to is directly. The type of an variable indicates that the variable can point or hold the values of its type only, the types of variables are simply called data types.

Data types in Java

Java contains both premitive and reference data types, there are 8 premitive data types: byte, short, int, long, float, char, double and boolean. Other than these primitive types everything is an object, even array and string are objects in java.

/* Primitive type variables */
	int a = 10;
	float f = 23f;

	/* Objects or reference type variables */
	int[] arr;
	String str;

	/* User defined reference type variables */
	Apple apple;
In the avobe example you can see, an integer type variable holds value 10, we can not assign a boolean to it unless we have to cast the boolean value explicity to int and we will loose value after decimal. We sill see more about casting in upcoming articles. The other refernece variables arr, str and apple are meant to point related objects of their type. Reference variable Apple can point to any object of type Apple only.

There are 8 data types in Java, as listed below:


1) byte: In java byte data type is an 8-bit signed two's complement integer. The default value of a byte data type is 0 and it can hold any data starting from -128 (-2^7) to 127 (2^7 -1).
	/* byte data type examples */
	byte a = 10;
    byte b = -10;
byte is 4 times smaller than int, it is used in places where we need less space to store values.

2) short: In java short data type is an 16-bit signed two's complement integer. The default value of a byte data type is 0 and it can hold any data starting -32,768 (-2^15) to 32,767 (2^15 -1).
	/* int data type examples */
	byte a = 1034;
    byte b = -1056;
short is 2 times smaller than int, it is used in places where we need less space to store values.

3) int: In java int data type is an 32-bit signed two's complement integer. The default value of a byte data type is 0 and it can hold any data starting - 2,147,483,648.(-2^31) to 2,147,483,647(inclusive).(2^31 -1).
	/* int data type examples */
	int a = 10344;
    int  b = -10546;
int is most commonly used integer data type in java.

4) long: In java long data type is an 64-bit signed two's complement integer. The default value of a long data type is 0L and it can hold any data starting -9,223,372,036,854,775,808.(-2^63) to 9,223,372,036,854,775,807 (2^63 -1).
	/* long data type examples */
	long  a = 103544L;
    long  b = -105546L;
long data type is used where a wider range of integer value need to be stored.

5) float: In java float data type is used to store an 32-bit single-precision floating point number. The default value of a float data type is 0.0f.
	/* float data type examples */
	float a = 103.544f;
    float b = -105.546f;
float data type is used to store floating point numbers that does not require big memory, it?s just half in size than a double.

6) double: In java double data type is used to store an 64-bit double-precision floating point number. The default value of a float data type is 0.0.
	/* double data type examples */
	double a = 103.5449;
    double b = -105.546;
double data type is used to store floating point numbers that require double precision to store values.

7) boolean: In java boolean data type is used to store 1 bit value, boolean can store either true or false. The default value of a boolean data type is false.

	/* boolean data type examples */
	boolean a = true;
    boolean b = false;
boolean data type is used to store true or false flag values.

8) char: In java char data type is used to store 16 bit Unicode characters, char can values starting from '\u0000' (or 0) to '\uffff' (or 65,535) . The default value of a char data type is '\u0000' (or 0) .

	/* char data type examples */
	char d = 'a';
	char e =45;
char data type is used to store store 16 bit Unicode characters.

Variables in Java

In general Java variables can be categorised in 4 parts, instance variables, local variables, class variables and parameters.

1) Instance variables or non-static variables

In Java instance variables are the variables those are being declared at class level but does not have a static keyword with them. These variables have their own copy for each instance of the class they are declared in.

2) Class level variables or static variables

In Java class level variables are the variables those are being declared at class lavel and do have a static keyword associate with them. These variables does not contain a saparate copy for each instance of the class, rather any modification intheir state is reflected to each instance.

/* instance variables */
	int a = 10;
	float f = 23f;
	
	/* static or class level variables */
	static int sa = 10;
	static float sf = 23f;
	
	public void getValue(){
		/* static or class level variables */
		int value=0;
	}
	
	/* parameters */
	public void setValue(int set){
		
}

3) Local variables

In Java local variables are the variables those are being declared inside the body of a method. These variables are meant to be used in method body only. Local variables needs to be initialized before use.

4) Parameters

Parameters are local variables that are passed as arguments in a method call, these variables can be accessed inside the method?s body only.

Variables Naming rules and practices

Every programming language has its own rules for declaring variables. Java is a case sensitive language, that means name and Nameare two different identifiers.

1) A Java variable can be a combination of any unicode character or number, but the name should start either by a letter, underscore or dollor sign only. By convention variable name should start with a small letter and not with a dollor or number.

2) A variable name should not be as same as any of the reserved keywords in Java, there are 50 reserved words in Java we will see shortly.

3) By convention, variable names should be written in camel cashing. If a varianle name is indicating first row of a class, by convention it should be spelled like ?firstRow? and not ?FIRSTROW? not ?firstrow?.
  • By Techburps.com
  • Feb 21, 2015
  • Core Java