• Java数据类型


    Java数据类型

    一、         String

     String类型的数据不可改变,而StringBuffer类型的数据可以改变。如果一个字符串需要频繁更改的话,应该使用StringBuffer,

    String类提供了以下方法来操作字符和字符串:

    ·根据索引来查找字符串指定位置的字符:public char charAt(int index)

    ·字符串变为字符数组:public char[] toCharArray()

    ·将字符数组变为字符串:

          |—将全部字符数组变为String类型:public String(char[] value)

          |—将部分字符数组变为String类型:public String(char[] value,int offset,int count)

    ·判断是否以指定字符串开头:startsWith(String pre)

    ·判断是否以指定字符串结尾:endsWith(String end)

    ·替换:replaceAll()

    ·截取:substring(startIndex),substring(startIndex,endIndex)

    ·拆分:split(regex)

    ·查找:查找字符串的位置indexOf,查找是否包含:contains

    ·去掉空格:trim

    ·取得长度:length

    ·大小写转换:toUpperCase,toLowerCase

    二、         StringBuffer

    StringBuffer使用方法:

    ·使用append(String val)增加内容

    ·使用insert(int index,String val)在指定位置插入内容

    ·使用delete(startIndex,endIndex)删除指定位置的内容

    ·使用replace(int index,int end,String str)来替换内容

    ·使用reverse来翻转内容

    三、         包装类:

    包装类是为了保证Java程序的一切皆对象的理念而产生。

    装箱操作:将数据类型变为包装类

    拆箱操作:将包装类变为基本数据类型

    四、         大数操作:

    BigInteger,BigDecimal来操作大数。前者用来表示大的整型数据,后则表示小数。它们都位于java.math包中。示例代码如下:

    import java.math.BigInteger;

    public class StringDemo {

       public static void main(String args[]){

         BigInteger big=new BigInteger("555555555555555555555555555555555555");

         BigInteger big2=big.add(new BigInteger("555555555555555555"));

         System.out.println(big2);

       }

    }

    五、Runtime类和System类

    Runtime类构造方法私有化,使用getRuntime来获得实例。使用exec启动一个程序。exec方法返回了一个Process,Process类有destroy来销毁进程。

    public class RuntimeDemo {

       public static void main(String args[]) throws Exception{

         Runtime r=Runtime.getRuntime();

         Process p=r.exec("notepad");

         Thread.sleep(10000l);

         p.destroy();

       }

    }

    在Runtime类中可以通过,maxMemory,freeMemory,totalMemory来取得内存信息。

    可以通过System类的currentTimeMillis来取得当前系统时间。System类的gc方法来回收。

    六:日期操作类:

    1. Date类:是一个系统提供的日期操作类。

    2.     Calendar类:可以将时间精确到毫秒。该类是一个抽象类,需要子类将其实例化。该类有一个唯一的子类就是GregorianCalendar

    3.     DateFormat类:该类提供了一个方法format,该类接收一个Date类数据。返回String类数据。示例代码如下:

    import java.text.DateFormat;

    import java.util.Date;

    public class DateDemo {

       /**

        * @param args

        */

       public static void main(String[] args) {

         // TODO Auto-generated method stub

         Date date=new Date();

         DateFormat df=DateFormat.getDateInstance();

         DateFormat df2=DateFormat.getDateTimeInstance();

         System.out.println(df.format(date));

         System.out.println(df2.format(date));

       }

    }

    4.     SimpleDateFormat类:使用parse来将字符串变为日期类型,使用format来格式化日期。示例代码如下所示:

    import java.text.SimpleDateFormat;

    import java.util.Date;

    public class DateDemo {

       public static void main(String[] args) throws Exception {

         String date="2011-12-5";

         SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");

         Date d=sd.parse(date);//提取指定格式的日期

         SimpleDateFormat sd2=new SimpleDateFormat("yyyy MM dd");

         System.out.println(sd2.format(d));//将日期格式化

       }

    }

    七、Math和Random类

    1.Math类用于数学计算,提供了一系列的静态方法:Math.max(1,2),Math.min(2,3),Math.round(float)四舍五入。

    2.Random类:产生随机值。位于java.util包中。

    代码示例:

    import java.util.Random;

    public class RandomDemo {

       public static void main(String args[]) throws Exception{

         Random r=new Random();

         System.out.println(r.nextInt(100));

       }

    }

  • 相关阅读:
    【从零开始学Java】第六章 运算符
    【从零开始学Java】第五章 变量和数据类型
    【从零开始学Java】第四章 常量
    【从零开始学Java】第三章 HelloWorld入门程序
    【从零开始学Java】第二章 Java语言开发环境搭建
    【从零开始学Java】第一章 开发前言
    vim配置
    神奇的洛谷运势汇总
    达哥题表
    数论总结
  • 原文地址:https://www.cnblogs.com/itfenqing/p/4429556.html
Copyright © 2020-2023  润新知