top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java Data types

0 votes
470 views

Data Types

When you define a variable in java, you must tell the compiler what king of a variable it is. That is, whether it will be expected to store an integer, a character, or some other kind of data. This information tells the compiler how much space to allocate in the memory depending on the data type of the variable. The data type of a variable, therefore determines the type of data that can be stored in the variable and the operations that can be performed of this data.

In java programming language, data types are mainly divided into two categories:

  • Primitive data types: In java programming language provides primitive data types to store and represent data. A primitive data type, also called built-in data types, stores a single value, normally a literal of some sort, such as a number or a character.

The table shown below summarizes the primitive data types in java.

Data Type

Description

Range

byte

8-bit signed integer

-128 to 127

short

16-bit signed integer

-32768 to 32767

long

64-bit signed integer

-9,223,372,036,854,775,808to 9,223,372,036,854,775,807

int

32-bit signed integer

-2,147,483,648to +3.40292347E+38

boolean

Stores a true or false value

True or false

char

16-bit Unicode character

0 to 65535

double

64-bit floating-point variable

-1.79769313486231570E+308 to 1.79769313486231570E+308

 

Signed Integers

Signed integers are those integers, which are capable of representing positive as well as negative numbers, for example -40

Data Types on different platforms

Data types supported by java language are implemented in the same format across all platforms. For instance, a variable declared of type int in java is allocated 4-bytes of memory on all machines, no matter where the java program runs. Due to this reason, java programs need be modified to run on different platforms.

Examples of primitive data types:-

int empNumber;

float salary;

char gender=’M’;

double shareBalance = 456790.897;

boolean ownvehicle = false;

  • Reference Data Type:

A variable of a reference data type holds the reference to the actual value or a set of values represented by the variable.

The table displays three reference data types that are available in java.

Data Type

Description

Array

A collection of several items of the same data type such as names of students.

Class

A collection of variables and methods. For example, class student containing the complete details of the students and the methods that operate on the details.

Interface

A type of class in java used to implement multiple inheritance.

 

Go through this video:-

posted Jun 21, 2017 by Md Irfan

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Classification

Reference data types store the memory reference of other variables. These other variable hold the actual values. Reference type can be classified as:

Object: Object is a built-in reference data type. It is a base class for all predefined and user-defined data types. A class is a logical structure that represents a real world entity. This means that the predefined and user-defined data types are created on the object class.

String: String is a built-in reference type. String type signifies Unicode character string values. Once string are created, they cannot be modified.

Class: A class is user-defined structure that contains variables and methods. For example, the Employee class can be a user-defined structure that can contain variables such as empsalary, empname, and empAddress. In addition, it can contain methods such as CalculateSalary() , which returns the net salary of an employee.

Delegate: A delegate is a user-defined reference type that stores the reference of one or more methods.

Interface: An interface is a type of user-defined class that is used for multiple inheritance.

Array: An array is user-defined data structure that contains values of the same data type, such as marks of students.

Variable Naming Rules

A variable needs to be declared before it can be referenced. You need to follow certain rules while declaring a variable:

  • A variable name can begin with an upper case or a lower case letter. The name can contain letters, digits and the underscore character(_).
  • The first character of the variable name must be a letter and not a digit. The underscore is also a legal first character, but it is not recommended at the beginning of a name.
  • C# is a case-sensitive language; hence variable names count and count refer to two different variables.
  • C# keywords cannot be used as variable names. If you still need to use a c# keyword prefix it with the ‘@’ symbol.

Note: Microsoft recommends camelcase notation for c# variable names. You should not use underscores and must ensure that the first letter of the identifier is in lowercase. In addition, you must capitalize the first letter of each subsequent word of the identifier. For example, consider the following variable declarations:

Int totMonths = 12;

String empName = “John Ferb”;

Bool statusInfo = true;

 

Create and use variables

A variable’s type and identifier (name ) need to be mentioned when you declare a variable. This tells the compiler the name of the variable and the variable and the type of data the variable will store. If you attempt to use an undeclared variable, the compiler will generate and error message. The table given below displays a list of valid and invalid variable names in c#.

Variable Name

Valid/Invalid

Employee

Valid

student

Valid

_Name

Valid

Emp_Name

Valid

@goto

Valid

static

Invalid as it is a keyword

4myclass

Invalid as a variable cannot start with a digit

Student&Faculty

Invalid as a variable cannot have the special character &

Note: When you declare a variable, the computer allocates memory for it. Hence, to avoid wasting computer memory, it is recommended to declare variables only when required.

READ MORE

Variable and Data Types in C#

A variable is an entity whose value can keep changing. For example, the age of a student, the age of a student, the address of a faculty member and the salary of an employee are all examples of variables.

  

In C#, a variable is a location in the computer’s memory that is identified by a unique name and is used to store a value. The name of the variable is used to access and read the value stored in it. Different types of data such as a character, an integer or a string can be stored in variables. Based on the type of data that needs to be stored in variable, variables can be assigned different data types.

Using variables

In C# , memory is allocated to a variable at the time of its creation. During creation, a variable is given a name that uniquely identifies the variable within its scope. For example, you can create a variable called empName to store the name of an employee.

You can initialize the variables at the time of creating the variable or at a later time. Once initialized, the value of a variable can be changed as required.

In c#, variable enable you to keep track of data. When you are referring to a variable, you are actually referring to the value stored in that variable.

The following syntax is used to declare variable in c#,

<datatype> <variableName>;

Where,

Datatype: Is a valid data type in C#.

VariableName: Is a valid variable name.

Data Types

You can store different types of values such as numbers, characters or strings in different variables. But the compiler must know what kind of data a particular variable is expected to store. To identify the type of data that can be stored in a variable, c# provides different data types.

When a variable is declared, a data type is assigned to the variable. This allows the variable to store values of the assigned data types. In C# programming language, data types are divided into two categories. These are:

 

Value Types: variable of value types store actual values. These values are stored in a stack. The values can be either of a built-in data type or a user-defined data type. Most of the built-in data type are value types. The value type built-in data types are int, float, double, char and bool. Stack storage results in faster memory allocation to variables of value types.

Reference Type: Variables of reference type store the memory address of other variables in a heap. These values can either belong to a built-in data type or a user-defined data type. For example, string is a built-in data type which is a reference type. Most of the user-defined data types such as class are reference types.

Predefined Data Types

The predefined data types are referred to as basic data types in c#. These data types have a predefined range and size. The size of the data type helps the compiler to allocate memory space and the range helps the compiler to ensure that the value assigned is within the range of the variable’s data type.

The predefined data types in c# is given below:-

Data Type

Size

Range

byte

Unsigned 8-bit integer

0 to 255

short

Signed 16-bit integer

-32, 768 to 32,767

int

Signed 32-bit integer

-2,147,483,648 to 2,147,483,647

long

Signed 64-bit integer

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float

32-bit floating point with 7 digits precisions

±1.5e-45 to ±3.4e38

double

64-bit floating point with 15-16 digits precision

±5.oe-324 to ± 1.7e308

decimal

128-bit floating point with 28-29 digits precision

±1.0  x 10e-28 to ±7.9 x 10e28

char

Unicode 16-bit character

U+0000 to U+ffff

bool

Stores either true or false

True or flase

 

READ MORE

What is Data Type

A type of data is called the data type (or in simple word we can say type of variable is called data type) and its true for all compiler based computer languages where as in most of the interpreter based language a variable type is not fixed rather dependent at the usage at a particular instance.

Classification of Data Types

In C we can classify the data types in four ways -

1. Basic Types:
Basic Data types are arithmatic data types and can be of two types i.e.
(a) integer types (char is also an integer)

Type            Storage size    Value range
char            1 byte          -128 to 127 or 0 to 255
unsigned char   1 byte          0 to 255
signed char     1 byte          -128 to 127
int             2 or 4 bytes    -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int    2 or 4 bytes    0 to 65,535 or 0 to 4,294,967,295
short           2 bytes         -32,768 to 32,767
unsigned short  2 bytes         0 to 65,535
long            4 bytes         -2,147,483,648 to 2,147,483,647
unsigned long   4 bytes         0 to 4,294,967,295

(b) floating-point types.

Type        Storage Size    Value range          Precision
float          4 byte     1.2E-38 to 3.4E+38     6 decimal places
double         8 byte     2.3E-308 to 1.7E+308   15 decimal places
long double    10 byte    3.4E-4932 to 1.1E+4932 19 decimal places

2. Enumerated types:
They are again arithmetic types and they are used to define variables that can only be assigned certain discrete integer values throughout the program.

Example

enum month { jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec };
enum month this_month;

In the above declaration, month is declared as an enumerated data type. It consists of a set of values, jan to dec. Numerically, jan is given the value 1, feb the value 2, and so on dec as 12. So variable this_month can take any value from 1 to 12 as an integer or jan to dec as enum.

3. The type void:
The type specifier void indicates that no value is available. there can be three cases which can be described as void data types in C.
(a) Function returns as void
A function which do not return value or you can say they return void.
(b) Function arguments as void
A function with no parameter can accept void as parameters. For example, int myfunc(void);
(c) Pointers to void
A pointer of type void * represents the address of an object, but not its type. For example a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be type casted to any data type.

4. Derived types:
These are
(a) Pointer types
(b) Array types
(c) Structure types
(d) Union types and
(e) Function types

READ MORE
...