Datatype and Variables in Java

5/5 - (1 vote)

Hello everyone, in the previous topic we were talking about Java. Today we are going to talk about Datatype and Variables in Java.

Datatype and Variables in Java

Datatype in JAVA

The datatype is a special keyword used to allocate sufficient memory space for the data, in other words, Data type is used for representing the data in the main memory (RAM) of the computer.

Datatype and Variables in Java
Datatype and Variables in Java

Datatypes in Java are classified into two types-

  • Primitive- boolean, char, int, short, byte, long, float, and double
  • Non-primitive – Classes, Interfaces, and Arrays.
DATA TYPES Range Default SizeDefault Values Ex
boolean True/ False 1 byte
(8 bits)
FALSEboolean b1 = True
byte -128 (-2^7) to 127 (2^7 -1)1 byte 0byte b2 = 14
short -32,768 (-2^15) to 32,767(2^15 -1) 2 byte 0short s1=100
int – 2,147,483,648 (-2^31) to 2,147,483,647(2^31 -1)2 byte 0int j= 10000000
long -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1) 8 byte 0Llong l1= 1000000L
float Single-precision 32-bit IEEE 754 floating point 4 byte 0.0ffloat f1 = 24.7f
double Double-precision 64-bit IEEE 754 floating point 8 byte 0.0ddouble d1 = 142.5
char ‘u0000’ to ‘uffff’ 2 byte ‘u0000’char ch1=’P’
Primitive Datatypes

Boolean

Boolean data type represents only one bit of information either true or false. These values do not correspond to 1 or 0 as in C or C++, but the size of boolean data type is virtual machine-dependent. Values of type boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer can easily write conversion code.

  • Syntax- boolean booleanVar;
  • Size- virtual machine dependent
  • Values- true, false
  • Default Value- false

Byte

The byte data type is an 8-bit signed two’s complement integer. The byte data type is useful for saving memory in large arrays.

  • Syntax- byte byteVar,
  • Size- 1 byte (8 bits)
  • Values -128 to 127
  • Default Value- 0
Example of Byte-
class test {
 public static void main(String args[])
{
 byte a = 126;
System.out.println(a);
a++;
System.out.printIn(a);
 a++;
 System.out.println(a);
a++;
 System.out.printIn(a);
}
}
 Output-
126
 127
 -128
 -127
Example of Byte

Short

The short data type is a 16-bit signed two’s complement integer. The short data type can also be used to save memory just like a byte data type. A short data type is 2 times smaller than an integer.

  • Syntax- short shortVar;
  • Size- 2 byte (16 bits)
  • Values- -32, 768 to 32, 767 (inclusive)
  • Default Value- 0

Int

It is a 32-bit signed two’s complement integer.

  • Syntax- int intVar;
  • Size- 4 byte (32 bits)
  • Values- -2, 147, 483, 648 to 2, 147, 483, 647 (inclusive)
  • Default Value- 0

Note– In Java SE 8 and later, we can use the int data type to represent an unsigned 32-bit integer, which has a value in the range [0, 232-1]. Use the Integer class to use the int data type as an unsigned integer.

Long

The long data type is a 64-bit two’s complement integer.

  • Syntax- long longVar;
  • Size- 8 byte (64 bits)
  • Default Value-0

Note– In Java SE 8 and later, we can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2^64-1.

Float

The float data type is a single-precision 32-bit IEEE 754 floating-point. Use a float (instead of double) if we need to save memory in large arrays of floating-point numbers.

  • Syntax- float floatVar;
  • Size- 4 byte (32 bits)
  • Default Value- 0.0
  • Values- up to 7 decimal digits

Double

The double data type is a double-precision 64-bit IEEE 754 floating-point. For decimal values, this data type is generally the default choice.

  • Syntax- double doubleVar;
  • Size- 8 byte (64 bits)
  • Values- up to 16 decimal digits
  •  Default Value- 0.0

Use float or double?

The precision of a floating-point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.

Char

The char data type is a single 16-bit Unicode character.

  • Syntax- char charVar,
  • Size- 2 byte (16 bits)
  • Values- “\u0000” (0) to “\uffff” (65535)
  • Default Value- “\u0000”

Why does char use 2 bytes in java and what is”\u0000?

It is because Java uses Unicode system not ASCII code system. The \u0000 is the lowest range of the Unicode system.

Note– The range of values is calculated as – (2^n-1) to (2^n-1)-1; where n is the number of bits required. For example, the byte data type requires 1 byte = 8 bits. Therefore, the range of values that can be stored in the byte data type is -(2^8-1) to (2^8-1)–1 = -2^7 to (2^7) -1 = – 128 to 127

Variables in Java

A variable is a name given to a memory location. It is the basic unit of storage in a program.

  1. The value stored in a variable can be changed during program execution.
  2. A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
  3. In Java, all the variables must be declared before use.

Types of Variables

There are three types of variables in Java-

  • Local Variables
  • Instance Variables
  • Static Variables

Local Variables

A variable defined within a block or method or constructor is called a local variable.

These variables are created when the block is entered or the function is called and destroyed after exiting from the block or when the call returns from the function.

The scope of these variables exists only within the block in which the variable is declared. i.e. we can access these variables only within that block.

Initialization of Local Variable is Mandatory.

Example- 1

 class test
{
public void StudentAge()
{
 //local variable age
 int age = 0,
age = age + 5;
System.out.println(“Student age is:”+ age);
}
 public static void main(String args[])
[
test obj  = new test();
obj.StudentAge ();
}
}
 Student age- 5
Example- 1
Example- 2
public class test
{
 public void StudentAge()
{// local variable age
 int age = 0;
age = age + 5;
}
 public static void main(String args[])
{
// using local variable age outside it’s scope
 System.out.println(“Student age is : “+ age);
}
 Compilation Error in java code
 If we use the variable age outside
StudentAge0 function, the compiler will produce an error
Example- 2

Instance Variables

Instance variables are non-static variables and are declared in a class outside any method, constructor, or block.

  • As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.
  • Unlike local variables, we may use access specifiers for instance variables. Do not specify any access specifier then the default access specifier will be used.
  • Initialization of Instance Variable is not mandatory. Its default value is 0.
  • Instance variables can be accessed only by creating objects.

Rules for Instance variables in Java

Following are the rules for the Instance variables-

  1. Instance variables can use any of the four access levels.
  2. They can be marked final.
  3. They can be marked transient.
  4. They cannot be marked as abstract.
  5. They cannot be marked synchronized.
  6. They cannot be marked strictfp.
  7. They cannot be marked, native.
  8. They cannot be marked static.
  9. The instance variable will get a default value means the instance variable can be used without initializing it. The same is not true for the Local Variables.

The default value of the instance variable

INSTANCE VARIABLE TYPE  DEFAULT VALUE
Boolean false
byte (byte)0
short   (short) 0
int   0
long 0L
char u0000
float 0.0f
double 0.0d
  Object null
INSTANCE VARIABLE- TYPE  DEFAULT VALUE

Static Variables

 Static variables are also known as Class variables.

  • These variables are declared similarly instance variables, the difference is that static variables are declared using the static keyword within a class outside any method constructor or block.
  • Unlike instance variables, we can only have one copy of a static variable per class irrespective of how many objects we create.
  • Static variables are created at the start of program execution and destroyed automatically when execution ends.
  • Initialization of Static Variable is not Mandatory. Its default value is 0.

Here we are going to define Static Variable in brief-

  1. If we access the static variable like the Instance variable (through an object), the compiler will show the warning message and it won’t halt the program. The compiler will replace the object name with the class name automatically.
  2. A static method can access only static data. It can not access non-static data (instance variables).
  3. A static method can call only other static methods and can not call a non-static method from it.
  4. A static method cannot refer to “this” or “super” keywords in any way.
  5. If we access the static variable without the class name, the Compiler will automatically append the class name.
  6. To access static variables, we need not create an object of that class, we can simply access the variable as.

class_name.variable_name;

Example- class_name.variable_name;
 class Emp
{
 // static variable salary
 public static double salary;
 public static String name = “Ram”;
}
 public class EmpDemo
{
 public static void main(String args[])
{
/ /accessing static variable without object
 Emp salary= 5000;
 System.out println(Empname +”s average salary” + Emp salary);
}
}
 Output
Ram average salary-5000.0
Ecample of class_name.variable_name;

 

Instance variable Vs Static variable

  • Each object will have its own copy of the instance variable whereas We can only have one copy of a static variable per class irrespective of how many objects we create.
  • Changes made in an instance variable using one object will not be reflected in other objects as each object has its own copy of the instance variable. In the case of static, changes will be reflected in other objects as static variables are common to all objects of a class.
  • We can access instance variables through object references and Static Variables can be accessed directly using the class name.

 The syntax for static and instance variables:

class Example
{
static int a; //static variable
int b; //instance variable
}
 

So, that is all for today guys see you in our next blog. If you like our article please don’t forget to share with others & follow our Instagram page for your daily dose of Motivation.

Thank You,

Regards

Grooming Urban

General FAQ

What is a datatype in Java?

The datatype is a special keyword used to allocate sufficient memory space for the data, in other words, Data type is used for representing the data in the main memory (RAM) of the computer.

How many datatype in Java?

Datatypes in Java are classified into two types-
Primitive- boolean, char, int, short, byte, long, float, and double
Non-primitive – Classes, Interfaces, and Arrays.

Which is better float or double?

The precision of a floating-point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.

Why does char use 2 bytes in java and what is”\u0000?

It is because Java uses Unicode system not ASCII code system. The \u0000 is the lowest range of the Unicode system. The range of values is calculated as – (2^n-1) to (2^n-1)-1; where n is the number of bits required. For example, the byte data type requires 1 byte = 8 bits. Therefore, the range of values that can be stored in the byte data type is -(2^8-1) to (2^8-1)–1 = -2^7 to (2^7) -1 = – 128 to 127

What are variables in Java?

A variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be changed during program execution. A variable is only a name given to a memory location, all the operations done on the variable effects that memory location. In Java, all the variables must be declared before use.

How many types of variables?

There are three types of variables in Java-
1. Local Variables
2. Instance Variables
3. Static Variables

What are Local variables?

A variable defined within a block or method or constructor is called a local variable.

What are Instance variables?

Instance variables are non-static variables and are declared in a class outside any method, constructor, or block.

What are the rules for Instance variables?

Following are the rules for the Instance variables-
1. Instance variables can use any of the four access levels.
2. They can be marked final.
3. They can be marked transient.
4. They cannot be marked as abstract.
5. They cannot be marked synchronized.
6. They cannot be marked strictfp.
7. They cannot be marked, native.
8. They cannot be marked static.
9. The instance variable will get a default value means the instance variable can be used without initializing it. The same is not true for the Local Variables.

What are Static variables?

Static variables are also known as Class variables. These variables are declared similarly as instance variables, the difference is that static variables are declared using the static keyword within a class outside any method constructor or block.

Sharing Is Caring:
Sneha Kriti

An aspiring MCA student formed an obsession with Computer Science And Coding with HTML, C, C++, Java, and Python now helping others to improve in their studies, grooming and personality traits as well.

1 thought on “Datatype and Variables in Java”

Leave a Comment