Skip to content

Commit

Permalink
update codes and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dunwu committed Aug 6, 2020
1 parent 2fa7bc9 commit e9d8480
Show file tree
Hide file tree
Showing 55 changed files with 3,035 additions and 8,739 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
- [Java 基础语法特性](docs/basics/java-basic-grammar.md)
- [Java 基本数据类型](docs/basics/java-data-type.md)
- [Java String 类型](docs/basics/java-string.md)
- [Java 类和对象](docs/basics/java-class.md)
- [Java 面向对象](docs/basics/java-oop.md)
- [Java 方法](docs/basics/java-method.md)
- [Java 数组](docs/basics/java-array.md)
- [Java 枚举](docs/basics/java-enum.md)
Expand Down
4 changes: 2 additions & 2 deletions codes/javacore-basics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<name>JavaCore :: Basics</name>

<properties>
<java.version>1.7</java.version>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.dunwu.javacore.datatype;

import lombok.extern.slf4j.Slf4j;

/**
* @author <a href="mailto:[email protected]">Zhang Peng</a>
* @since 2020-08-06
*/
@Slf4j
public class Integer判等 {

public static void main(String[] args) {
Integer a = 127; //Integer.valueOf(127)
Integer b = 127; //Integer.valueOf(127)
log.info("\nInteger a = 127;\nInteger b = 127;\na == b ? {}", a == b); // true

Integer c = 128; //Integer.valueOf(128)
Integer d = 128; //Integer.valueOf(128)
log.info("\nInteger c = 128;\nInteger d = 128;\nc == d ? {}", c == d); //false
//设置-XX:AutoBoxCacheMax=1000再试试

Integer e = 127; //Integer.valueOf(127)
Integer f = new Integer(127); //new instance
log.info("\nInteger e = 127;\nInteger f = new Integer(127);\ne == f ? {}", e == f); //false

Integer g = new Integer(127); //new instance
Integer h = new Integer(127); //new instance
log.info("\nInteger g = new Integer(127);\nInteger h = new Integer(127);\ng == h ? {}", g == h); //false

Integer i = 128; //unbox
int j = 128;
log.info("\nInteger i = 128;\nint j = 128;\ni == j ? {}", i == j); //true
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.github.dunwu.javacore.datatype;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

/**
* @author <a href="mailto:[email protected]">Zhang Peng</a>
* @since 2020-08-06
*/
@Slf4j
public class Lombok生成Equals的问题 {

public static void main(String[] args) {
test1();
test2();
}

public static void test1() {
Person person1 = new Person("zhuye", "001");
Person person2 = new Person("Joseph", "001");
log.info("person1.equals(person2) ? {}", person1.equals(person2));
}

public static void test2() {
Employee employee1 = new Employee("zhuye", "001", "bkjk.com");
Employee employee2 = new Employee("Joseph", "002", "bkjk.com");
log.info("employee1.equals(employee2) ? {}", employee1.equals(employee2));
}

@Data
static class Person {

@EqualsAndHashCode.Exclude
private String name;
private String identity;

public Person(String name, String identity) {
this.name = name;
this.identity = identity;
}

}

@Data
@EqualsAndHashCode(callSuper = true)
static class Employee extends Person {

private String company;

public Employee(String name, String identity, String company) {
super(name, identity);
this.company = company;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.dunwu.javacore.datatype;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* 没事别轻易用 {@link String#intern},如果要用一定要注意控制驻留的字符串的数量,并留意常量表的各项指标
*
* @author <a href="mailto:[email protected]">Zhang Peng</a>
* @since 2020-08-06
*/
public class StringIntern性能测试 {

public static void main(String[] args) {
//-XX:+PrintStringTableStatistics,可以打印出字符串常量表的统计信息
//设置 -XX:StringTableSize=10000000 后,执行速度会快很多
List<String> list = new ArrayList<>();
long begin = System.currentTimeMillis();
list = IntStream.rangeClosed(1, 10000000)
.mapToObj(i -> String.valueOf(i).intern())
.collect(Collectors.toList());
System.out.println("size:" + list.size());
System.out.println("time:" + (System.currentTimeMillis() - begin));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.dunwu.javacore.datatype;

import lombok.extern.slf4j.Slf4j;

/**
* @author <a href="mailto:[email protected]">Zhang Peng</a>
* @since 2020-08-06
*/
@Slf4j
public class String判等 {

public static void main(String[] args) {
String a = "1";
String b = "1";
log.info("\nString a = \"1\";\nString b = \"1\";\na == b ? {}", a == b); //true

String c = new String("2");
String d = new String("2");
log.info("\nString c = new String(\"2\");\nString d = new String(\"2\");\nc == d ? {}", c == d); //false

String e = new String("3").intern();
String f = new String("3").intern();
log.info("\nString e = new String(\"3\").intern();\nString f = new String(\"3\").intern();\ne == f ? {}", e == f); //true

String g = new String("4");
String h = new String("4");
log.info("\nString g = new String(\"4\");\nString h = new String(\"4\");\ng == h ? {}", g.equals(h)); //true
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.github.dunwu.javacore.datatype;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
* @author <a href="mailto:[email protected]">Zhang Peng</a>
* @since 2020-08-06
*/
@Slf4j
public class equals和CompareTo {

public static void main(String[] args) {
wrong();
right();
}

public static void wrong() {

List<Student> list = new ArrayList<>();
list.add(new Student(1, "zhang"));
list.add(new Student(2, "wang"));
Student student = new Student(2, "li");

log.info("ArrayList.indexOf");
int index1 = list.indexOf(student);
Collections.sort(list);
log.info("Collections.binarySearch");
int index2 = Collections.binarySearch(list, student);

log.info("index1 = " + index1);
log.info("index2 = " + index2);
}

public static void right() {

List<StudentRight> list = new ArrayList<>();
list.add(new StudentRight(1, "zhang"));
list.add(new StudentRight(2, "wang"));
StudentRight student = new StudentRight(2, "li");

log.info("ArrayList.indexOf");
int index1 = list.indexOf(student);
Collections.sort(list);
log.info("Collections.binarySearch");
int index2 = Collections.binarySearch(list, student);

log.info("index1 = " + index1);
log.info("index2 = " + index2);
}

@Data
@AllArgsConstructor
static class Student implements Comparable<Student> {

private int id;
private String name;

@Override
public int compareTo(Student other) {
int result = Integer.compare(other.id, id);
if (result == 0) { log.info("this {} == other {}", this, other); }
return result;
}

}

@Data
@AllArgsConstructor
static class StudentRight implements Comparable<StudentRight> {

private int id;
private String name;

@Override
public int compareTo(StudentRight other) {
return Comparator.comparing(StudentRight::getName)
.thenComparingInt(StudentRight::getId)
.compare(this, other);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author <a href="mailto:[email protected]">Zhang Peng</a>
* @since 2019-03-26
*/
public class DataTypeDemo01 {
public class 值类型使用示例 {

public static void demo1() {
// compile error
Expand Down Expand Up @@ -95,7 +95,7 @@ public static void demo9() {
public static void main(String[] args)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
for (int i = 1; i <= 10; i++) {
Method method = DataTypeDemo01.class.getMethod("demo" + i);
Method method = 值类型使用示例.class.getMethod("demo" + i);
method.invoke(null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @author <a href="mailto:[email protected]">Zhang Peng</a>
* @since 2019-03-26
*/
public class DataTypeDemo02 {
public class 包装类型使用示例 {

public static void main(String[] args) {
printByte();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @author <a href="mailto:[email protected]">Zhang Peng</a>
* @since 2019-03-10
*/
public class BoxingDemo {
public class 包装类装箱拆箱 {

public static void main(String[] args) {
Integer i1 = 10; // 自动装箱
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.github.dunwu.javacore.datatype;

import lombok.Data;

/**
* @author <a href="mailto:[email protected]">Zhang Peng</a>
* @since 2020-08-06
*/
public class 枚举判等 {

public static void main(String[] args) {
StatusEnum statusEnum = StatusEnum.DELIVERED;
OrderQuery orderQuery = new OrderQuery(StatusEnum.DELIVERED.status, StatusEnum.DELIVERED.desc);
System.out.println(statusEnum.status == orderQuery.getStatus());
}

@Data
static class OrderQuery {

private Integer status;
private String name;

public OrderQuery(Integer status, String name) {
this.status = status;
this.name = name;
}

}

enum StatusEnum {
CREATED(1000, "已创建"),
PAID(1001, "已支付"),
DELIVERED(1002, "已送到"),
FINISHED(1003, "已完成");

private final Integer status;
private final String desc;

StatusEnum(Integer status, String desc) {
this.status = status;
this.desc = desc;
}
}

}
Loading

0 comments on commit e9d8480

Please sign in to comment.