Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sample Analysis of data types in JavaSE

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about sample analysis of data types in Java SE. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Java Type Summary

integer variable-int\long integer variable-long\short integer variable-short

Basic syntax format:

int variable name = initial;long variable name = initial;short variable name = initial;

Package: Integer (still plastic); Long ; Short

Examples:

int num = 10; //Defines an integer variable System.out.println(num);long num = 10L; //Defines a long integer variable with an initial value of 10L (lowercase L, not 1). System.out.println(num);short value = 0; System.out.println(value);

Note: Java is a strongly typed language, 10 defaults to int type, without L, the left and right types of equal signs do not match

Integer data range:

int type takes 4 bytes, data range is-2^31 ~2^31 -1

How to view:

System.out.println(Integer.MAX_VALUE); // int max 2147483648System.out.println(Integer.MIN_VALUE); // int min-2147483648

Long Shaped Data Range:

The long type takes up 8 bytes. Range of data represented- 2^63 - > 2^63 - 1

How to view:

System.out.println(Long.MAX_VALUE);\\ 9223372036854775807System.out.println(Long.MIN_VALUE);\\-9223372036854775807

Short Shaped Data Range:

short takes up 2 bytes and represents the data range of- 32768 - > +32767

How to view:

System.out.println(Short.MAX_VALUE); System.out.println(Short.MIN_VALUE);

Note:

Overflow occurs when the result of an operation is outside the range of the corresponding type (see circle range diagram above).

For example, an int type:

System.out.println(Integer.MAX_VALUE+1);//The result is-2147483648System.out.println(Integer.MIN_VALUE-1);//The result is 2147483648

Pay particular attention to:

Variable definition must be initialized, otherwise compilation does not pass (java language security is high)

Variable size is independent of platform digits (Java language portability is high)

floating-point variable-double\float

Basic syntax:

double name = initial value;float name = initial value;

Examples:

double num = 1.0;System.out.println(num)float num = 1.0f; System.out.println(num);

Note: Java is a strongly typed language, 1.0 defaults to double type, without f, the left and right types of the equal sign do not match

Output:

Use printf if you want to specify how many decimal places to output

System.out.printf("%.nlf",num);\\nIndicates that you want to output a few decimal places

Note:

1. Java double is 8 bytes;float is 4 bytes

2. When you divide integers, you get an integer, and if you want to get a decimal point, you need a double type operation.

3. Floating-point number storage and integer storage is different, java floating-point number storage reference C language (comply with IEEE754 standard)

Character Type Variable-char

Basic format:

char variable name = initial value;

Examples:

char ch2 = 'A';char ch3 = ''; System.out.println(ch);

Note: In Java, single quotes + single letters are used to represent character literals.

Character Type Data Range:

char type in java takes up two bytes (representing more character types, including Chinese)

System.out.println(Character.MAX_VALUE); // char max '\uFFFF'System.out.println(Character.MIN_VALUE); // char minimum value '\u0000'

Note: Characters in computers are integers in nature: ASCII is used to represent characters in C language, Unicode is used to represent characters in Java.

Note:

When there is coding in Chinese, add-encoding UTF-8 when compiling and executing javac (otherwise, compilation error will occur)

Byte type change-byte

Basic syntax format:

byte variable name = initial value;

Examples:

byte value = 0; System.out.println(value);

Note: byte type is also an integer, only one byte, indicating a smaller range ( -128 -> 127 )(different from char)

Integer promotion occurs when bytes are less than 4 bytes

Boolean type variable-boolean

Basic syntax format:

boolean variable name = initial value;//initial value can only be true\flase

Examples:

boolean value = true; System.out.println(value);

Note:

1. boolean variables have only two values, true for true and false for false (unlike C)

2. boolean type and int cannot be converted to each other in Java (type incompatibility), there is no such usage as 1 means true, 0 means false

3. boolean type Some JVM implementations are 1 byte, some are 1 bit, this is not explicitly specified

String Type Variable-String

String is not a primitive type, it is a reference type

Basic syntax format:

String variable name = "initial value";

Examples:

String name = "zhangsan"; System.out.println(name);

Note: Java uses double quotes + several characters to represent string literals

name stores the address of the space occupied by the string

string concatenation

String "+" Any type of data results in string

Examples:

//String concatenation String a = "hello"; String b = "world"; String c = a + b; System.out.println(c); Output result: hello world//String concatenation int a = 10;System.out.println("a = " + a); Output result: a=10 Note: You can add parentheses when you want to calculate the data first

Examples:

int a = 10;int b = 20;System.out.println("a+b = " + (a + b)); output result: a+b=30 string escape Some specific characters in the string that are not convenient to express directly need to be escaped (same as C language)

Examples:

String str = "Hello \"xxx\" ! ";System.out.println(str); Output result: Hello "xxx" !

Special escape characters:

Escape sequences for special characters

Escape Character Interpretation Unicode Value\b Backspace\u0008\tTabs\u0009\n Line feed\u000a\r Carriage return\u000d\''Double quotes\u0022\'Single quotes\u0027\ \Backslash\u005c Scope of variable

Definition: refers to the range in which variables can take effect,

Generally is the variable definition in the code block (curly brackets)(consistent with C language)

Naming conventions for variables

Hard indicators:

1. A variable name can only contain numbers, letters, underscores,

2. Numbers cannot begin.

3. Variable names are case sensitive. So num and Num are two different variables.

Note: Although the Chinese/dollar sign ($) is syntactically allowed for naming variables, it is strongly discouraged

Soft indicators:

1. Variable names should be descriptive, see the name to know the meaning

2. Variable names should not use pinyin (but not absolute)

3. Variable name part of speech Recommended noun

4. Variable naming recommended small hump nomenclature, when a variable name consists of more than one word, except for the first word, all other words are capitalized.

constant

Definition: means that the runtime type cannot be changed

literal constant 10 // int literal constant (decimal) 010 // int literal constants (octal) start with the number 0. 010 is decimal 8 0 x 10. // int literal constants (hexadecimal) start with the number 0x. 0x10 is decimal 16 10L // long literal constant. It can also be written as 10l (lowercase L) 1.0 // double literal constant. It can also be written as 1.0d or 1.0D 1.5e2. // double literal constant. Scientific notation. Equivalent to 1.5 * 10^2 1.0f // float literal constant, can also be written as 1.0F true // boolen literal constants, also false 'a' // char literal constant, only one character "abc" in single quotation marks // String literal constant, double quotes can have multiple characters final keyword modified constant final int a = 10; a = 20; //compilation error. Prompt cannot assign value to final variable a

Note: Constants cannot be modified during program execution.

Type conversion int and long/double mutually assign

implicit type conversion

small-scope type assigned to large-scope type (no error)

dominant type conversion

Large scope type assigned to small scope type (error)

Examples:

int a = 10; long b = 20; a = b;//Compilation error, indicating that precision may be lost b = a; //Compilation passes Note: Using forced type conversion, double type can be forced to int, but precision may be missing int and boolean mutual assignment Example: int a = 10; boolean b = true; b = a; //Compilation error, indicating incompatible type a = b; //Compilation error, indicating incompatible type

Note: int and boolean are unrelated, and unrelated types cannot be strongly converted.

Summary:

1. Assignment between variables of different numeric types means that a smaller range of types can be implicitly converted to a larger range of types

2. If you need to assign a large-range type to a small-range type, you need to cast, but you may lose precision.

3. Java automatically checks for numeric ranges when assigning a literal constant

int a = 10; long b = 20; int c = a + b; //Compilation error, indicating that converting long to int loses precision long d = a + b; //Compilation passes

Conclusion:

When int and long are mixed, int will be promoted to long , and the result will be long type, which needs long type to receive

If you have to use int to receive the result, you need to use cast

byte and byte operations byte a = 10; byte b = 20; byte c = a + b; System.out.println(c); //compile error

Explanation: When calculating, first promote both a and b to int, and the result is also int, requiring int type reception (or forced type conversion)

Conclusion:

Integer lifting is considered when calculating data types less than 4 bytes

Converting int to String and vice versa

Examples:

int num = 10;String str1 = num + "";//String str2 = String.valueOf(num);String converted to int

Examples:

String str = "100";int num = Integer.parseInt(str);//int num = Integer.valueOf(str); Thank you for reading! About "JavaSE data type sample analysis" This article is shared here, I hope the above content can be of some help to everyone, so that we can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 204

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report