Ad Code

Data Types || The Primitive Types||Integers || Java || Pt - 3.1

 


A Strongly Typed Language Is Java

Java is a tightly typed language, which needs to be stated right away. In reality, this fact contributes to Java's safety and robustness. Let's examine this's meaning. To start, each variable has a type, as does every expression, and each type is rigidly specified. Second, type compatibility is examined for all assignments, whether explicit or made possible by parameter passing in method calls. As in certain languages, there are no automated conversions or coercions of incompatible types. The Java compiler verifies the types of all expressions and arguments to guarantee compatibility. Before the compiler can complete building the class, it must fix any type incompatibilities.

The Primitive Types

The primitive data types that Java specifies are the byte, short, int, long, char, float, double, and boolean. Both words will be used in this book to refer to the simple kinds, which are another name for the basic types. Four groupings may be made out of these:

  • Integers This group consists of the whole-valued signed number types byte, short, int, and long.
  • Numbers with floating points This category comprises the fractionally precise numerical representations float and double.
  • Characters This category includes the symbol char, which stands for a character set's symbols such letters and digits.
  • Boolean Boolean, a specific type for expressing true or false values, is a member of this group.

You can create arrays or your own class types, or utilise these kinds exactly as they are. They serve as the foundation for all other forms of data you can produce. Instead of complicated objects, basic types represent single values. Java is entirely object-oriented aside from the primitive types, though. They are comparable to the basic kinds present in the majority of non-object oriented languages. The efficiency behind this is the reason. Performance would have suffered too much if the primitive types had been converted to objects.

The explicit range and mathematical behaviour of the primitive kinds are specified. The size of an integer might change depending on the requirements of the execution environment in languages like C and C++. Java, however, is unique. All data types have a well specified range since Java requires portability. For instance, regardless of the platform, an int is always 32 bits. As a result, programmes may be created that are certain to operate on any computer architecture. Although rigidly defining an integer's size could result in a little performance hit in some circumstances, it is required to achieve portability.

Let's examine each type of data separately.

Integers

The four integer types that Java defines are byte, short, int, and long. These are all signed values that may be both positive and negative. Unsigned numbers that are solely positive are not supported in Java. Both signed and unsigned integers are supported by several additional programming languages. Unsigned integers, however, weren't considered required by the developers of Java. They believed that the term "unsigned" was primarily used to describe how the high-order bit behaves, which determines the sign of an integer number. Java uses a unique "unsigned right shift" operator to control the meaning of the high-order bit in a different way. As a result, there was no longer a requirement for an unsigned integer type.

The behaviour that an integer type provides for variables and expressions of that type should be considered when determining an integer type's width rather than the quantity of storage it requires. If the types perform as you declared them to, the Java run-time environment is free to use any size it likes. This table illustrates the considerable variations in various integer types' widths and ranges:

Name                 Width                 Range

long                     64                –9,223,372,036,854,775,808    to    9,223,372,036,854,775,807

int                        32                –2,147,483,648    to    2,147,483,647

short                   16                 –32,768    to    32,767

byte                     8                   –128    to    127

Let’s look at each type of integer.

byte

Byte is the lowest integer type. The range of this signed 8-bit type is from -128 to 127. When working with a stream of data from a network or file, variables of type byte are extremely helpful. They are helpful for dealing with unprocessed binary data that might not be immediately compatible with Java's other built-in kinds.

The byte keyword is used to declare byte variables. For instance, the declarations below define two bytes variables named b and c:

byte b, c;

short

A signed 16-bit type, short. Its range is between -32,768 and 32,767. Probably the least popular Java type, it. These are a few illustrations of brief variable declarations:

short s;

short t;

int

The integer type int is the most used one. The type's signed 32-bit ranges from -2,147,483,648 to 2,147,483,647. Integer variables are frequently used to index arrays and control loops in addition to their other applications. Despite the fact that you may assume that utilising a byte or short would be more effective than using an int in circumstances when the broader range of an int is not required, this might not be the case. The explanation for this is because when byte and short values are employed in an expression, they are promoted to int during evaluation. In this chapter's later sections, type promotion is discussed. As a result, int is frequently the best option when an integer is required.

long

Long is a signed 64-bit type that comes in handy when an int type is unable to accommodate the required value. A long has a considerable range. It is therefore helpful when large, entire numbers are required. Here is an example of a software that determines how many miles light will cover in a certain number of days:

class Light {

public static void main(String args[]) {

int lightspeed;

long days;

long seconds;

long distance;

lightspeed = 186000;

days = 1000; 

sec (seconds) = days * 24 * 60 * 60; 

distance = lightspeed * seconds; 

System.out.print("In " + days);

System.out.print(" days light will travel about ");

System.out.println(distance + " miles.");

}

}

    

These are the results this software produces:

In 1000 days, The can light will travel about 16070400000000 miles.

It is obvious that the outcome could not have been stored in an int variable.


Ad Code

Responsive Advertisement