You are on page 1of 16

Module 2

Basic Data Types

At the end of this module you are expected to:

1. Understand different Variables in Java;


2. Learn what are the Data types in Java; and,
3. Apply the different Data types in java.

Variables

Variables are only designated memory spaces for the storage of values. This
implies that you set aside some memory when you create a variable. Data is stored in
variables for processing.

A name (or identifier), such as area, age, height, or another term, is given to a
variable. Each variable is assigned a name that uniquely identifies it, allowing for the
assignment of a value and retrieval of that value.

The operating system allots memory and determines what can be kept in the
reserved memory based on the data type of a variable. Therefore, you may store
integers, decimals, or characters in these variables by giving them alternative data
types.

Variables have types. Some examples:

• int: for entire integers (numbers between 123 and 456)

• double: for floating-point or real values, such as 3.1416 and -55.66, with
optional decimal points and fractional components in fixed or scientific notations.

• String: used for greetings like "Hello" and "Good Morning!" Strings of text are
contained in double quotations.

33
You can declare a variable of a type and assign it a value. Example: String name =
"David";
This creates a variable called name of type String, and assigns it the value "David".

It is crucial to remember that a variable has a type associated with it and can
only hold values of that type. An int variable, for instance, may hold integer values like
123 but not real numbers like 12.34 or text like "Hello".

Data Types in Java

According to the definition of data types in Java, these are specifiers that assign
various sizes and types of values that can be stored in a variable or an identifier. Java
offers a wide variety of data types. Java data types are split into two categories.:

1. Primitive Data Types - which include integer, character, boolean, and float
2. Non-primitive Data Types - which include classes, arrays and interfaces.

Figure 31. Data Types

34
Primitive Data Types

Java supports eight different basic data types. The language has predefined
primitive data types that are given keyword names. Let's take a closer look at each of
the eight primitive data types.

byte

• Byte data type is an 8-bit signed two's complement integer


• Minimum value is -128 (-2^7)
• Maximum value is 127 (inclusive)(2^7 -1)
• Default value is 0
• Byte data type is used to save space in large arrays, mainly in place of integers,
since a byte is four times smaller than an integer.
• Example: byte a = 100, byte b = -50

Figure 32. Sample Java Code

short

• Short data type is a 16-bit signed two's complement integer


• Minimum value is -32,768 (-2^15)
• Maximum value is 32,767 (inclusive) (2^15 -1)
• Short data type can also be used to save memory as byte data type. A short is 2
times smaller than an integer
• Default value is 0.

35
• Example: short s = 10000, short r = -20000

Figure 33. Sample Java Code

int

• Int data type is a 32-bit signed two's complement integer.


• Minimum value is - 2,147,483,648 (-2^31)
• Maximum value is 2,147,483,647(inclusive) (2^31 -1)
• Integer is generally used as the default data type for integral values unless there
is a concern about memory.
• The default value is 0
• Example: int a = 100000, int b = -200000

Figure 34. Sample Java Code

36
long

• Long data type is a 64-bit signed two's complement integer


• Minimum value is -9,223,372,036,854,775,808(-2^63)
• Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
• This type is used when a wider range than int is needed
• Default value is 0L
• Example: long a = 100000L, long b = -200000L

Figure 35. Sample Java Code

Take note of the letter L added at the end of -42332200000. This indicates that it is
an integral long literal. Integral literals will be covered further on in this text.

float

• Float data type is a single-precision 32-bit IEEE 754 floating point


• Float is mainly used to save memory in large arrays of floating point numbers
• Default value is 0.0f
• Float data type is never used for precise values such as currency
• Example: float f1 = 234.5f

37
Figure 36. Sample Java Code

In the program above, you'll see that we used -42.3f rather than -42.3. -42.3 is a double
literal, which explains why. Use f or F to instruct the compiler to consider -42.3 as a float
rather than a double.

double

• A double-precision 64-bit IEEE 754 floating point data type is called double.
• For decimal numbers, this data type is typically chosen as the default, or the first
option.
• Never use the double data type for exact numbers like current
• 0.0d is the default value.
• For instance, double d1 = 123.4

Figure 37. Sample Java Code

38
boolean

• One piece of information is represented by a boolean data type.


• Only true and false are available as values.
• Simple flags that capture true/false situations are used with this data type.
• False is the default value.
• For instance, boolean one equals true.

Figure 38. Sample Java Code

char

• char data type is a single 16-bit Unicode character


• Minimum value is '\u0000' (or 0)
• Maximum value is '\uffff' (or 65,535 inclusive)
• Char data type is used to store any character
• Example: char letterA = 'A'

39
Figure 39. Sample Java Code

Reference Data types

• Reference variables are made using the classes' designated constructors. They
are utilized to get at things. These variables are specified as being of an
unchangeable kind. Examples include "employee," "puppy," etc.
• Reference datatypes include class objects and several types of array variables.
• Any reference variable's default value is null.
• Any object of the defined type or any type that is compatible can be referenced
by a reference variable.
• Animal animal = new Animal("giraffe"), for instance.

Java Literals

A literal is a fixed value represented in source code. Without any computation,


they are expressed directly in the code.. (Burd, 2014)

Literals can be assigned to any primitive type variable. For example –

byte a = 68;
char a = 'A';

The number systems of decimal (base 10), hexadecimal (base 16), and octal
(base 8) can also be used to define the terms byte, int, long, and short..

40
When employing these number systems for literals, prefix 0 is used to denote
octal, while prefix 0x is used to denote hexadecimal. For example –

int decimal = 100;


int octal = 0144;
int hexa = 0x64;

Variables

Figure 40. Sample java Code

Non-Primitive Data Types

Because they make references to objects, non-primitive data types are known as
reference types. (Burd, 2014)

The main difference between primitive and non-primitive data types are:

• Java already has primitive types specified (predefined). Except for String, non-
primitive types are constructed by the programmer. Java does not define them.
• Unlike basic types, non-primitive types may be used to invoke methods to carry
out specific actions.
• Unlike non-primitive types, which can be null, primitive types always have a
value.
• Non-primitive types begin with an uppercase letter; primitive types begin with a
lowercase letter.

41
• The size of a primitive type depends on the data type, while non-primitive types
have all the same size.

Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc. You
will learn more about these in a later chapter.

42
Learning Activity 1

Run the code below to view the output. Then, modify the settings and rerun the
program. To see what occurs, try deleting spaces from the print out statements and
adding quotes to the variables.

Figure 41. Java Code

Guide Question 1

To verify your understanding of what has been discussed thus far, please
respond to the following. At the conclusion of this unit, use the accompanying answer
key to verify your answers.

1. What is a variable?
2. Primitive Data Types?

Guide Question 2

Multiple choices: Select the best answer for each question.

1. Which variable type would you use for a city name?


a. double

43
b. String
c. Int
2. What type should you use to represent the average grade for a course?
a. int
b. double
c. boolean
d. String
3. What type should you use to represent the number of people in a household?
a. int
b. double
c. boolean
d. String
4. What type should you use to hold the first name of a person?
a. int
b. double
c. boolean
d. String
5. What type should you use to record if it is raining or not?
a. int
b. double
c. boolean
d. String

Guide Question 3

This assignment statement below is in the wrong order. Try to fix it to compile
and run.

public class Test3


{
public static void main(String[] args)
{

44
int score;
4 = score;
System.out.println(score);
}
}

Answers to the Guide Questions 1

Question 1: Variables store data for processing.

Question 2: Primitive Data Types - which include integer, character, boolean, and
float

Answers to the Guide Questions 2

Question 1: b. String

Question 2: b. double

Question 3: a. int

Question 4: d. String

Question 5: c. boolean

Answers to the Guide Questions 3

public class Test3


{
public static void main(String[] args)
{
int score;
score = 4;
System.out.println(score);
}
}

45
Key Points

1. Data is stored in variables for processing..


2. Java defines data types as specifiers that allow for a variety of sizes and types of
values to be stored in variables or identifiers..
3. Primitive Data Types - which include integer, character, boolean, and float

46
References and Supplementary Materials

Books
1. Barry Burd (2014). Java for Dummies. A Wiley Brand, New Jersey: John Wiley &
Sons, Inc.
Online Supplementary reading Material

1. Java - Basic Data types;


https://www.tutorialspoint.com/java/java_basic_datatypes.htm; Retrieved August
30, 2020
2. Java Variables and Data Types; https://www.guru99.com/java-variables.html;
Retrieved August 30, 2020
3. Java data Types; https://www.w3schools.com/java/java_data_types.asp;
Retrieved August 30, 2020

47
Assessment 1

Name: Course, Year and Section:

Direction: Fill in the Blanks: answer the following question or complete the statements
by writing the appropriate words in the answer blanks.

1. Variable is also known as __________.


2. __________________ are produced using the classes' designated constructors.
They are utilized to get at things. These variables are specified as being of an
unchangeable kind. Examples include "employee," "puppy," etc.
3. ________ : for texts such as "Hello" or "Good Morning!". Text strings are
enclosed within double quotes.
4. __________ unless there is a memory issue, is often utilized as the default data
type for integral values..
5. _________ A data type is a single piece of information. For straightforward flags
that monitor true/false circumstances, this data type is employed.
6. Non-primitive data types are called _________________ because they refer to
objects.
7. ________ : for integers (whole numbers) such as 123 and -456
8. __________ : uses fixed or scientific notations for floating-point or real numbers
with optional decimal points and fractional portions, such as 3.1416 and -55.66..
9. Add the correct data type for the following variables:

______ myNum = 9;
______ myFloatNum = 8.99f;
______ myLetter = 'A';
______ myBool = false;
______ myText = "Hello World";

10. byte, short, int, long, float, double, boolean and char are called: _______ data
types.

48

You might also like