Lesson 2 – Data Types and Variables in Java Copy

A software application consists of two parts: Data and Logic. In this lesson, you will see what are the different data types that Java programming can handle.

Broadly speaking, there are two main data types in Java: primitive data types and custom data types. Primitive data types are the default data types that Java supports and custom data types are user defined data types.

Primitive Data Types can be divided into four categories:

  • Numeric Data
  • Boolean Data
  • Characters
  • Strings

Numeric Data

Following table summarizes the numeric data types in Java

Data Type Default Value 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

Before we see these data types in action, we need to understand the concept of a variables in Java. Variables are basically placeholders that store data type. For instance, you can declare a variable “a” and store a value 10 in it. Depending upon the accessibility, variables have three types: public, protected and private. Public variables are accessible everywhere. Protected variables can be seen in sub-classes and other classes in the package. Private variables are only accessible within the class. You will see the concept of classes in lesson 8. It is important to mention that Java is a strongly typed language which means that you have to specify the type of the data that the variable will store. For instance, if you want to store 10 in a variable, you have to specify that the variable is integer type. The following example will make this clear:

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

int a = 20;
int b = 30;
int result = a + b;
System.out.println(result);

}

}

In the above code, we declare two integer variables “a” and “b”. The value of “a” is 10 and the value of “b” is 20. Next, we added the two variable and store the result in another variable “result”, which is then printed on the console. In the output you will see 50 since the sum of 20 and 30 is 50.

Boolean Data

 

Boolean data type is the type of data that can be either True or False. For example, if you want to store the information regarding his martial status, you can declare a Boolean variable “married” and set its value to True or False. Let’s see a simple example of Boolean data in action.

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

boolean married = false;

System.out.println("Is he married? Anser = " + married);
}

}

In the script above, a Boolean variable “married” is declared which stores a value “false”. Next, the value of this variable is printed on the console.

Chars and Strings

Characters are strings are used to store textual data. If you want to store single character, you can use char variable. Else if you want to store multiple characters, you can use string variable. Let’s look at simple examples of both these data types:

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

char gender= 'f';
String message = "Your gender is ";
System.out.println(message + gender);
}

}

In the above code we declared a char variable “gender” and a string variable “message”. Then we use the “println” method to print the concatenation of “gender” and “message” variable. The plus sign when applied to char or string type variables, performs the concatenation operation. In the output, you will see the string “Your gender is f”.

 

What’s Next?

In this lesson, you saw Java data types. In the next lesson,  you will see operators in Java.