0% found this document useful (0 votes)
2 views

Java 211

The document provides an overview of primitive data types in Java, which include boolean, byte, int, long, float, double, char, and short. It explains the characteristics and uses of each type, as well as the concept of arrays and their advantages and disadvantages. Additionally, it introduces stacks and queues, highlighting their operational principles of LIFO and FIFO, respectively.

Uploaded by

Aliyu Faruk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java 211

The document provides an overview of primitive data types in Java, which include boolean, byte, int, long, float, double, char, and short. It explains the characteristics and uses of each type, as well as the concept of arrays and their advantages and disadvantages. Additionally, it introduces stacks and queues, highlighting their operational principles of LIFO and FIFO, respectively.

Uploaded by

Aliyu Faruk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Primitive Data Types in Java

Primitive data types in Java are predefined by the Java


language and named as the reserved keywords. A
primitive data type does not share a state with other
primitive values. Java programming language supports the
following eight primitive data types.

1. Boolean data type

2. byte data type

3. int data type

4. long data type

5. float data type

6. double data type

7. char data type

8. short data type

in this section, we will discuss all the Primitive data types


in detail.

Primitive Number Types

Generally, the primitive number types are classified into


two categories:

Whole numbers: The whole numbers hold the complete


number, positive and negative, for example, 170, 225, -
170, -225, etc. For these numbers, the valid data types
are byte, short, int, and long. It depends on the number
that which data type would be preferred.
Floating Numbers: The floating numbers are the
numbers with a fraction part. These numbers have one or
more decimal values, for example, 10.25, 15.25, etc. For
these numbers, the valid data types
are float and double.

From the all above data types, the int, double, and float
are the most widely used data types.

Before understanding the Primitive data types, let's


discuss data types in Java:

Data Types in Java

As its name specifies, data types are used to specify the


type of data to store inside the variables. Java is a
statically-typed language, which means all the variables
should be declared before use. So, we have to specify the
variable's type and name. A variable is declared as
follows:

. int a = 1;
The above statement acknowledges your program that a
file 'a' exists and holds integer type data with value 1. A
variables data type specifies the type of value it contains.

Data types in Java categories into two categories:


o Primitive
o Non-primitive

A non-primitive data type can be a class, interface, and


Array.

Let's back to our main topic, primitive data type; discuss


each primitive data type in detail:

1) Boolean Data Type

A boolean data type can have two types of values, which


are true and false. It is used to add a simple flag that
displays true/false conditions. It represents one bit of
information. It's is not a specific size data type. So, we can
not precisely define its size.

Example:

. boolean isJtpBest=true;
. boolean isCold = false;
. System.out.println(isJtpBest);
. System.out.println(isCold);

2) Byte Data Type

It is an 8-bit signed 2's complement integer. It can have a


value of (-128) to 127 ( inclusive). Below are the benefits
of using the byte data type:

o It is useful for saving memory in large Arrays.


o It can be used instead of int to clarify our code using
its limits.
o It saves memory, too, because it is 4 times smaller
than an integer.

Example:

. byte a = 100;
. System.out.println(a);

3) Int Data Type

The int stands for Integer; it is a 32-bit signed two's


complement integer. It's value can be from -2^31 to
(2^31-1), which is -32,768 to 32,767 (inclusive). Its default
value is zero. It represents an unsigned 32-bit integer,
which has a value range from 0 to 32,767.

If memory saving is not our primary goal, then the int data
type is used to define the integer value.

Example:

. int num= 50000;


. System.out.println(num);

4) Long Data Type

It is a 64-bit 2's complement integer with a value range of


(-2^63) to (2^63 -1) inclusive. It is used for the higher
values that can not be handled by the int data type.

Example:
. long l = 7000000000L;
. System.out.println(l);

5) Float Data Type

The Float data type is used to declare the floating values


( fractional numbers). It is a single-precision 32-bit IEEE
754 floating-point data type.

Its value range is infinite. While declaring the floating, we


must end the value with an f.

It is useful for saving memory in large arrays of floating-


point numbers. It is recommended to use float data type
instead of double while saving the floating numbers in
large arrays, and not use it with precise numbers such as
currency.

Example:

. float num = 5.75f;


. System.out.println(num);

Note: A Scientific number can also be used to


represent a scientific number with the power of 'e',
where e represents the power of 10. for example,
float f1= 25e2f; double d1= 15E2d; etc.

6) Double Data Type

The double data type is also used for the floating-point


( Fractional values) number. It is much similar to the float
data type. But, generally, it is used for decimal values.
Like the float data type, its value range is infinite and also
can not be used for precise values such as currency.

The default value of the double data type is 0.0d. While


declaring the double type values, we must end the value
with a d.

Example:

. double num= 19.99d;


. System.out.println(num);

The char data type is also an essential primitive data type


in Java. It is used to declare the character values. It is a
single 16-bit Unicode Character with a value range of 0 to
65,535 (inclusive).

While declaring a character variable, its value must be


surrounded by a single quote ('').

Example:

. char myChar= 'H';


. System.out.println(myChar);

8) Short Data Type


The short data type is also used to store the integer
values. It is a 16-bit signed 2's complement integer with a
value range of -32,768 to 32,767 (inclusive). It is also used
to save memory, just like the byte data type.

It is recommended to use the short data type in a large


array when memory saving is essential.

. short num = 5000;


. System.out.println(num);
Here, we have discussed all the primitive data types in
Java. Other data types such as Strings, Classes, Interfaces,
and Arrays are non-primitive data types in Java.

However, Java provides support for character strings using


the String class of Java.lang package. String class has
some special support from the Java Programming
language, so, technically it is a primitive data type. While
using String class, a character string will automatically
create a new String Object. For example, String s= "
JavaTpoint is the best portal to learn Java";
ARRAY IN JAVA

What is an Array in Java?

An array refers to a data structure that contains homogeneous


elements. This means that all the elements in the array are of
the same data type. Let's take an example:

This is an array of seven elements. All the elements are integers


and homogeneous. The green box below the array is called the
index, which always starts from zero and goes up to n-1 elements.
In this case, as there are seven elements, the index is from zero to
six. There are three main features of an array:

1. Dynamic allocation: In arrays, the memory is created


dynamically, which reduces the amount of storage required for
the code.
2. Elements stored under a single name: All the elements are

stored under one name. This name is used any time we use an
array.
3. Occupies contiguous location: The elements in the arrays are

stored at adjacent positions. This makes it easy for the user to


find the locations of its elements.

Advantages of Arrays in Java

 Java arrays enable you to access any element randomly with


the help of indexes
 It is easy to store and manipulate large data sets

Disadvantages of Arrays in Java

 The size of the array cannot be increased or decreased once it


is declared—arrays have a fixed size
 Java cannot store heterogeneous data. It can only store a
single type of primitives

Now that we understand what Java arrays are- let us look at how
arrays in Java are declared and defined.
Stacks and Queues

Stack is a container of objects that are inserted and


removed according to the last-in first-out (LIFO) principle.

Queue is a container of objects (a linear collection) that


are inserted and removed according to the first-in first-out
(FIFO) principle.

Stack:

In the pushdown stacks only two operations are allowed:


push the item into the stack, and pop the item out of the
stack. A stack is a limited access data structure - elements
can be added and removed from the stack only at the top.
push adds an item to the top of the stack, pop removes
the item from the top. A helpful analogy is to think of a
stack of books; you can remove only the top book, also
you can add a new book on the top.

Queue:

An excellent example of a queue is a line of students in


the food court of the UC. New additions to a line made to
the back of the queue, while removal (or serving) happens
in the front. In the queue only two operations are allowed
enqueue and dequeue. Enqueue means to insert an item
into the back of the queue, dequeue means removing the
front item. The picture demonstrates the FIFO access. The
difference between stacks and queues is in removing. In a
stack we remove the item the most recently added; in a
queue, we remove the item the least recently added.

You might also like