Java basics (1)-2019/12/19

Java basics (1)-2019/12/19

  • How to support regular expression operations in Java

The String class in Java provides methods to support regular expression operations: matches(), replaceAll(), split(), etc. In addition, Java also provides Pattern and Matcher classes to support regular expression operations.

  • Briefly describe the purpose of regular expressions

When we are dealing with writing programs that process strings, we often need to perform certain operations on strings according to a certain pattern: query, cut, replace, etc. Regular expressions are what we use to define patterns. Powerful tool. Most languages ​​support regular expressions.

  • Compare Java and JavaScript
  • Java : Object-oriented, compiled language, strongly typed language
  • JavaScript : Based on object and event-driven, interpreted language, weakly typed language
  • Classification of programming languages
Classification standard category feature advantage Disadvantage Representative language
Program execution method Compiled language A language that requires a compiler to compile the source code into machine code
before it can be executed
High performance The program needs to be recompiled when modifying the program, and the portability is poor C、C++
Interpreted language The source code does not need to be compiled,
it is translated line by line during execution
Cross-platform (provided that the interpreter is installed), can be quickly deployed Poor performance JavaScript、Python、PHP
Mixed language Mixed language is also called semi-compiled language. The source code is first compiled into intermediate code, and the intermediate code is
executed by JIT during execution.
Strong portability Moderate performance Java 、 C #
Time to check data type Dynamically typed language The data type is determined at runtime,
no type declaration is required. The type of the variable is usually the type of the value being assigned
Python、JavaScript、PHP、swift
Statically typed language To determine the data type at compile time, you
need to declare the data type
C 、 C ++ 、 C # 、 Java
How to deal with data types Strongly typed language When a variable is defined as a certain type, its type will not change if there is no forced type conversion (display conversion). Higher operating efficiency and higher memory utilization Java 、 C # 、 Python
Weakly typed language Data types can be ignored, and a variable can be assigned values ​​of different data types.
Assign a string value to an integer variable a, then a becomes a character type.
Low operating efficiency (implicit type conversion),
low memory utilization (more memory allocation in consideration of versatility),
insufficient detection of data types
JavaScript、PHP
  • Attentions
  1. JIT: Just-In-Time Compiler, a just-in-time compiler that converts Java bytecode into machine code.
  2. V8 engine: The V8 engine is designed to improve the performance of the browser to execute JavaScript. The V8 engine converts the js code into machine code (different from the intermediate code) instead of relying on the interpreter to execute the js code.
  3. Generally, interpreted languages ​​are dynamically typed languages, and compiled languages ​​are statically typed languages.
  4. Although C and C++ can pass implicit type conversion, they are also completed by the compiler by calling the corresponding type conversion function. C and C++ are strongly typed languages.
  • How to break out of multiple nested loops in Java

First of all, the depth of the nested loop should not be too deep. If it exceeds 3 levels, consider whether you can choose other methods or refactor the code. You can set the flag outside the loop , and then modify the value of the flag to control whether to jump out of the nested loop.

  • The difference between & and &&

& (Bitwise AND), && (logical AND), it should be noted that & does not support short-circuit, && supports short-circuit. Therefore, && should be used instead of & in most logic judgments, otherwise the program will crash. Of course, you can only use & when performing bit operations.
For example: username != null && !username.equal("") If you write username != null & !username.equal(""), a null pointer exception will occur when username is null.

  • What is the difference between int and Integer

Int is a basic data type, and Integer is a reference data type, a wrapper class of int. Java has introduced a corresponding packaging type for each basic data type. Starting from Java 5, an automatic boxing/unboxing mechanism has been introduced to enable automatic conversion between the two.

  • Whether String is a basic data type

The basic data types in Java are only: byte, char, short, int, long, float, double, boolean, and other data types are reference data types.

  • String、StringBuilder与StringBuffer
Types of feature Applicable scene
String String constants cannot be modified once created.
When assigning a value to a variable of type String, a new String object is created
and the pointer points to the new String object.
So when too many temporary String objects are created, the GC of the JVM will start to work,
and the performance of the program will decrease accordingly.
Only a few string operations are involved
StringBuilder When using StringBuilder to manipulate strings, temporary String objects will not be generated, and the
thread is not safe.
Large number of operations on single-threaded character buffers
StringBuffer Similar to StringBuilder, but StringBuffer is thread-safe. A large number of operations on multi-threaded character buffers (thread safety is achieved through synchronized)

Guess you like

Origin blog.csdn.net/a16302010048/article/details/103613443