快乐崇拜的技术博客

聪明出于勤奋,天才在于积累


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

  • Sitemap

  • 公益404

  • 搜索
快乐崇拜的技术博客

hexo+github博客环境搭建

发表于 2016-11-22 | 分类于 hexo |

常用命令

hexo new "postName" #新建文章
hexo new page "pageName" #新建页面
hexo generate #生成静态页面至public目录
hexo server #开启预览访问端口(默认端口4000,'ctrl + c'关闭server)
hexo deploy #将.deploy目录部署到GitHub
hexo help  # 查看帮助
hexo version  #查看Hexo的版本

命令简写为:

hexo deploy -g  #生成加部署
hexo server -g  #生成加预览
hexo n == hexo new
hexo g == hexo generate
hexo s == hexo server
hexo d == hexo deploy

添加百度站点注意事项

  1. 安装插件
    1
    2
    npm install hexo-generator-sitemap --save
    npm install hexo-generator-baidu-sitemap --save

然后重启服务,访问http://localhost:4000/baidusitemap.xml即可看到生产的站点地图

我这里没有生成sitemap.xml只生成了baidusitemap.xml。不过没有影响

  1. 进入http://zhanzhang.baidu.com/添加站点以及配置网页抓取

    这里推荐自动推送和sitemap结合使用。next主题可以配置一下自动推送baidu_push: true

检查百度站点是否成功

我修改的几个地方:

百度站点地图生产修改

  • 由于生成的baidusitemap.xml文件如下:
    1
    <loc>ttp://liubenlong.github.io/2016/11/22/hexo+github%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/</loc>

所以修改node_modules\hexo-generator-baidu-sitemap\baidusitemap.ejs文件,在前面加个h

1
<loc><%- encodeURI('h' + url + post.path) %></loc>

打赏样式

默认显示二维码 themes\next\layout\_macro\reward.swig

分享样式

太小了,调大点 themes\next\layout\_partials\share\jiathis.swig

注意 往github上发布之前,要先安装插件 npm install hexo-deployer-git --save

google SEO优化

在这里录入url以后,会让搜索结果排在第一位,只不过麻烦点
Hexo Seo优化让你的博客在google搜索排名第一

参考Hexo Seo优化让你的博客在google搜索排名第一

参考资料

  • Hexo搭建Github静态博客
  • next主题官网
  • 升级hexo碰到“Deployer not found”问题及解决
快乐崇拜的技术博客

springboot由浅入深(一)helloWord

发表于 2016-11-22 | 分类于 springboot |
快乐崇拜的技术博客

关于Integer数值比较的问题以及不可变对象

发表于 2016-08-10 | 分类于 java |

前言

写这篇文章是因为在之前的项目中通过findbugs进行代码优化,爆出的问题。其实我们的代码中暗藏危机,只是没有暴露出来而已

我这里使用jdk7

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public static void main(String[] args) {
Integer a = 10;
Integer b = 10;
System.out.println("a == b " + (a == b));
System.out.println("a.equals(b) " + (a.equals(b)));
System.out.println("a.intValue() == b.intValue() " + (a.intValue() == b.intValue()));
System.out.println("a.compareTo(b) " + (a.compareTo(b)));
System.out.println("---------");
a = new Integer(10);
b = new Integer(10);
System.out.println("a == b " + (a == b));
System.out.println("a.equals(b) " + (a.equals(b)));
System.out.println("a.intValue() == b.intValue() " + (a.intValue() == b.intValue()));
System.out.println("a.compareTo(b) " + (a.compareTo(b)));
System.out.println("---------");
a = 189;
b = 189;
System.out.println("a == b " + (a == b));
System.out.println("a.equals(b) " + (a.equals(b)));
System.out.println("a.intValue() == b.intValue() " + (a.intValue() == b.intValue()));
System.out.println("a.compareTo(b) " + (a.compareTo(b)));
System.out.println("---------");
int m = 189;
int n = 189;
System.out.println(m == n);
}

运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
a == b true
a.equals(b) true
a.intValue() == b.intValue() true
a.compareTo(b) 0
---------
a == b false
a.equals(b) true
a.intValue() == b.intValue() true
a.compareTo(b) 0
---------
a == b false
a.equals(b) true
a.intValue() == b.intValue() true
a.compareTo(b) 0
---------
true

奇不奇怪,为什么会酱紫呢?

原因分析

我们都知道,在java中==是比较的两个对象所指向的内存地址是否相等,而equals方法比较的是值。如果按照这个理论,上面的结果中可以看出Integer a = 10;Integer b = 10;这两个对象内存地址是同一个,也就是a和b两个引用指向同一个对象。而Integer a = 189;Integer b = 189;中,a和b两个引用指向了两个不同的对象。真的是这样吗?为什么呢?

我们查看Integer的源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the -XX:AutoBoxCacheMax=<size> option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
/**
* The value of the {@code Integer}.
*
* @serial
*/
private final int value;
/**
* Constructs a newly allocated {@code Integer} object that
* represents the specified {@code int} value.
*
* @param value the value to be represented by the
* {@code Integer} object.
*/
public Integer(int value) {
this.value = value;
}

通过这段源码可知:

  • Integer是不可变对象,因为里面的value是final的private final int value;
  • -128到127之间的数据放到了IntegerCache中,IntegerCache是static的,因此将会放到常量池中作为缓存使用

因此可知,Integer a = 10;Integer b = 10;这两个对象其实是从IntegerCache缓存中取的,是同一个对象,地址肯定是相同的。而Integer a = 189;Integer b = 189;是创建的两个新的对象,因此地址肯定不同啦

结论

永远不要用==比较integer对象,以避免一些偶发不可预测的错误。

其他与此相关的知识点

Object与Integer比较

1
2
3
4
5
6
7
Object o1 = 10;
Integer o2 = 10;
System.out.println(o1 == o2);
Object o3 = 200;
Integer o4 = 200;
System.out.println(o3 == o4);

结果:

1
2
true
false

原因与上面的一样。

jdk8中上面代码编译可是不通过的吆

锁相关

我们都知道用于加锁的对象必须是不可变对象, 永远不要再不可变对象上加锁

因此永远不要再integer对象上加锁,因为其实不可变对象private final int value;。当integer重新赋值或者进行了计算以后,得到的值是新创建的对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Stu{
private int a;
public int getA() { return a; }
public void setA(int a) {
this.a = a;
}
}
public class Test {
public static void main(String[] args) {
Stu stu = new Stu();
stu.setA(1);
increase(stu);
increase(stu);
System.out.println(stu.getA());
System.out.println("---");
Integer a = 1;
increase(a);
increase(a);
System.out.println(a);
}
//我们知道java方法传递对象是传引用的
public static void increase(Stu stu){
stu.setA(stu.getA() + 1);
System.out.println(stu.getA());
}
public static void increase(Integer integer){
integer = integer + 1;
System.out.println(integer);//这个integer其实是新创建的一个对象,而不是参数传入进来的对象
}
}

结果

1
2
3
4
5
6
7
2
3
3
---
2
2
1

12
liubenlong

liubenlong

聪明出于勤奋,天才在于积累

13 日志
7 分类
15 标签
RSS
© 2015 - 2016 liubenlong
由 Hexo 强力驱动
主题 - NexT.Pisces