Users Online
· Members Online: 0
· Total Members: 187
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
#00003 Data Types in Java
1.1. Data Types in Java
Data type defines the values that a variable can take, for example if a variable has int data type, it can only take integer values. In java we have two categories of data type: 1) Primitive data types 2) Non-primitive data types – Arrays and Strings are non-primitive data types, we will discuss them later in the coming tutorials. Here we will discuss primitive data types and literals in Java.
There are majorly two types of languages. First one is Statically typed language where each variable and expression type is already known at compile time.Once a variable is declared to be of a certain data type, it cannot hold values of other data types.Example: C,C++, Java. Other, Dynamically typed languages: These languages can receive different data types over the time. Ruby, Python
Java is statically typed and also a strongly typed language because in Java, each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.
Java has two categories of data:
- Primitive data (e.g., number, character)
- Object data (programmer created types)
Java is a statically typed language. A language is statically typed, if the data type of a variable is known at compile time. This means that you must specify the type of the variable (Declare the variable) before you can use it.
In the last tutorial about Java Variables, we learned how to declare a variable, lets recall it:
int num;
So in order to use the variable num in our program, we must declare it first as shown above. It is a good programming practice to declare all the variables ( that you are going to use) in the beginning of the program.
Primitive data types
In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and double. Java developers included these data types to maintain the portability of java as the size of these primitive data types do not change from one operating system to another.
byte, short, int and long data types are used for storing whole numbers.
float and double are used for fractional numbers.
char is used for storing characters(letters).
boolean data type is used for variables that holds either true or false.
Primitive data are only single values; they have no special capabilities. There are 8 primitive data types
byte:
This can hold whole number between -128 and 127. Mostly used to save memory and when you are certain that the numbers would be in the limit specified by byte data type.
Default size of this data type: 1 byte.
Default value: 0
Example:
class JavaExample {
public static void main(String[] args) {
byte num;
num = 113;
System.out.println(num);
}
}
Output:
113
Try the same program by assigning value assigning 150 value to variable num, you would get type mismatch error because the value 150 is out of the range of byte data type. The range of byte as I mentioned above is -128 to 127.
short:
This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767.
Default size of this data type: 2 byte
short num = 45678;
int: Used when short is not large enough to hold the number, it has a wider range: -2,147,483,648 to 2,147,483,647
Default size: 4 byte
Default value: 0
Example:
class JavaExample {
public static void main(String[] args) {
short num;
num = 150;
System.out.println(num);
}
}
Output:
150
The byte data type couldn’t hold the value 150 but a short data type can because it has a wider range.
long:
Used when int is not large enough to hold the value, it has wider range than int data type, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
size: 8 bytes
Default value: 0
Example:
class JavaExample {
public static void main(String[] args) {
long num = -12332252626L;
System.out.println(num);
}
}
Output:
-12332252626
double: Sufficient for holding 15 decimal digits
size: 8 bytes
Example:
class JavaExample {
public static void main(String[] args) {
double num = -42937737.9d;
System.out.println(num);
}
}
Output:
-4.29377379E7
float: Sufficient for holding 6 to 7 decimal digits
size: 4 bytes
class JavaExample {
public static void main(String[] args) {
float num = 19.98f;
System.out.println(num);
}
}
Output:
19.98
boolean: holds either true of false.
class JavaExample {
public static void main(String[] args) {
boolean b = false;
System.out.println(b);
}
}
Output:
false
char: holds characters.
size: 2 bytes
class JavaExample {
public static void main(String[] args) {
char ch = 'Z';
System.out.println(ch);
}
}
Output:
Z
Literals in Java
A literal is a fixed value that we assign to a variable in a Program.
int num=10;
Here value 10 is a Integer literal.
char ch = 'A';
Here A is a char literal
Integer Literal
Integer literals are assigned to the variables of data type byte
, short
, int
and long
.
byte b = 100;
short s = 200;
int num = 13313131;
long l = 928389283L;
Float Literals
Used for data type float
and double
.
double num1 = 22.4;
float num2 = 22.4f;
Note: Always suffix float value with the “f” else compiler will consider it as double.
Char and String Literal
Used for char and String type.
char ch = 'Z';
String str = "BeginnersBook";
Java Program to Add two Numbers
Here we will see two programs to add two numbers, In the first program we specify the value of both the numbers in the program itself. The second programs takes both the numbers (entered by user) and prints the sum.
First Example: Sum of two numbers
public class AddTwoNumbers {
public static void main(String[] args) {
int num1 = 5, num2 = 15, sum;
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}
Output:
Sum of these numbers: 20
Second Example: Sum of two numbers using Scanner
The scanner allows us to capture the user input so that we can get the values of both the numbers from user. The program then calculates the sum and displays it.
import java.util.Scanner;
public class AddTwoNumbers2 {
public static void main(String[] args) {
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = sc.nextInt();
System.out.println("Enter Second Number: ");
num2 = sc.nextInt();
sc.close();
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}
Output:
Enter First Number:
121
Enter Second Number:
19
Sum of these numbers: 140
Java Program to Multiply Two Numbers
When you start learning java programming, you get these type of problems in your assignment. Here we will see two Java programs, first program takes two integer numbers (entered by user) and displays the product of these numbers. The second program takes any two numbers (can be integer or floating point) and displays the result.
Example 1: Program to read two integer and print product of them
This program asks user to enter two integer numbers and displays the product. To understand how to use scanner to take user input, checkout this program: Program to read integer from system input.
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
/* This reads the input provided by user
* using keyboard
*/
Scanner scan = new Scanner(System.in);
System.out.print("Enter first number: ");
// This method reads the number provided using keyboard
int num1 = scan.nextInt();
System.out.print("Enter second number: ");
int num2 = scan.nextInt();
// Closing Scanner after the use
scan.close();
// Calculating product of two numbers
int product = num1*num2;
// Displaying the multiplication result
System.out.println("Output: "+product);
}
}
Output:
Enter first number: 15
Enter second number: 6
Output: 90
Example 2: Read two integer or floating point numbers and display the multiplication
In the above program, we can only integers. What if we want to calculate the multiplication of two float numbers? This programs allows you to enter float numbers and calculates the product.
Here we are using data type double for numbers so that you can enter integer as well as floating point numbers.
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
/* This reads the input provided by user
* using keyboard
*/
Scanner scan = new Scanner(System.in);
System.out.print("Enter first number: ");
// This method reads the number provided using keyboard
double num1 = scan.nextDouble();
System.out.print("Enter second number: ");
double num2 = scan.nextDouble();
// Closing Scanner after the use
scan.close();
// Calculating product of two numbers
double product = num1*num2;
// Displaying the multiplication result
System.out.println("Output: "+product);
}
}
Output:
Enter first number: 1.5
Enter second number: 2.5
Output: 3.75
Java Program to read integer value from the Standard Input
In this program we will see how to read an integer number entered by user. Scanner class is in java.util package. It is used for capturing the input of the primitive types like int, double etc. and strings.
Example: Program to read the number entered by user
We have imported the package java.util.Scanner
to use the Scanner. In order to read the input provided by user, we first create the object of Scanner by passing System.in
as parameter.
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
/* This reads the input provided by user
* using keyboard
*/
Scanner scan = new Scanner(System.in);
System.out.print("Enter any number: ");
// This method reads the number provided using keyboard
int num = scan.nextInt();
// Closing Scanner after the use
scan.close();
// Displaying the number
System.out.println("The number entered by user: "+num);
}
}
Output:
Enter any number: 101
The number entered by user: 101
QUIZ on Java DataTypes
Java Data Types
1
Question 1 |
class Main { public static void main(String args[]) { int t; System.out.println(t); } } |
A |
0 |
B |
garbage value |
C |
compiler error |
D |
runtime error |
Question 2 |
Predict the output of following Java program.
class Test { public static void main(String[] args) { for(int i = 0; 0; i++) { System.out.println("Hello"); break; } } } |
A |
Hello |
B |
Empty Output |
C |
Compiler error |
D |
Runtime error |
Question 3 |
Predict the output of the following program.
class Test { public static void main(String[] args) { Double object = new Double("2.4"); int a = object.intValue(); byte b = object.byteValue(); float d = object.floatValue(); double c = object.doubleValue();
System.out.println(a + b + c + d );
} } |
A |
8 |
B |
8.8 |
C |
8.800000095367432 |
Question 4 |
Which of the following statements is/are TRUE regarding JAVA ? (a) Constants that cannot be changed are declared using the ‘static’ keyword. (b) A class can only inherit one class but can implement multiple interfaces.
A |
Only (a) is TRUE. |
B |
Only (b) is TRUE. |
C |
Both (a) and (b) are TRUE. |
D |
Neither (a) nor (b) are TRUE. |