zoukankan      html  css  js  c++  java
  • Java中long和Long有什么区别 (转载)

    “Long is a class. long is a primitive. That means Long can be null, where long can't. Long can go anywhere that takes an Object, long can't (since it isn't a class it doesn't derive from Object).

    Java will usually translate a Long into a long automatically (and vice versa), but won't for nulls (since a long can't be a long), and you need to use the Long version when you need to pass a class (such as in a generic declaration).

    Java的数据类型分两种:
    1.基本类型:long,int,byte,float,double,char
    2. 对象类型(类): Long,Integer,Byte,Float,Double,Char,String,其它一切java提供的,或者你自己创建的类。

    其中Long又叫 long的包装类。而Byte和Float也类似,一般包装类的名字首写是数值名的大写开头。

    什么叫包装类?
    在java中有时候的运算必须是两个类对象之间进行的,不充许对象与数字之间进行运算。所以需要有一个对象,这个对象把数字进行了一下包装,这样这个对象就可以和另一个对象进行运算了。

    比如我们也可以定义一个类:

    1 class Long {
    2     int i=0;
    3     public Long (int i){
    4     this.i=i;
    5     }
    6 }

     这样这个Long就是一个包装类,他包装了一个整数值,然后可以在里面写一些运算符重载的方法使它支持某些运算。这个时候可以赋值:
    Long it=new Long(10);
    现在变量it就是一个对象,不是一个数字。

    Long 是长整型,在怎么长本身也是整型,12.10的整形部分是12,当然结果是12,

    byte: 八位整数 -128——127,可用来节省内存的使用。
    short: 16位整数 -32768——32,767,也比较省内存。
    int: 32位整数 -2,147,483,648——2,147,483,647,一般来说整数都够用了
    long: 64位整数 -9,223,372,036,854,775,808—— 9,223,372,036,854,775,807,一般不需要用
    float: 32位浮点,如果浮点需要节省内存用这个。
    Double: 64位浮点,一般非整数浮点可用这个。

    但是要记住float和double都不是精确的,如果要储存钱一类的必须精确的,用java.math.BigDecimal

  • 相关阅读:
    容斥原理算法总结(bzoj 2986 2839)
    网络流系列算法总结(bzoj 3438 1061)
    bzoj 2746: [HEOI2012]旅行问题 AC自动机fail树
    bzoj 3283: 运算器 扩展Baby Step Giant Step && 快速阶乘
    计算几何考场绘图技巧
    bzoj 1845: [Cqoi2005] 三角形面积并 扫描线
    bzoj 3784: 树上的路径 堆维护第k大
    BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛
    BZOJ 1112: [POI2008]砖块Klo
    BZOJ 1003: [ZJOI2006]物流运输trans DP+最短路
  • 原文地址:https://www.cnblogs.com/springfor/p/4231442.html
Copyright © 2011-2022 走看看