java数据类型

 数据类型分为1.基本数据类型2.引用数据类型


1.基本数据类型(四类八种)

1.1.布尔类型(boolean)

1.2.整数类型(byte,short,int,long)

1.3.字符类型(char)

1.4.浮点类型(float,double)


2.引用数据类型

2.1.数组

2.2.接口(interface)

2.3.类(class)


1.1 布尔类型

布尔类型只能取值 true 或者 false, 其他数据类型例如0或者非0不能替代true和false。

boolean a = true;
boolean b = false;
System.out.println(a);
System.out.println(b);

1.2 整数类型

byte 是做为最小的数字来处理的,因此它的值域被定义为-128~127,也就是signed byte。1 byte = 8 bit

public class Main {
    public static void main(String[] args) {
        byte b = 1;//0000 0001
        System.out.println(Arrays.toString(byteToBit(b)));
    }

    public static byte[] byteToBit(byte b) {
        byte[] array = new byte[8];
        for (int i = 0; i <= 7; i++) {
            array[7 - i] = (byte) (b & 1);
            b = (byte) (b >> 1);
        }
        return array;
    }
}

short 短整数 16 位有正负的二进制整数,最小值是 -32768(-2^15),最大值是 32767(2^15-1)

int 整数型  32 位有正负的二进制整数,最小值是 - 2,147,483,648(-2^31),最大值是 2,147,483,647(2^31 -1)

long 长整型 64 位有正负的二进制整数,最小值是 -9,223,372,036,854,775,808(-2^63),最大值是 9,223,372,036,854,775,807 (2^63 -1) 

*整数型一般默认被应用于整数值除非担心内存不够用。短整数类型的数据也可以像字节型一样用于节省空间。短整数比整数小两倍。长整型一般是在需要比整数型范围更大时应用。


类型
占用存储空间
表数范围
byte1字节-128~127
short 2字节-2^15~2^15-1
int4字节-2^31~2^31-1
long8字节-2^63~2^63-1


1.3字符类型

字符型数据是简单的 16 位 Unicode 标准下的字符。最小值是: '\u0000' (或 0),最大值是: '\uffff' (或 65,535 ),字符型数据可以用来储存任意字母。

类型占用存储空间表数范围
char2字节'\u0000' ( 0)~ '\uffff' ( 65,535 )

char 与 String 区别
1.char是基本数据类型,String是一个 类具有面向对象的特征,可以调用方法。
2.char是单引‘’ 如char c='x',String是双引“”如String name="tom";。
char c = 'c';
String name = "tom";
3.String在Java中是被定义为char数组来组织的,所以定义的String最终要被转换成char来存放

1.4浮点类型
double双精度浮点数
float单精度浮点数
float f = 1.23f;
double d = 1.23;



类型占用存储空间表数范围
float4字节-3.403E38~3.503E38
double8字节-1.798E308~1.798E308

*浮点型的数据在不声明的情况下都是double型的,如果想表示float类型,需要在数字末尾加F或者f,例如3.14f,3.14F
*浮点型的数据是不能完全精确的,所以有的时候在计算的时候可能会在小数点最后几位出现浮动,比较浮点数时a==0.1是不合适的,应该a-0.1==0

Variable.java
package cn.javase.domain;

public class Variable {
/*
            1.1.布尔类型(boolean)
            1.2.整数类型(byte,short,int,long)
            1.3.字符类型(char)
            1.4.浮点类型(float,double)
            */
    private boolean bBoolean;
    private byte bByte;
    private short  bShort;
    private int bInt;
    private long bLong;
    private float bFloat;
    private  double bDouble;
    private char bChar;
    private String bString;

    public String getbString() {
        return bString;
    }

    public void setbString(String bString) {
        this.bString = bString;
    }

    public Variable() {
        this.bBoolean = true;
        this.bByte = 1;
        this.bShort = 1;
        this.bInt = 1;
        this.bLong = 1;
        this.bFloat = 1.1f;
        this.bDouble = 1.1;
        this.bChar = '1';
        this.bString="1";
    }

    public Variable(boolean bBoolean, byte bByte, short bShort, int bInt, long bLong, float bFloat, double bDouble, char bChar, String bString) {
        this.bBoolean = bBoolean;
        this.bByte = bByte;
        this.bShort = bShort;
        this.bInt = bInt;
        this.bLong = bLong;
        this.bFloat = bFloat;
        this.bDouble = bDouble;
        this.bChar = bChar;
        this.bString = bString;
    }

    public boolean isbBoolean() {
        return bBoolean;
    }

    public void setbBoolean(boolean bBoolean) {
        this.bBoolean = bBoolean;
    }

    public byte getbByte() {
        return bByte;
    }

    public void setbByte(byte bByte) {
        this.bByte = bByte;
    }

    public short getbShort() {
        return bShort;
    }

    public void setbShort(short bShort) {
        this.bShort = bShort;
    }

    public int getbInt() {
        return bInt;
    }

    public void setbInt(int bInt) {
        this.bInt = bInt;
    }

    public long getbLong() {
        return bLong;
    }

    public void setbLong(long bLong) {
        this.bLong = bLong;
    }

    public float getbFloat() {
        return bFloat;
    }

    public void setbFloat(float bFloat) {
        this.bFloat = bFloat;
    }

    public double getbDouble() {
        return bDouble;
    }

    public void setbDouble(double bDouble) {
        this.bDouble = bDouble;
    }

    public char getbChar() {
        return bChar;
    }

    public void setbChar(char bChar) {
        this.bChar = bChar;
    }
}


Main.java
import cn.javase.domain.Variable;
import org.junit.Test;


public class Main {

    public static void main(String[] args) {
        Variable variable = new Variable();
        System.out.println(variable.isbBoolean());
        System.out.println(variable.getbByte());
        System.out.println(variable.getbShort());
        System.out.println(variable.getbInt());
        System.out.println(variable.getbLong());
        System.out.println(variable.getbFloat());
        System.out.println(variable.getbDouble());
        System.out.println(variable.getbChar());
        System.out.println(variable.getbString());
    }
@Test
    public void test(){
        System.out.println("test");
    }

@Test
    //如何将字串 String 转换成整数 int?
    //用Interger类中valueOf方法将String转Interger然后调用intValue方法获取int值
    public void stringToInt() {
        Variable variable = new Variable();
        int i = Integer.parseInt(variable.getbString());
        System.out.println(i==1);

        int j = Integer.valueOf(variable.getbString()).intValue();
        System.out.println(j==1);
}

@Test

    //如何将整数 int 转换成字串 String ?
/*1.) String s = String.valueOf(i);用String类中valueOf方法将int转String
2.) String s = Integer.toString(i);用Integer类中toString方法将int转String
3.) String s = "" + i;*/
    public  void intToString(){
        Variable variable = new Variable();
        String s = String.valueOf(variable.getbInt());
        System.out.println(s.equals("1"));

        String s2 = Integer.toString(variable.getbInt());
        System.out.println(s2.equals("1"));

        String s3 = "" + variable.getbInt();
        System.out.println(s3.equals("1"));

    }

@Test
    //Double, Float, Long 转成字串的方法大同小异.
    public void doubleToString(){
        Variable variable = new Variable();
        String s = String.valueOf(variable.getbDouble());
        System.out.println(s.equals("1.1"));

        String s2 = Double.toString(variable.getbDouble());
        System.out.println(s2.equals("1.1"));

        String s3 = "" + variable.getbDouble();
        System.out.println(s3.equals("1.1"));
    }


/*    JAVA中常用数据类型转换函数
    虽然都能在JAVA API中找到,整理一下做个备份。

    string->byte
    Byte static byte parseByte(String s)
    byte->string
    Byte static String toString(byte b)

    char->string
    Character static String toString (char c)

    string->Short
    Short static Short parseShort(String s)
    Short->String
    Short static String toString(Short s)

    String->Integer
    Integer static int parseInt(String s)
    Integer->String
    Integer static String tostring(int i)

    String->Long
    Long static long parseLong(String s)
    Long->String
    Long static String toString(Long i)

    String->Float
    Float static float parseFloat(String s)
    Float->String
    Float static String toString(float f)

    String->Double
    Double static double parseDouble(String s)
    Double->String
    Double static String toString(Double)*/

}

2.1数组
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值