Data types in java bytes

Difference Between byte, short, int and long Datatype in Java

There are two types of data types namely primitive datatype/fundamental and non-primitive/derived datatype. The primitive data type is defined as local sets or default set of datatype already present while deriving a datatype or creating a set of datatype using them are known as derived data types such as an array, stack, queue, tries, etc. Let’s clear understanding of primitive data types before diving into differentiating the same.

Fundamental Data Types Derived Data Types
A fundamental data type is also called a primitive data type. These are the basic data types. The derived data type is the aggregation of the fundamental data type.
character, integer, float, and void are fundamental data types. Pointers, arrays, structures, and unions are derived data types.
Character is used for characters. It can be classified as
char, signed char, Unsigned char.
Pointers are used for storing the address of variables.
Integer is used for integers( not having decimal digits). It can be classified as signed and unsigned. Further, classified as int, short int, and long int. An array is used to contain a similar type of data.
float is used for decimal numbers. These are classified as float, double and long double. structure is used to group items of possibly different types into a single type.
void is used where there is no return value required. It is like structure but all members in the union share the same memory location

There are eight different primitive data types in JAVA namely byte, short, int, long, float, double, boolean, and char. In primitive data type requires different amounts of memory and has some specific operations which can be performed over it. They include a total of eight data types as follows as named. Among these, the integer data types are byte, short, long, and int. The integer data types are used to store numeric values. In this article, we will discuss the difference between these four Integer data-types. JAVA does not support an unsigned version of these integer data types. The main basis of difference is size and range.

Now, in primitive datatype let’s differentiate byte, short, int, and long to get a better understanding of which is to be used as per the requirements as they possess different traits that play a vital role in the implementation part in any programming language.

  1. byte datatype has a range from -128 to 127 and it requires very little memory (only 1 byte). It can be used in place of int where we are sure that the range will be very small. The compiler automatically promotes the byte variables to type int, if they are used in an expression and the value exceeds their range.
  2. short datatype is the variable range is more than byte but less than int and it also requires more memory than byte but less memory in comparison to int. The compiler automatically promotes the short variables to type int, if they are used in an expression and the value exceeds their range.
  3. int datatype is the most preferred type for numeric values.
  4. long datatype is less frequently used. It should only be used when the range of the numeric value is too high. It requires the most memory(8 bytes) in comparison to the other three data-types.
Читайте также:  Java очередь с приоритетами

The keyword used is ‘short’.

  • If the range of the numeric value is less, and we want to save the memory we can use byte or short depending on the range of values.
  • While if there is a need for medium-range value then we can use the type int but when the range for the numeric values will be larger, then the long type variable must be used to hold the values.
  • Order in terms of range

Источник

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.

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:

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.

1) 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.

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:

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

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:

Читайте также:  Java consumer with exception

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:

double

Sufficient for holding 15 decimal digits
size: 8 bytes
Example:

float

Sufficient for holding 6 to 7 decimal digits
size: 4 bytes

boolean

It holds either true of false.

char

It holds characters.
size: 2 bytes

Literals in Java

A literal is a fixed value that we assign to a variable in a Program.

Here value 10 is a Integer 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";

Check out these basic java programs before proceeding to the next topic:

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Источник

Data Types in Java

Data Types in Java

Java is a strongly typed language. This means that, in Java, each data type has its own strict definition. There are no implicit data type conversions when any conflicts occur between the data types. Any change in data types should be explicitly declared by the programmer.

Java defines 8 primitive data types : byte , short , int , long , char , float , double and boolean .

They are divided into the following categories:

The details of each of the data types is given below :

Integers:

These are of four types: byte , short , int , long . It is important to note that these are signed positive and negative values. Signed integers are stored in a computer using 2’s complement. It consist both negative and positive values but in different formats like (-1 to -128) or (0 to +127) . An unsigned integer can hold a larger positive value, and no negative value like (0 to 255) . Unlike C++ there is no unsigned integer in Java.

byte:

Byte data type is an 8-bit signed two’s complement integer.

Wrapper Class: Byte Minimum value: -128 (-2^7) Maximum value: 127 (2^7 -1) Default value: 0 Example: byte a = 10 , byte b = -50;

short:

Short data type is a 16-bit signed two’s complement integer.

Wrapper Class: Short Minimum value: -32,768 (-2^15) Maximum value: 32,767 (2^15 -1) Default value: 0. Example: short s = 10, short r = -1000;

int:

int data type is a 32-bit signed two’s complement integer. It is generally used as the default data type for integral values unless there is a concern about memory.

Wrapper Class: Integer Minimum value: (-2^31) Maximum value: (2^31 -1) The default value: 0. Example: int a = 50000, int b = -20

long:

Long data type is a 64-bit signed two’s complement integer.

Wrapper Class: Long Minimum value: (-2^63) Maximum value: (2^63 -1) Default value: 0L. Example: long a = 100000L, long b = -600000L; By default all integer type variable is "int". So long num=600851475143 will give an error. But it can be specified as long by appending the suffix L (or l)

Floating­ Point​:

These are also called real numbers and are used for expressions involving fractional precision. These are of two types: float , double . Float is actually avoided in case of precise data such as currency or research data.

Читайте также:  Network unavailable java net socketexception software caused connection abort recv failed

float:

float data type is a single-precision 32-bit IEEE 754 floating point.

Wrapper Class: Float Float is mainly used to save memory in large arrays of floating point numbers. Default value: 0.0f. Example: float f1 = 24.5f; The default data type of floating-point number is double. So float f = 24.5 will introduce an error. However, we can append the suffix F (or f) to designate the data type as float.

double:

double data type is a double-precision 64-bit IEEE 754 floating point. This data type is generally the default choice. This data type should never be used for precise values, such as currency.

Wrapper Class: Double This data type is generally used as the default data type for decimal values. Default value: 0.0d. Example: double d1 = 123.400778;

Character:

We use this data type to store characters. This is not the same as the char in C/C++. Java uses a UNICODE , internationally accepted character set. Char in Java is 16­bits long while that in C/C++ is 8­bits.

Wrapper Class: Character Minimum value: '\u0000' (or 0). Maximum value: '\uffff' (or 65,535). Default value: null ('\u0000'). Example: char letterA ='a';

Boolean:

This is used for storing logical values. A boolean type can have a value of either true or false. This type is generally returned by relational operators.

There are only two possible values: true and false. Wrapper Class: Boolean This data type is used for simple flags that track true/false conditions. Default value is false. Example: boolean b = true, boolean b1 = 1(this is not possible in java and give incompatible type error), boolean b2;

Reference Data Types:

Apart from primitive data types there are reference variables created using constructors of different classes. Reference variables are used for any class as well as array, String, Scanner, Random, Die etc. Reference variables are initialised using the new keyword.

public class Box < int length, breadth, height; public Box()< length=5; breadth=3; height=2; >> class demo < public static void main(String args[]) < Box box1 = new Box(); //box1 is the reference variable char[] arr = new char[10]; //arr is the reference variable >>

String:

String is not a primitive data type, but it lets you store multiple character data types in an array and has many methods that can be used. It is used quite commonly when the user types in data and you have to manipulate it.

In the example below, we try to remove all of the letters from the string and output it:

String input = "My birthday is 10 January 1984 and my favorite number is 42"; String output = ""; for(int i=0;i output = output + input.charAt(i); //the number is added onto the output > System.out.println(output);

More info on Java:

Источник

Оцените статью