Basic data types
Variables are memory locations that are reserved for storing values. This means that when you create a variable, it will take up a certain amount of space in memory.
Based on the data type of variables, the operating system makes memory allocation and decides what will be stored in reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or letters in these variables.
There are two efficient data types in Java:
Original data type
Java supports 8 primitive data types. The original data type is predefined by the language and named with keywords. Let's learn more about these 8 data types below.
Byte type (byte)
Byte type is an 8-bit binary integer with positive and negative minimum value is -128(-2^7)
The maximum value is 127(2^7-1)
The default value is 0
Byte type data types are mainly used to save space in large arrays, and are mainly used to replace integers. Because byte type is 4 times smaller than integers.
For example:
byte a = 100 , byte b = -50
Short integer (short)
Short integer is a 16-bit binary integer with positive and negative minimum value is -32768(-2^15)
The maximum value is 32767(2^15-1)
Short integer type data can also be used to save space like byte types. Short integers are twice smaller than integers. The default value is 0.
For example:
short s = 10000, short r = -20000
Int type (int)
Integer type is a 32-bit binary integer with positive and negative minimum value is - 2,147,483,648(-2^31)
The maximum value is 2,147,483,647(2^31 -1)
Integer types are generally applied to integer values by default unless you are worried about insufficient memory.
The default value is 0
For example:
int a = 100000, int b = -200000
Long type (long)
Long integer is a 64-bit binary integer with positive and negative minimum value is -9,223,372,036,854,775,808(-2^63)
The maximum value is 9,223,372,036,854,775,807 (2^63 -1)
This data type is generally applied when a larger range than an integer type is required.
The default value is 0L
For example:
long a = 100000L, int b = -200000L
Float
Floating point data is a single-precision 32-bit IEEE 754 standard floating point data.
Floating point data is mainly used to save memory in large floating point digital arrays.
The default value is 0.0f.
Floating point data cannot be used for precise data such as currency.
For example:
float f1 = 234.5f
Double precision type (double)
Double precision data is a double precision 64-bit IEEE 754 standard floating point data.
This data type is mainly used by default to represent the value of the decimal, and is generally the default choice.
Double-precision data cannot be used for precise data such as currency.
The default value is 0.0d
For example:
double d1 = 123.4
Boolean
Boolean data represents an information bit.
It has only two possible values: true (true) and false (false)
This data type is used for simple tags under real or false conditions.
The default value is false (false)
For example:
boolean one = true
Character type (char)
Character data is simple 16-bit Unicode standard characters.
The minimum value is: '/u0000' (or 0).
The maximum value is: '/uffffff' (or 65,535 ).
Character data can be used to store any letter.
For example: char letter A (character letter A) ='A'
Reference data type
The reference data type is defined by the class's editor. They are used to access objects. These variables are defined as specific types that are not alterable. For example: Employee, Puppy, etc.
Class objects and array variables are this type of reference data type.
The default value for any referenced data type is empty.
A reference data type can be used for any object that declares and compatible types.
For example:
Animal animal = new Animal("giraffe"); Java Constants
Constants are source code representing fixed values. They are represented directly in code form without any estimates.
Constants can be assigned to any original variable type. For example:
byte a = 68;char a = 'A'
Byte, integer, long and short can also be represented by decimal, hexadecimal and octal counting systems.
When these technical systems represent direct quantities, the prefix 0 is to indicate octal, and the prefix 0x is to indicate hexadecimal. For example:
int decimal = 100;int octal = 0144;int hexa = 0x64;
The provisions of string constants in Java, like most other languages, should also be written in the middle of double quotes. Examples of string-type direct quantity are as follows:
"Hello World""two/nlines""/"This is in quotes/""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Character and string constants can contain any Unicode letters. For example:
char a = '/u0001';String a = "/u0001";
The Java language also supports special escape sequences of characters and strings directly. They are:
| Escape characters | meaning |
|---|---|
| /n | Line break (0x0a) |
| /r | Enter (0x0d) |
| /f | Page change (0x0c) |
| /b | Backspace (0x08) |
| /s | Space(0x20) |
| /t | tab |
| /" | Double quotes |
| /' | Single quotes |
| / | Backslash |
| /ddd | Octal characters (ddd) |
| /uxxxxx | Hexadecimal UNICODE characters (xxxx) |
Before using it, you must now declare the variable you want to use. The basic format of declaring variables is as follows:
data type variable [ = value][, variable [= value] ...] ;
The data type here is a data type in Java, and variable is the name of a variable. To declare more than one specific variable type, you can use commas to separate it.
The following are examples of valid variable declarations and assignments in Java:
int a, b, c; // Declares three ints, a, b, and c.int a = 10, b = 10; // Example of initializationbyte B = 22; // initializes a byte type variable B.double pi = 3.14159; // declares and assigns a value of PI.char a = 'a'; // the char variable a iis initialized with value 'a'
There are three variables in Java:
Local variables
example
Here, age (age) is a local variable. This is defined under the pupAge() method, and its scope is limited to this method.
public class Test{ public void pupAge(){ int age = 0; age = age + 7; System.out.println("Puppy age is : " + age); } public static void main(String args[]){ Test test = new Test(); test.pupAge(); }}The above code will output the following results:
Puppy age is: 7
Example The following example uses the local variable age but is not initialized, so an error will be displayed when editing.
public class Test{ public void pupAge(){ int age; age = age + 7; System.out.println("Puppy age is : " + age); } public static void main(String args[]){ Test test = new Test(); test.pupAge(); }}The following error will be generated during editing:
Test.java:4:variable number might not have been initializedage = age + 7;^1 error
Instance variables
example
import java.io.*;public class Employee{ // this instance variable is visible for any child class. public String name; // salary variable is visible in Employee class only. private double salary; // The name variable is assigned in the constructor. public Employee (String empName){ name = empName; } // The salary variable is assigned a value. public void setSalary(double empSal){ salary = empSal; } // This method prints the employee details. public void printEmp(){ System.out.println("name : " + name ); System.out.println("salary :" + salary); } public static void main(String args[]){ Employee empOne = new Employee("Ransika"); empOne.setSalary(1000); empOne.printEmp(); }}The above code will output the following results:
name: Ransikasalary:1000.0
Class, static variables
example
import java.io.*;public class Employee{ // salary variable is a private static variable private static double salary; // DEPARTMENT is a constant public static final String DEPARTMENT = "Development "; public static void main(String args[]){ salary = 1000; System.out.println(DEPARTMENT+"average salary:"+salary); }}The above code will output the following results:
Development average salary:1000
Note: If a variable is accessed from outside the class, the constant must be accessed as Employee.DEPARTMENT.