zoukankan      html  css  js  c++  java
  • javase基础复习攻略《六》

      学习JAVA的同学都知道,sun为我们封装了很多常用类,本篇就为大家总结一下我们经常使用的类。上一篇博客一位朋友留言问我String是不是引用数据类型?我通过查找资料发现String属于应用数据类型,现在就让我们首先了解一下String类吧。

     1、String类

      java.long.String类代表不可变的字符序列。String类举例:

    public class Str {
        /**
         * @param String类举例
         */
        public static void main(String[] args) {
            String a1 = "abc";
            String a2 = "abc";
            String a3 = "abcd";
            System.out.println(a1==a2);//true
            System.out.println(a1==a3);//false
            System.out.println();
            
            String b1 = new String("abc");
            String b2 = new String("abc");
            String b3 = new String("abcd");
            System.out.println(b1==b2);//false
            System.out.println(b1==b3);//false
            System.out.println(b1.equals(b2));//true
            System.out.println(b1.equals(b3));//false
            System.out.println();
            
            char [] c = {'a','b','c','d'};
            String c1 = new String(c);
            String c2 = new String(c,0,3);//c:字符数组;0:数组中的第一个元素;3:c2的长度
            System.out.println(c1);//abcd
            System.out.println(c2);//abc
        }
    }

     2、String类中的常用方法:

      public char charAt(int index):返回字符串中的第index个字符

      public int length(): 返回字符串的长度

      public int indexOf(String str):返回字符串中第一个出现str的位置

      public int indexOf(String str, int fromIndex):返回字符串中从fromIndex后第一个出现str的位置

      public boolean equalsIgnoreCase(String another):比较字符串与another是否一样(忽略大小写)

      public String replace(char lastChar, char newChar):在字符串中用newChar替换lastChar字符

      public boollean startsWith(String str):判断字符串是否以str开始

      public boollean endsWith(String str):判断字符串是否以str结尾

    public class Test {
        public static void main(String [] args){
            String a1 = "Sun JAVA";
            String a2 = "sun java";
            System.out.println("a1的长度:"+a1.length());
            System.out.println("a1中第五个字符是:"+a1.charAt(5));
            System.out.println("a1中第一个出现JAVA的位置:"+a1.indexOf("JAVA"));
            System.out.println(a1.equals(a2));
            System.out.println(a1.equalsIgnoreCase(a2));
            System.out.println(a1.startsWith("Sun"));//判断是否以指定前缀开始
            System.out.println(a1.endsWith("JAVA"));//判断是否以指定后缀结尾
            
            String a3 = "I Love Play Computer Game!";
            String a4 = a3.replace(' ', '-');
            System.out.println("a3="+a3);
            System.out.println("a4="+a4);
        }
    }

     3、基本数据类型向字符串转换:

      静态方法public String valueOf(...),可将基本数据类型转换为字符串。实例:

         int b1 = 1234;
            double b2 = 12.13;
            boolean b3 = true;
            //类型转换
            String b4 = String.valueOf(b1);
            String b5 = String.valueOf(b2);
            String b6 = String.valueOf(b3);

     4、StringBuffer类:

      java.long.StringBuffer代表可变的字符序列,实例代码:

      

     5、Math类:

      java.long.Math提供了一系列静态方法用于科学计算,其方法的参数和返回值类型一般为double型。

      abs:绝对值

      sqrt:平方根

      pow(double a, double b):a的b次幂

      log:自然对数

      max(double a, double b):a、b中的较大值

      min(double a, double b):a、b中的较小值

      random():返回0.0到1.0的随机数

      Long round(double a):double型的数据a转换为Long型(四舍五入)

      toDegrees(double angrad):弧度->角度

      toRadians(double angdeg):角度->弧度

    public class Text {
        /**
         * @param Math类
         */
        public static void main(String[] args) {
            double a1 = Math.random();
            double a2 = Math.random();
            System.out.println(Math.sqrt(a1*a1+a2*a2));
            System.out.println(Math.pow(a1, 8));
            System.out.println(Math.round(a1));
            System.out.println(Math.log(Math.pow(Math.E,15)));
        }
    }

     6、File类:

      java.io.File类代表系统文件名(路径和文件名)

    public class file {
        /**
         * @param File类
         */
        public static void main(String[] args) {
            String separator = File.separator;
            String fileName = "myfile.txt";//文件名
            
            String directory = "mydir1"+separator+"mydir2";//文件路径
            //String directory = "mydir1/mydir2";
            //String directory = "mydir1\mydir2";
            
            File f = new File(directory, fileName);
            if(f.exists()){//判断文件是否存在
                System.out.println("文件名:"+f.getName());
                System.out.println("文件大小:"+f.length());
            }else{
                f.getParentFile().mkdirs();//创建文件路径
                try {
                    f.createNewFile();//创建文件
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

      对于JAVA中的常用类,就总结到这里,大家可以多多查阅JAVA的帮助文档,以便更好的理解JAVA中类的使用。

  • 相关阅读:
    模式识别之检索---Bag of visual word(词袋模型)
    模式识别之knn---KNN(k-nearest neighbor algorithm)--从原理到实现
    目标跟踪之meanshift---meanshift2
    图像处理之增强---图像模糊检测
    模式识别之非参数估计---非参估计中的核函数
    haproxy文章
    nginx 有关防盗链的设置
    haproxy 关闭ssl3
    navicat 在写存储过程的时候总是说语法错误
    开源的报表系统easyreport的部署
  • 原文地址:https://www.cnblogs.com/AndroidJotting/p/4336916.html
Copyright © 2011-2022 走看看