Fully resolve the data types supported by Java and the constant and variable types of Java
- 2020-05-05 11:14:47
- OfStack
basic data type
The variable is the memory location that is reserved for storing values. This means that when you create a variable it takes up some space in memory.
Based on the variable's data type, the operating system allocates memory and decides what will be stored in reserved memory. Therefore, by assigning variables to different data types, you can store integers, decimals, or letters in these variables.
There are two valid data types in Java:
The raw data type refers to the data typeraw data type
Java supports eight primitive data types. The raw data types are predefined by the language and named by keywords. Let's take a closer look at these eight data types.
byte type (byte)
The byte type is an 8-bit binary integer with plus or minus
The minimum is -128(-2^7)
The maximum is 127(2^7-1)
The default value is 0
Byte data types are mainly used to save space in large arrays, mainly to replace integers because bytes are 4 times smaller than integers.
For example:
byte a = 100 , byte b = -50
short integer (short)
A short integer is a 16-bit binary integer with plus or minus
The minimum is -32768(-2^15)
The maximum is 32767(2^15-1)
Data of short integer type can also be used to save space just like byte type. Short integers are two times smaller than whole ones
The default value is 0
For example:
short s = 10000, short r = -20000
integer type (int)
Integer type is a 32-bit binary integer with plus or minus
The minimum is minus 2,147,483,648(minus 2 to the 31)
The maximum is 2,147,483,647(2^ 31-1)
Integer types are generally applied to integer values by default unless memory runs out.
The default value is 0
For example:
int a = 100000, int b = -200000
long integral (long)
A long integer is a 64-bit binary integer with plus or minus
The minimum value is - 9223372036854775808 (63) - 2 ^
Maximum is 9223372036854775807
(2 ^ 63-1)
This data type is generally used when you need a larger range than an integer type.
The default value is 0L
For example:
long a = 100000L, int b = -200000L
floating point (float)
Floating point data is a single-precision floating point data in the 32-bit IEEE 754 standard.
Floating point data is primarily used to save memory in large arrays of floating point Numbers.
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 (double)
Double-precision data is a double-precision 64-bit floating point data in the IEEE 754 standard.
This data type is mainly used by default to represent decimal values, 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)
A Boolean data represents a bit of information.
It has only two possible values: true (true) and false (false)
This data type is used for simple markup under true or false conditions.
The default value is false (false)
For example:
boolean one = true
character type (char)
The character data is a simple 16-bit Unicode character.
The minimum value is: '\u0000' (or 0).
The maximum value is: '\uffff' (or 65,535).
Character data can be used to store any letter.
For example: char letter A (character letter A) ='A'
refers to the data type
The reference data type is defined by the editor of the class. They are used to access objects. These variables are defined as specific types that cannot be changed. For example: Employee, Puppy, etc.
Class objects and array variables are such reference data types.
The default value for any reference data type is null.
A reference data type can be used for any object that declares a type and a compatible type.
For example:
Animal animal = new Animal("giraffe") ;
Java constant
The constant is the source code that represents a fixed value. They are represented directly in code without any estimation.
Constants can be assigned to any primitive variable type. For example:
byte a = 68;
char a = 'A'
Byte, integer, long and short integers can also be represented by decimal, hexadecimal and octal counting systems.
When these technical systems are used to represent direct quantities, the prefix 0 is for octal and the prefix 0x is for hexadecimal. For example:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
String constants in Java, as in most languages, are written in double quotes. An example of a string direct quantity is
short s = 10000, short r = -20000
0
Characters and string constants can contain any Unicode letter. For example:
short s = 10000, short r = -20000
1
The Java language also supports some special escape sequences of characters and string direct quantities. They are:
转义字符 | |
---|---|
\n | 换行 (0x0a) |
\r | 回车 (0x0d) |
\f | 换页 (0x0c) |
\b | 退格 (0x08) |
\s | 空格 (0x20) |
\t | tab |
\" | 双引号 |
\' | 单引号 |
\ | 反斜杠 |
\ddd | 八进制字符 (ddd) |
\uxxxx | 十六进制 UNICODE 字符 (xxxx) |
variable type
The variable gives us a named store that our program can manipulate. Each variable in Java has a specific type, which determines the size of the variable and its design footprint; The values of these columns can be stored in that memory space; Operations that variables can apply to.
You must now declare the variables you want to use before you can use them. The basic format for declaring variables is as follows:
data type variable [ = value][, variable [= value] ...] ;
Here data type is a data type of Java and variable is the name of a variable. To declare more than one specific variable type, you can separate it with a comma.
Here is an example of a valid variable declaration and assignment in Java:
short s = 10000, short r = -20000
2
There are three variables in Java:
Local variable instance variable class, static variablelocal variable
A local variable in a method, the constructor or a local variable in a method declared in a block, constructor or block to be created, once out of the variable can be destroyed but can't access descriptor for local variables are local variables in only the declared method, constructor or visible local variables within the stack depth in implementation of the local variable is no default value, so the local variable must be declared and used for the first time to assign a value to it beforeExample
Here, age (age) is the 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 code above will output the following:
short s = 10000, short r = -20000
4
Example
The following example USES the local variable age but is not initialized, so an error is displayed during editing.
short s = 10000, short r = -20000
5
The following error occurs when editing:
short s = 10000, short r = -20000
6
instance variable
Example
short s = 10000, short r = -20000
7
The code above will output the following:
name : Ransika
salary :1000.0
class, static variable
Example
short s = 10000, short r = -20000
9
The above code will output the following:
Development average salary:1000
Note: if the variable is accessed from outside the class, the constant must be accessed as Employee.DEPARTMENT.