java basic data type_Java-basic-4-数据类型

Number类

cf79cd77d5d53fb6003e6829864a61f1.png

装箱:将内置数据类型作为包装类对象使用;拆箱:相反

public class test{

public static void main(String args[]) {

// box int into Integer Obeject

Integer x = 5;

// unbox Integer Object to int

x = x + 10;

System.out.println(x);

}

}

// functions:

// convert to xxx type and return value

xxxValue()

compareTo() equals()

// 返回Integer对象指定的内置数据类型

valueOf()

toString()

// 将字符串解析为int型

parseInt()

// 对整型变量向左取整,返回类型为double

ceil()

// 对整型变量向右取整,返回类型为double

floor()

// 返回一个最接近int、long型的值

round()

min() max() exp() log() abs() pow() sqrt() sin() cos() tan() asin() acos() atan() atan2() toDegrees() toRadians()

// 返回一个随机数

random()

Character类

Character ch = 'a';

// functions:

isLetter()

isDigit()

isWhitespace()

isUpperCase()

isLowerCase()

toUpperCase()

toLowerCase()

toString()

String类

一旦创建,值就无法改变。

public class test{

public static void main(String args[]) {

String str1 = "test";

char[] temp = {'t','e','s','t','2'};

// use char[] for initialization

String str2 = new String(temp);

System.out.println("the length of str2: " + str2.length());

// formated output. or use printf

String fs;

fs = String.format("The value of the float variable is " +

"%f, while the value of the integer " +

"variable is %d, and the string " +

"is %s", 0.7f, 7, str2);

System.out.println(fs);

}

}

StringBuffer类和StringBuilder类

由于StringBuilder相较于StringBuffer有速度优势,所以多数情况下建议使用StringBuilder类。然而在应用程序要求线程安全的情况下,则必须使用StringBuffer类。

public StringBuffer append(String s)

//将指定的字符串追加到此字符序列。

public StringBuffer reverse()

// 将此字符序列用其反转形式取代。

public delete(int start, int end)

// 移除此序列的子字符串中的字符。

public insert(int offset, int i)

//将 int 参数的字符串表示形式插入此序列中。

replace(int start, int end, String str)

//使用给定 String 中的字符替换此序列的子字符串中的字符。

Array类

// for functions used below

import java.util.Arrays;

public class test{

public static void main(String args[]) {

int[] arr = {1, 9, 3, 6, 4};

// sort by increasing order

Arrays.sort(arr);

// return index

// not find, return -1*insertIndex-1. so it's -5

System.out.println(Arrays.binarySearch(arr, 8));

int[] arr2 = {0, 4};

// arr2 now is {6, 6}。but arr2 can't be blank before fill

Arrays.fill(arr2, 6);

for(int num:arr2)

System.out.println("* " + num);

// compare two arrays

System.out.println(Arrays.equals(arr, arr2));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值