JAVA Basics (1)

● Please compare Java and JavaSciprt?

Java & JavaScript

Reference Answer:

  • Java is a fully object-oriented programming language that is half compiled and half interpreted. JavaScript is an interpreted object-based scripting language
  • Java was born by Sun and acquired by Oracle. JavaScript was developed by Netscape
  • Java source code must be compiled before execution; JavaScript is an interpreted programming language in which the source code is interpreted and executed by the browser without compilation
  • Java uses strongly typed variable checking, that is, all variables must be declared before compilation; Variables in JavaScript are weakly typed and may not even be declared before they are used, and the JavaScript interpreter checks at run time to infer their data type
  • The code format is different.

Extensions: Why is Java a half-interpreted, half-compiled programming language?

What is a compiled language and what is an interpreted language?

  • Compiled language: a runnable program that compiles all the source programs into binary code. You can then run the program directly. performSpeed is fast.High efficiencyDepending on the compiler,Less cross-platform.
  • Interpretive language: To have done the source program, translate a sentence, execute a sentence, until the end. performSlow speed.Low efficiencyDepending on the compiler,It’s a little more cross-platform.

So why is Java a compiled language?

The first argument is that Java is a compiled language, because Java programs want to run, so the first step is to compile using Javac. You can’t run without a compiled **.java** file!

So why is Java an interpreted language?

The second argument is that Java is an interpreted language. After Java is compiled, it still needs to be interpreted by the JVM. Javac compiles Java source files into **.class files and executes them by JVM** interpretation.

Taken together, Java seems to be both a compiled language and an interpreted language, and there are no authoritative books that identify Java as a type of language.

● Can you tell the difference between & and &&?

The operator

Reference Answer:

The **&** operator can be used in two ways:

  • (1)**&**;

    • & bitwise and underlying operations are binary numbers, common in JDK collection source, such as HashMap underlying positioning of the slot is achieved by the following formula:

      • (tab.length - 1) & hashThat is, the array length -1 is bitwise with the hash value!
    • & How do bitwise and operands work? Let’s take a simple example:

    3 & 2So what is that going to be?3The binary number of is represented as b00102The binary number of is b00013 & 2   
    -----
    b0011 ---> 3
    &
    b0010 ---> 2
    -----
    b0010 ---> 2so3with2As the result of the2
    Copy the code

    Conclusion: the bitwise and operator performs Boolean algebra on the corresponding bits of two integer operands, printing 1 if both bits are 1, otherwise 0~

    If you forget the basic knowledge of bit operation, to recommend a piece of my article JAVA base to improve the bit operation!

  • (2)**&&** logic and;

    • The **&&** operators are short-circuit and operations.
    • The difference between logic and short-circuit and is huge, although both require booleans on the left and right sides of the operatortrueThe value of the entire expression istrue.&&It’s called short-circuiting because theta = theta if&&The expression on the left has a value offalse, the expression on the right will be short-circuited directly without the operation ==. Many times we may need to use **&&Rather than& For example, when verifying a user login, determine that the user name is notnullInstead of an empty string, it should be written as:username ! = null &&! username.equals("")The order of the two cannot be exchanged, let alone usedThe &** operator, because if the first condition is not true, the equals comparison of strings cannot be performed at all, otherwise it will be generatedNullPointerExceptionThe exception.

● What is the difference between int and Integer?

The data type

Reference Answer:

Java is an almost pure object-oriented programming language, but it still introduces basic data types for programming convenience, but in order to be able to treat these basic data types as objects, Java introduces a corresponding wrapper type for each basic data type, int wrapper class is Integer, Automatic boxing/unboxing has been introduced since Java 5, making them interchangeable. Java provides a wrapper type for each primitive type:

  • Original type:Boolean, char, byte, short, int, long, float, double
  • Packing type:Boolean, Character, Byte, Short, Integer, Long, Float, Double

Such as:

class AutoUnboxingTest {
  public static void main(String[] args) {
    Integer a = new Integer(3);
    Integer b = 3;                // Automatically box 3 into type Integer
    int c = 3;
    System.out.println(a == b);   // false Two references do not refer to the same object
    System.out.println(a == c);   // true a is automatically unpacked into int types and then compared with C}}Copy the code

Extension: Java 8 base objects (9 if void is included)

There are nine basic data types and value ranges:

Basic types of Size (bit /bit) Number of bytes (byte) The minimum value The maximum The default value Wrapper type
boolean false true false Boolean
char 16 bits 2 bytes Unicode 0 Unicode 2^16-1 empty Character
byte 8 bits 1 byte – 2 ^ 7 2 ^ 7-1 0 Byte
short 16 bits 2 bytes – 2 ~ 15 2 ^ 15-1 0 Short
int 32 bits 4 bytes – 2 ^ 31 2 ^ 31-1 0 Integer
long 64 bits 8 bytes – 2 ^ 63 2 ^ 63-1 0 Long
float 32 bits 4 bytes 0.0 Fload
double 64 bits 8 bytes 0.0 Double
void Void

Note: The size of a Boolean is not specified in the Java Virtual Machine Specification. The size of a Boolean array is 4 bytes, and the size of a Boolean array is 1 byte, depending on whether the virtual machine is implemented according to the specification. So one byte, four bytes is possible. In addition to void, the other eight basic data types are referred to as the eight basic data types.

Left-to-right conversions in the figure are implicit conversions that do not need to be cast in code. You have to cast everything from right to left to get it compiled. Casts lose precision, such as:

// Left to right
byte i = 12;
System.out.println("byte:"+i);
short i2 = i;
System.out.println("short:"+i2);
char j = 'squared';
System.out.println("char:"+j);
int j3 = j;
System.out.println("int:"+j3);
long j4 = j;
Byte :12 short:12 char:² int:178 */

// From right to left
double i = 178.33;
System.out.println("double:"+i);
float i1 = (float) i;
System.out.println("float:"+i1);
byte i5 = (byte) i;
System.out.println("byte:"+i5);
char i6 = (char) i;
System.out.println("char:"+i6);
/* Float :178.33 float:178.33 byte:-78 char:² */
Copy the code

● In the process of web application development, we often encounter the output of a certain code character, such as ISO8859-1, etc., please tell us how to output a certain code string?

Point of investigation: Data types

Reference Answer:

 Public String translate (String str) {
    String tempStr = "";
 try {
    tempStr = new String(str.getBytes("ISO-8859-1"), "GBK");
    tempStr = tempStr.trim();
 }
 catch (Exception e) {
    System.err.println(e.getMessage());
 }
    return tempStr;
 }
Copy the code

● What are the uses of the final keyword in Java?

Reference Answer:

  • Modifier class: indicates that the class cannot be inherited;
  • Modifier: indicates that a method cannot be overridden;
  • Modifier: To indicate that a variable can be assigned only once and its value cannot be modified after the assignment (constant)

● Explain the difference between String and StringBuffer

Point of investigation: Data types

Reference Answer:

The JAVA platform provides two classes: String and StringBuffer, which store and manipulate strings, which are character data containing multiple characters. The String class provides a String with an immutable value. And this StringBuffer class provides strings that are modified. You use StringBuffer when you know the character data is going to change. Typically, you can use StringBuffers to dynamically construct character data.

Related interview questions: String/StringBuffer/StringBuilder

For the above three differences and source analysis, refer to my other article String StringBuilder StringBuffer differences and source analysis