随笔 - 81  文章 - 1033  trackbacks - 0
<2007年4月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

在浮躁的年代里,我们进取心太切,患得患失;虚荣心太强,战战兢兢。一心争强好胜,惟恐榜上无名。
I think I can fly , and flying like a bird !
程序员一名,已售出,缺货中!

我的邮件联系方式

用且仅用于MSN

博客点击率
free web counter
free web counter

常用链接

留言簿(36)

随笔档案

搜索

  •  

积分与排名

  • 积分 - 185602
  • 排名 - 308

最新评论

阅读排行榜

评论排行榜

      学Java也有些年头了,但无时无刻不深感Java基础知识、Java的OO以及Java的API也就是所谓的Core Java之重要性,从来不敢放松对它们的反复学习和提炼。并且事实证明在对它们的慢慢积累和深透的学习中悟出更多的编程之道,编程能力也在一步步见长。即使在转而学其它语言的时候应用相应套路也会事半功倍(不过抓住不同语言之区别也至关重要,要不会走很多弯路)。

      最近决定重读Core Java,并希望能找到以前遗漏的部分,并梳理出个更清晰的头绪。做到先把书由薄读厚,然后把书由厚读薄!(再说计算机的书本来就没有薄的,汗)不打算大篇幅的把内容都贴上来,只是想借助blog记录些需要时刻注意的重点供自己与大家分享。

      言归正传,抛个砖引个玉,先来一篇Java基础类型,可能你觉得这是Java里最简单的概念,其实里面的东西未必每个人都清楚,并能运用的很好。


      Java整型

int  4字节 -2147483648 ~ 2147483647   (正好超过20亿)
short 2字节 -32768 ~ 32767
long 8字节 -9223372036854775808 ~ 9223372036854774807
byte 1字节 -128 ~ 127



      浮点类型

float 4字节 大约±3.40282347E+38F (有效位数为6-7位)
double 8字节 大约±1.79769313486231570E+308 (有效位数为15位)

一些需要注意:

1if(x == Double.NaN)  // is never true

1if(Double.isNaN(x))  // check whether is "not a number"

      浮点数值不适合用于禁止出现舍入误差的金融计算中。例如System.out.println( 2.0 - 1.1);将打印0.899999999999999,而不是0.9。因为浮点数值采用二进制系统表示,而二进制无法精确表示分数1/10,就像十进制无法精确表示1/3一样。如果需要在数值计算中不含有舍入误差,就应该使用BigDecimal类。


      char类型

      在Java中,char类型用UTF-16编码描述一个代码单元。强烈建议不要在程序中使用char。


      boolean类型

      在C或C++中数值或指针可以代替boolean的值,0相当于flase,非0相当于true,而在Java中则不行,并且在编译时就会报错。
posted on 2007-04-03 01:48 cresposhi 阅读(8711) 评论(35)  编辑  收藏

FeedBack:
# re: Core Java之Java基本类型 2007-04-03 01:52 cresposhi
刚去了趟BlogJava首页,文章寥寥,又一次感到BlogJava之凄凉,哎,眼泪哗哗的,睡觉去了。。。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-03 02:37 sinoly
呵呵,小伙子还是精神很好嘛。。。。
  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-03 08:51 liigo
“强烈建议不要在程序中使用char”
很想知道理由,谢谢。  回复  更多评论
  
# re: Core Java之Java基本类型[未登录] 2007-04-03 08:54 阿蜜果
做到先把书由薄读厚,然后把书有厚读薄!
中意这句。。。。。。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-03 09:27 颖颖
这丑个头,难怪人家都不写了呢,呵呵  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-03 09:38 cresposhi
@liigo
这种惊世骇俗的话我肯定是不敢说的,只是从大师的描述中领悟到一点道道。还是把大师的话贴在这里,一切就真相大白了。

To understand the char type, you have to know about the Unicode encoding scheme. Unicode was invented to overcome the limitations of traditional character encoding schemes. Before Unicode, there were many different standards: ASCII in the United States, ISO 8859-1 for Western European languages, KOI-8 for Russian, GB18030 and BIG-5 for Chinese, and so on. This causes two problems. A particular code value corresponds to different letters in the various encoding schemes. Moreover, the encodings for languages with large character sets have variable length: some common characters are encoded as single bytes, others require two or more bytes.

Unicode was designed to solve these problems. When the unification effort started in the 1980s, a fixed 2-byte width code was more than sufficient to encode all characters used in all languages in the world, with room to spare for future expansion—or so everyone thought at the time. In 1991, Unicode 1.0 was released, using slightly less than half of the available 65,536 code values. Java was designed from the ground up to use 16-bit Unicode characters, which was a major advance over other programming languages that used 8-bit characters.

Unfortunately, over time, the inevitable happened. Unicode grew beyond 65,536 characters, primarily due to the addition of a very large set of ideographs used for Chinese, Japanese, and Korean. Now, the 16-bit char type is insufficient to describe all Unicode characters.

We need a bit of terminology to explain how this problem is resolved in Java, beginning with JDK 5.0. A code point is a code value that is associated with a character in an encoding scheme. In the Unicode standard, code points are written in hexadecimal and prefixed with U+, such as U+0041 for the code point of the letter A. Unicode has code points that are grouped into 17 code planes. The first code plane, called the basic multilingual plane, consists of the "classic" Unicode characters with code points U+0000 to U+FFFF. Sixteen additional planes, with code points U+10000 to U+10FFFF, hold the supplementary characters.

The UTF-16 encoding is a method of representing all Unicode code points in a variable length code. The characters in the basic multilingual plane are represented as 16-bit values, called code units. The supplementary characters are encoded as consecutive pairs of code units. Each of the values in such an encoding pair falls into an unused 2048-byte range of the basic multilingual plane, called the surrogates area (U+D800 to U+DBFF for the first code unit, U+DC00 to U+DFFF for the second code unit).This is rather clever, because you can immediately tell whether a code unit encodes a single character or whether it is the first or second part of a supplementary character. For example, the mathematical symbol for the set of integers has code point U+1D56B and is encoded by the two code units U+D835 and U+DD6B. (See http://en.wikipedia.org/wiki/UTF-16 for a description of the encoding algorithm.)

In Java, the char type describes a code unit in the UTF-16 encoding.

Our strong recommendation is not to use the char type in your programs unless you are actually manipulating UTF-16 code units. You are almost always better off treating strings (which we will discuss starting on page 51) as abstract data types.

  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-04 11:42 kirari_wxy
大师也经常出错的 呵呵 不过有些还是通用的 带小数计算的问题还是留给BigDecimal吧 这应该是很多公司的编码规范了  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-04 11:47 kirari_wxy
不鼓励使用char可能是因为java全部是Unicode编码 容易造成字符和字符串的问题  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-04 12:32 cresposhi
@kirari_wxy
如果需要结果一致BigInteger和BigDecimal确实更好。
至于char的问题其实是Java里char是用UTF-16的,这样就有code units 和 code points 这两个概念,上面大师已经描述过了,char表示一个code units而对于常规字符来讲一个code units就是一个code points而对于一些特殊的字符需要两个code units来表示,这个时候char就会引起很多混淆,所以最好使用Character或者一些其他的包装过的类型。
我觉得国外的大师一般不随便乱说话的,而国内的“大师”比较喜欢忽悠大家。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-04 14:02 kirari_wxy
BigInteger倒是很少用,但是BigDecimal应该是编码规范了,至少我看到的公司都是这样,也可能我看到的都是金融业的公司,对数值要求高,而char只要明白原理了应该不会错,至少我们系统没有出现这样的问题,不过确实很少用到char。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-04 14:09 cresposhi
@kirari_wxy
对,金融的肯定要BigDecimal,要不每次结果都随机那还不汗死~~~
至于char可能那些特别字符用的比较少吧,等到哪天碰到了找bug要找晕,呵呵。
JDK5.0里面就提供了很多对code points的方法支持,比如String里面Character里面都有。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-04 14:29 kirari_wxy
可惜大型系统都是jdk1.4的,至今没有用到1.5,j2ee更新慢,中间件server也更新慢,开发系统的也不敢冒险。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-04 14:49 cresposhi
@kirari_wxy
其实JEE现在不也有了J5EE,各大Application Server厂商也都在新的产品线全面拥抱了JDK5,大部分活跃的开源产品也拥抱了JDK5,并有很多应用面极广泛的开源产品的新特性都是依赖JDK5的新特性,必须要JDK5的支持,如spring、hibernate等。
不过确实非常多比较慎重的团队依然守护这JDK1.4.2,不愿意冒险。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-04 17:40 颖颖
一哈不来你们都聊起来了啊  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-04 18:59 cresposhi
@颖颖
做技术最开心的莫过于遇到一个能讨论的人,呵呵  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-05 17:32 kirari_wxy
呵呵,上班没事随便聊聊,做大型系统都是很谨慎的,在没有不能解决的问题发生的情况下不会使用新技术,也不会升级中间件系统的,甚至是jdk。不升级版本是因为出了问题谁也负不了这个责任,不使用新技术是因为使用最简单的技术才会降低开发门槛,人力资源才会充足。而且这些大型系统即使用hibernate,也是假的,最多就是一个单表查询。听说工商银行的核心系统用了spring,我没见过,其他的金融公司的核心系统,怕是没人敢用的。我们公司4000万美元的项目都不敢用。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-05 17:36 cresposhi
4KW dollar。。。我还没有机会。。。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-05 17:59 kirari_wxy
我也就04年做过一段时间,后来就换到其他项目了。这个项目到现在还没有完工,无数人做了3年多了,哎,大项目有时也蛮痛苦,还好是赚钱的,人力成本也是客户出钱,每人每月RMB12500,出差每天补助将近RMB200。我没有赶到好时候啊,呵呵。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-05 19:21 cresposhi
呵呵,那什么时候给我介绍个工作干干
在小公司呆烦了。。。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-09 14:01 kirari_wxy
有机会可以来我们公司试试  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-09 14:28 cresposhi
oh yeah,能不能把网站告诉我观摩一下  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-10 11:07 颖颖
是啊,顺便把我也招过去列  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-10 11:13 kirari_wxy
http://www.ebaotech.com/

这是我目前工作的公司 可以先看看 不过感觉最近没有前几年好了  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-10 14:01 颖颖
呵呵,那就去别的地方,哎,哪个知道哪个公司现在好啊  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-10 14:29 kirari_wxy
有机会就跳,没机会就混着,呵呵,我的目标是争取明年去法国转转。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-10 16:56 颖颖
你转的还满远列  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-10 18:05 kirari_wxy
我在武汉搞了3到4年,来上海也差不多3到4年了,应该再换一个地方看看了,呵呵,男的就应该到处转转。。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-10 19:36 cresposhi
呵呵,老人了。多给点意见啊,thanks
是应该到处转转,我就是觉得我现在眼界太有限了  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-11 09:25 颖颖
那我还在武汉待了24年了列,还老些,都冒转,  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-11 09:26 cresposhi
美女怎么能随便透漏年龄列,呵呵  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-11 09:50 kirari_wxy
要是这样说的话,我离开武汉的时候都已经不止24岁了咧,呵呵。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-11 10:56 颖颖
又不老,怕什么,呵呵那你现在。。。。那不是更老了。。  回复  更多评论
  
# re: Core Java之Java基本类型 2007-04-11 15:35 cresposhi
人家那叫成熟嘛,呵呵  回复  更多评论
  
# re: Core Java之Java基本类型 2008-11-08 13:36 drz2008mx@163.com
BigDecimal t1 = new BigDecimal(2.0),t2 = new BigDecimal(-1.1);
System.out.println(t1.add(t2).toString());

效果: 0.899999999999999911182158029987476766109466552734375

解决不了 舍入误差问题。在些情况下还是会出错!  回复  更多评论
  
# re: Core Java之Java基本类型 2008-11-20 13:39 luguo
@drz2008mx@163.com
你的构造函数用错了,用了BigDecimal(double),double本来就不是准确地。得到的BigDecimal也就不对了,做出来的结果就不对了。

BigDecimal t1 = new BigDecimal("2.0"),t2 = new BigDecimal("-1.1");
System.out.println(t1.add(t2).toString());  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: