Basic knowledge of Java Java Basics

Java Basics

ffer: apply to the situation of a large number of operating under multiple threads in the character buffer.

5.2 Example

10000 to splice strings, for example, we look at three times their needs:

String str = "";
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) { str = str + i; } long endTime = System.currentTimeMillis(); long time = endTime - startTime; System.out.println("String消耗时间:" + time); StringBuilder builder = new StringBuilder(""); startTime = System.currentTimeMillis(); for (int j = 0; j < 10000; j++) { builder.append(j); } endTime = System.currentTimeMillis(); time = endTime - startTime; System.out.println("StringBuilder消耗时间:" + time); StringBuffer buffer = new StringBuffer(""); startTime = System.currentTimeMillis(); for (int k = 0; k < 10000; k++) { buffer.append(k); } endTime = System.currentTimeMillis(); time = endTime - startTime; System.out.println("StringBuffer消耗时间:" + time);

operation result:

String elapsed time: 258
the StringBuilder time-consuming: 0
StringBuffer time-consuming: 1

Also verified the above mentioned StringBuilder> StringBuffer> String.

6. boxing and unboxing

6.1 What is boxing? What is the unboxing?

Packing: automatically converts the basic data types for the packaging type.

Unpacking: automatically converts the package type of the basic data types.

Integer i = 10; // 装箱
int j = i; // 拆箱

6.2 How boxing and unboxing are implemented?

Packing process is achieved by calling the method valueOf wrapper, the unpacking process is achieved by the packaging method call xxxValue instance. (Xxx represents that the corresponding basic data types).

How to prove this conclusion does, we create a class Main, add the following code in the main method:

package com.zwwhnly.springbootdemo;

public class Main { public static void main(String[] args) { Integer i = 100; int j = i; } }

Then open cmd window, the path is switched to the Main class, Run: javac Main.java, find that the directory will generate a file Main.class with IDEA open, you will find that the compiled code is as follows:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.zwwhnly.springbootdemo; public class Main { public Main() { } public static void main(String[] var0) { Integer var1 = Integer.valueOf(100); int var2 = var1.intValue(); } }

6.3 Example

Example 1:

Double i1 = 100.0;
Double i2 = 100.0;
Double i3 = 200.0;
Double i4 = 200.0;

System.out.println(i1==i2);
System.out.println(i3==i4);

Output:

false

false

Why it returns false, we look Double.valueOf () method, you know the:

private final double value;

public Double(double value) { this.value = value; } public static Double valueOf(double d) { return new Double(d); }

Example 2:

Boolean i1 = false;
Boolean i2 = false;
Boolean i3 = true;
Boolean i4 = true;

System.out.println(i1==i2);
System.out.println(i3==i4);

Output:

true

true

Why return true, let's look Boolean.valueOf () method, you know the:

public static final Boolean TRUE = new Boolean(true); public static final Boolean FALSE = new Boolean(false); public static Boolean valueOf(boolean b) { return (b ? TRUE : FALSE); }

And the reference value is passed in the transfer 7.Java

7.1 Basic Concepts

Value is passed: passing a copy of the object, even if the copy is changed, it will not affect the source object, because the value passed when, in fact, the value of the argument to the parameter copy.

Passed by reference: the actual object is not passed, but references to the object, external changes to the referenced object will be reflected in the source object, because when passed by reference, the argument is actually a copy to the address value parameter.

Explanation: The object transfer (array, classes, interfaces) are passed by reference, the original data type (plastic, floating point, character, Boolean) transfer is passed by value.

7.2 Example

Example 1 (passed value):

package com.zwwhnly.springbootdemo;

public class ArrayListDemo { public static void main(String[] args) { int num1 = 10; int num2 = 20; swap(num1, num2); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); } public static void swap(int a, int b) { int temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b); } }

operation result:

a = 20

b = 10

num1 = 10

num2 = 20

While the swap () method of the values ​​a, b do exchange, but the primary method num1, num2 value did not change.

Example 2 (reference transmission type):

package com.zwwhnly.springbootdemo; 

public class ArrayListDemo { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; change(arr); System.out.println(arr[0]); } public static void change(int[] array) { System.out.println(array[0]); array[0] = 0; } }

operation result:

1

0

In the change () method of the first element of the array will be changed to 0, the first element in the array of primary method followed becomes zero.

Example 3 (StringBuffer type):

package com.zwwhnly.springbootdemo;

public class ArrayListDemo { public static void main(String[] args) { StringBuffer stringBuffer = new StringBuffer("博客园:周伟伟的博客"); System.out.println(stringBuffer); changeStringBuffer(stringBuffer); System.out.println(stringBuffer); } public static void changeStringBuffer(StringBuffer stringBuffer) { stringBuffer = new StringBuffer("掘金:周伟伟的博客"); stringBuffer.append(",欢迎大家关注"); } }

operation result:

Park blog: Zhou Weiwei's blog

Park blog: Zhou Weiwei's blog

You might think the 2nd output should be "Nuggets: Zhou Weiwei's blog, welcome everyone's attention," how output or the original value of it, it is because in changeStringBuffer in, and a new StringBuffer object, then the object point stringBuffer memory address has changed, so the main method of stringBuffer variable is not affected.

If the code is modified changeStringBuffer () method is:

public static void changeStringBuffer(StringBuffer stringBuffer) { stringBuffer.append(",欢迎大家关注"); }

The result becomes a run:

Park blog: Zhou Weiwei's blog

Park blog: Zhou Weiwei's blog, welcome attention

Example 4 (String Type):

package com.zwwhnly.springbootdemo;

public class ArrayListDemo { public static void main(String[] args) { String str = new String("博客园:周伟伟的博客"); System.out.println(str); changeString(str); System.out.println(str); } public static void changeString(String string) { //string = "掘金:周伟伟的博客"; string = new String("掘金:周伟伟的博客"); } }

operation result:

Park blog: Zhou Weiwei's blog

Park blog: Zhou Weiwei's blog

Whether used in changeString () method string = "掘金:周伟伟的博客";is string = new String("掘金:周伟伟的博客");the main method str variable will not be affected, also verified after the String created can not be changed.

Example 5 (custom type):

package com.zwwhnly.springbootdemo;

public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Person(String name) { this.name = name; } }
package com.zwwhnly.springbootdemo;

public class ArrayListDemo { public static void main(String[] args) { Person person = new Person("zhangsan"); System.out.println(person.getName()); changePerson(person); System.out.println(person.getName()); } public static void changePerson(Person p) { Person person = new Person("lisi"); p = person; } }

operation result:

zhangsan

zhangsan

Modified changePerson () method code is:

public static void changePerson(Person p) { p.setName("lisi"); }

The operating results as follows:

zhangsan

lysis

8. Reference Links

About == and equals the difference between contact and interview so you can answer

No difference in Java == and equals () method

Differences in Java String, StringBuilder, StringBuffer three

Java in-depth analysis of boxing and unboxing

Integer, new Integer () int comparison face questions and

Int java face questions difference and sum of Integer

Java by far the most common interview questions summary - first week

Guess you like

Origin www.cnblogs.com/Leo_wl/p/10988547.html