Java Xiaobai introduction must learn! Notes on the most complete data types and operators, with examples

Ink rain and clouds 2021-05-04 09:09:46
java xiaobai introduction learn notes

This article is dedicated to Java The small white 、 Or want to get started Java My classmates and I'm learning Java It's arranged by my classmates , I hope it can help you . This content mainly explains Java The use of data types and operators, etc , Every knowledge point has examples .

Java Xiaobai must learn at the beginning ! Most complete notes on data types and operators , Examples are attached

One . Integer variables

1. The basic format

int Variable name = Initial value ;

Code example :

public class CSDN { public static void main(String[] args) { int a=10; System.out.println(a); }} Medium int yes 4 Bytes . in int Maximum and minimum of

If the number is greater than 0, The highest order is 0, If the number is less than 0, The highest order is 1. When judging the range of values , Regardless of the negative number, it is stored in the form of complement , Because the complement and the original code are one-to-one correspondence . When the highest is 1, Negative number , There are at most 31 individual 1, And this 31 individual 1 The sum of the added value of is 2 Of 31 Power -1, And the highest negative number is 1, This bit still satisfies the condition of negative number when it is carried again , The highest bit can be regarded as a numerical bit , So the minimum value is -2 Of 31 Power , The maximum integer must have a value of 1, And the original code equals the complement , This limits the maximum number of integers 31 Bit 1 Add up , The sum is 2^31-1.

 public class CSDN{ public static void main(String[] args) { int a = 10; System.out.println(Integer.MAX.VALUE); System.out.println(Integer.MIN.VALUE); } }

4. If a local variable , You can't use it without initialization .

Identifiers can contain : Numbers 、 Letter 、 Underscores , But you can't start with numbers , Underline is not recommended .

Two . Long integer variables

Define a long integer , The number must be followed by L

 public static void main(String[] args) { Long a = 10L; System.out.println(a); }

java And c Different language , There is no such thing as no sign ,Long Yes 8 Bytes ,64 A bit , The first is the sign bit .

If you want to print Chinese characters, use the plus sign .

public static void main(String[] args) { Long a = 10L; System.out.println(a); System.out.println("a The value of is :"+a);// Printed Chinese characters are enclosed in quotation marks and then spliced with a plus sign 

Any type of data and string to splice , The result is a string .

3、 ... and . Double precision floating point type

 public static void main(String[] args) { // Double precision floating point double d = 12.5; System.out.println(d); }

Decimal is double by default .

public static void main(String[] args) {int a = 1;int b = 2;System.out.println(a / b);}// The output is 0

because a and b It's all integers , So it's also integer type , Can't save decimals .

public static void main(String[] args) {,double num = 1.1;System.out.println(num * num)// Execution results =1.2100000000000002}

Any decimal has precision , There is no exact value , There's only one scope , To study this problem, we need to know how floating-point numbers are stored in memory .

In general , Decimal encountered , It is recommended to use double.

Four . Single precision floating point type

float yes 4 Bytes , Can't take double The type is placed in float In type , Compile errors , You need to add a... After the decimal f, Embodies the Java The safety of the .

public static void main(String[] args) { float f = 12.3; System.out.println(f); }

5、 ... and . Character data type

stay java in char Two bytes , stay c It's a byte in a language .

public static void main(String[] args) { char ch = 'a'; System.out.println(ch); char ch2 = 97; System.out.println(ch2); }

stay java Follow... In unicode Character sets are similar to c In language asii value , but unicode There's a wider range of characters .

6、 ... and . Byte type

matters needing attention :

Byte types also represent integers . It takes only one byte , The scope is small ( -128 -> +127 )

Byte type and character type are not related to each other .

 public static void main(String[] args) { byte b = 12; byte c = 21; System.out.println(b+" "+c); }

Each data type cannot be assigned beyond its range , Otherwise, an error will be reported .

7、 ... and . Short

short It's two bytes , The value range is -32768 To 32767.

The scope of this expression is small , It is generally not recommended to use .

 public static void main(String[] args) { short sh = 12; System.out.println(sh); }

8、 ... and . Boolean type

1. stay java in , Boolean types have no explicit size .

2. stay java in , Boolean type has only two values ,true and false.

3. stay java in , It doesn't matter 0 It's fake , Not 0 Is the true .

public static void main(String[] args) { boolean flag = true; System.out.println(flg); }

summary :

java in 8 The two basic data types are as follows :

1 2 2 4 8 4 8

byte char short int long float double boolean

Byte Character Short Integer Long Float Double Boolean

String type variables

String type uses String Define

matters needing attention :

Java Use Double quotes + Several characters Represents the literal value of a string .

Different from the type above , String Not a basic type , It's the type of reference ( I'll focus on it later ).

Some specific characters in a string that are not easy to express directly need to be escaped .

A string of + operation , Represents string splicing :

String a = "hello"; String b = "world"; String c = a + b; System.out.println(c);

The above code description , When one + When there is a string in an expression , Is to perform string splicing behavior .

So we can use it very easily System.out.println Print multiple strings or numbers at the same time .

Understanding numerical improvement

int and long Mixed operations

int a = 10; long b = 20; int c = a + b; // Compilation error , Prompt will long Turn into int Will lose precision long d = a + b; // Compile and pass 

Conclusion : When int and long When you're mixing , int Will be promoted to long, The result is still long type , Need to use long Type of variable to

Receive results . If you must int To receive results , You need to use cast .

byte and byte Arithmetic

byte a = 10; byte b = 20; byte c = a + b; System.out.println(c); // Compiler error error : Incompatible types : from int The switch to byte There may be losses byte c = a + b;

Conclusion : byte and byte It's all the same type , But there is a compilation error . as a result of , although a and b All are byte, But calculation a + b Will be will be a and b All promoted to int, recompute , The result is also int, This is a gift to c, There will be these mistakes .

Because of the computer's CPU Usually according to 4 Read and write data from memory in bytes . For the convenience of hardware implementation , Such as byte and short This is lower than 4 The type of bytes , Will be promoted to int, And then participate in the calculation .

Summary of type promotion :

Different types of data mixing operations , The small ones will be promoted to the large ones .

about short, byte This ratio 4 A small byte type , Will be promoted to 4 Bytes of int , Re operation .

int and String Mutual conversion between

int Turn into String

int num = 10; // Method 1 String str1 = num + ""; // Method 2 String str2 = String.valueOf(num);

String Turn into int

String str = "100"; int num = Integer.parseInt(str);


1. Four basic operators + - * / %

The rules are simpler , It's worth noting that division

a) int / int The results are int, Need to use double To calculate

b) 0 Not as a divisor

2. The return types of relational operators are boolean type .

3. Shift Operators

Move left <<: I don't want the leftmost one , On the far right 0.

Move right >>: I don't want the rightmost position , The leftmost sign bit ( Positive complement 0, Negative numbers make up 1)

Be careful :

1. Move left 1 position , Equivalent to the original number * 2. Move left N position , Equivalent to the original number * 2 Of N Power .

2. Move right 1 position , Equivalent to the original number / 2. Move right N position , Equivalent to the original number / 2 Of N Power .

3. Because the efficiency of computer calculation is higher than that of multiplication and division , When a code just multiplies and divides 2 Of N We can use shift operation instead of .

4. It doesn't make sense to move negative digits or shift too many digits .

summary : Is it right after reading this article Java Data types and operators have a detailed understanding and understanding , It's not easy to create , Brush to the partner , Please support it , Thank you very much .

本文为[Ink rain and clouds]所创,转载请带上原文链接,感谢

  1. Error in installing qtsdk in centos7.6: you need a C + + compiler
  2. Java集合(复习整理)
  3. Java collection (review)
  4. Hibernate的fetch问题,都琢磨一晚上了
  5. Hibernate's fetch problem has been pondering all night
  6. Kafka原理分析之基础篇
  7. The basis of Kafka principle analysis
  8. 请bang老师帮我看下这样说面向服务和面向对象的关系是否正确,谢谢
  9. struts ActionForm设计的缺陷
  10. 如何从weblogic集群环境中获得一个唯一对象
  11. 使用Redis缓存和Spring AOP使Spring Boot应用更健壮?
  12. Please help me see if the relationship between service-oriented and object-oriented is correct. Thank you
  13. Defects of struts ActionForm design
  14. How to get a unique object from Weblogic cluster environment
  15. Using redis cache and spring AOP to make spring boot application more robust?
  16. Kafka原理分析之基础篇
  17. The basis of Kafka principle analysis
  18. Java应用服务器死了
  19. tomcat session出现问题
  20. java+互联网架构人才
  21. UML和Java的阻抗
  22. The Java application server is dead
  23. There is a problem with Tomcat session
  24. Java + Internet architecture talents
  25. UML and Java
  26. 深入理解Linux内核之内核线程(上)
  27. 如何看待明尼苏达大学因插入实验性漏洞,被禁止贡献 Linux 内核代码?
  28. 梁金荣:使用eBPF追踪LINUX内核
  29. Docker study notes (2)
  30. Deep understanding of kernel threads in Linux kernel (Part one)
  31. How do you think the University of Minnesota is forbidden to contribute Linux kernel code because of the experimental loophole?
  32. Liang Jinrong: tracking Linux kernel with ebpf
  33. 2021Java最新学习路线图新鲜出炉
  34. New learning roadmap for 2021 Java
  35. Spring MVC 的Controller 结构问题
  36. 【烂KDE】Linux登录时用户密码正确,却登录失败
  37. Controller structure of spring MVC
  38. [KDE] when Linux logs in, the user password is correct, but the login fails
  39. HttpSession对象与Cooike的关系 以及 Cookie对象构造函数问题
  40. gRPC-Web:替代REST的gRPC的Javascript库包
  41. java call dll
  42. 用Java构建反应式REST API - Kalpa Senanayake
  43. The relationship between httpsession object and cooike and the construction of cookie object
  44. Grpc Web: a JavaScript library package to replace rest grpc
  45. java call dll
  46. Building reactive rest API with Java - kalpa Senanayake
  47. Five wonders. Feeling of visiting villa in spring
  48. Spring will be far away
  49. 最全面的SpringBoot配置文件详解
  50. 最全面的SpringBoot配置文件详解
  51. 微服务开发神器之JRebel 插件破解和实现本地及远程热部署教程
  52. 漫画 | 你还记得原生的JDBC怎么连接数据库吗?
  53. Java流及流操作示例
  54. The most comprehensive spring boot configuration file
  55. The most comprehensive spring boot configuration file
  56. Cracking and implementing jrebel plug-in of microservice development artifact
  57. Comic | do you remember how the native JDBC connects to the database?
  58. Java stream and stream operation example
  59. ftp连接windows与linux
  60. 装饰器设计模式 - Gene Zeiniss