java data type

 

int type (4 bytes) (- 2 ^ 31 ~ 2 ^ 31-1)

In java, one int byte accounts for 4 bytes, which is not directly related to the operating system (8 binary bits = 1 byte)

System.out.println(Integer.MAX_VALUE);//Calculate the maximum and minimum of shaping respectively
System.out.println(Integer.MIN_VALUE);

If the result of the operation exceeds the max value of int, an overflow condition occurs.

For the era of big data, data can easily exceed int_max, so the long type is introduced.

long type (8 bytes) (- 2 ^ 64 ~ 2 ^ 64 - 1)

long num=10L;
long num1=10l;//Define variables of type long

It is also possible to use 10 for initialization, but it is better to use 10L or 10L.

System.out.println(Long.MAX_VALUE);//Find the maximum and minimum of Long shape respectively
System.out.println(Long.MIN_VALUE);

double(8 bytes)

bouble num=1.0;//initialization
double a = 1.1;
System.out.println(a * a);//1.2100000000000002
/*double The layout type complies with IEEE754 standard and tries to use limited memory space to represent koneng's infinite decimal
,There will be some accuracy error*/

Although double in java is also 8 bytes, the memory layout of floating-point numbers is very different from that of integers, and the range cannot be expressed simply in the form of n-power of 2.

float(4 bytes)

float num=1.0f;
float num1=1.0F;//initialization

  Because float represents a small range of data precision, double is recommended for general floating-point types.

char type (2 bytes)

char ch = 'A';//initialization

In Java, the form of single quotation mark + single letter is used to represent the literal value of a character.

char ch2 = 97;//
System.out.println(ch);//The result is a
        

char in computer is essentially an integer. ASCII is used to represent characters in C language, while Unicode is used to represent characters in Java. Therefore, a character occupies two bytes and represents more types of characters, including Chinese.

  Byte (1 byte)

byte value = 0; 
System.out.println(value); //byte type initialization

Byte types also represent integers.

Byte only takes up one byte, indicating a small range (- 128 - > + 127)

Byte type and character type are irrelevant.

 byte ab=10;
 byte cd=20;
 byte de=(byte)(ab+cd);//short and byte types are converted to 4-byte int types before calculation, so they need to be cast

short (2 bytes)

short value = 0; 
System.out.println(value); //short type initialization

  The data range represented by short is - 32768 - > + 32767   ps: This indicates that the range is relatively small and is generally not recommended.

boolean (not necessarily)

boolean value = true; 
System.out.println(value); //boolean type initialization

  There are only two values for boolean variables. True means true and false means false

Java boolean type and int cannot be converted to each other. There is no such usage as 1 for true and 0 for false  

boolean type. Some JVM s occupy 1 byte and some occupy 1 bit. This is not explicitly specified

String()

String name = "zhangsan"; 
System.out.println(name);//string type initialization

Java uses double quotation marks + several characters to represent string literals

Unlike the above types, String is not a basic type, but a reference type

Some specific characters in the string that are not convenient for direct representation need to be escaped

Examples of escape characters:

  
String name = "My name is \"Zhang San\"";
//"\" bit "\" is required for printing bit
//"\ \ bit" is required to print \ bit \\“
//Printing \ \ bit \ \ requires "\ \ \ \ bit"\\“

 

  There are many escape characters, some of which are common as follows:

\n line feed

\t horizontal tab

\'single quote

\Double quotes

\Backslash

String + operation:   Represents string splicing:

System.out.println("hello"+(10+20));//hello30
System.out.println(10+""+20+"hello");//1020hello
 String a = "hello"; 
String b = "world"; String c = a + b; 
System.out.println(c); //helloworld

You can also splice strings and integers:

 String str = "result = "; 
int a = 10; 
int b = 20; 
String result = str + a + b;
 System.out.println(result); //result=1020

  The above code shows that string splicing is performed when there is a string in a + expression   Therefore, we can easily use System.out.println to print multiple strings or numbers at the same time:

int a = 10;
 int b = 20;
 System.out.println("a = " + a + ",b = " + b);

Introduce the concept of string.valueof()  

(1) String.valueOf(boolean b): converts boolean variable B to a string

(2) String.valueOf(char c): converts char variable C to a string

(3) String.valueOf(char[] data): converts char array data into a string

(4) String.valueOf(char[] data, int offset, int count): convert count elements from data[offset] in char array data into strings

(5) String.valueOf(double d): converts the double variable d to a string

(6) String.valueOf(float f): converts the float variable f to a string

(7) String.valueOf(int i): converts the int variable I to a string

(8) String.valueOf(long l): converts the long variable l into a string

(9) String.valueOf(Object obj): converts an obj object into a string, which is equal to obj.toString()

Introduce the concept of Integer.parseInt()

Integer.parseInt() converts integer data to basic data type int

Tags: Java JavaSE

Posted on Thu, 11 Nov 2021 05:37:47 -0500 by Sulphy