java的基本语法(三)

一些java里面的常用类。 

 

1-1.【字符串相关类 String 】java.lang.String类代表【不可变】的字符序列

String s1 = "hello";
String s2 = "hello";     栈空间指向data segment
System.out.println(s1 == s2);   //true


s1 = new String("hello");
s2 = new String("hello");
System.out.println(s1 == s2);   //false   new出来的是引用对象,在内存中永远不相同,不是一个对象
System.out.println(s1.equals(s2)); //true   (equals是object)

                    [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 oldChar, char newChar) 在字符串中用newChar字符替换oldChar字符
public boolean startsWith(String prefix)  判断字符串是否以prefix字符串开头
public boolean endsWith(String suffix)    判断字符串是否以suffix字符串结尾
public String toUpperCase()    返回一个字符串为该字符串的大写形式
public String toLowerCase()    返回一个字符串为该字符串的小写形式
public String substring(int beginIndex)  返回该字符串从beginIndex开始到结尾的子字符串
public String substring(int beginIndex,int endIndex) 返回该字符串从beginIndex开始到endIndex结尾的子字符串
public String trim() 返回将该字符串去掉开通和结尾空格后的字符串

   (1)静态重载方法:  public static String valueOf(...) 可以将基本类型数据转换为字符串
 例如:
 public static String valueOf(double d)
 public static String valueOf(int i)

   (2)方法:public String[] split(String regex) 可以将一个字符串按照指定的分割符分割,返回分割后的字符串数组
 例如:
 String s = "Mary,F,1976";
 String[] sPlit = s.split(","); //用逗号分割成3个数组,sPlit.length=3

1-2.【字符串相关类 StringBuffer】   java.lang.StringBuffer 代表【可变】的字符序列
 StringBuffer 类的常见构造方法:
   StringBuffer()
    创建一个不包含字符序列的“空”的StringBuffer对象
   StringBuffer(String str)
   创建一个StringBuffer对象,包含于String对象str相同的字符序列

   (1)重载方法 public StringBuffer append(...)  可以为该StringBuffer 对象添加字符序列,返回添加后的该StringBuffer对象引用,
        例如:
        public StringBuffer append(String str)
        public StringBuffer append(StringBuffer sbuf)
 public StringBuffer append(char[] str)
 public StringBuffer append(char[] str,int offset,int len) 
 public StringBuffer append(double d)
 public StringBuffer append(object obj)
   (2) 重载方法 public StringBuffer insert(...)   可以为该StringBuffer 对象在指定位置插入字符序列,
返回修改后的该StringBuffer对象引用.
 例如:
 public StringBuffer insert(int offset,String str)
 public StringBuffer insert(int offset,double d)
   (3) 方法 public StringBuffer delete(int start,int end)  可以删除从start开始到end-1 为止的一段
字符序列,返回修改后的该StringBuffer对象引用.
   (4) 方法 public StringBuffer reverse() 用于将字符序列逆序,返回修改后的该StringBuffer对象引用。

2.【基本数据类型包装类】   int类型是放在栈空间的,Integer是作为对象放在堆heap空间的

 原始类型           封装类  
  boolean        java.lang.Boolean  
  char           java.lang.Character  
  byte           java.lang.Byte  
  short          java.lang.Short  
  int            java.lang.Integer  
  long           java.lang.Long  
  float          java.lang.Float  
  double         java.lang.Double  

下面以java.lang.Integer为例
   public static final int MAX_VALUE  最大的int整型
   public static final int MIN_VALUE  最小的int整型
   public long   longValue()  返回封装数据的long型值
   public int intValue()      返回封装数据的int型值
   public static int parseInt(String s) throws  NumberFormatException  将字符串解析成int型数据,返回该数据
   public static Integer valueOf(String s)  throws  NumberFormatException  返回Integer对象,其中封装的整型数据位字符串s所表示。

例如:
 Integer i = new Integer(100); // 分配在堆空间里
 Double d = new Double("123.456");  //将字符类型转为Double类型放在Double类型封装类里面
 int j = i.intValue()+d.intValue(); // 把i,d 转为int类型 结果为:223

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

abs 绝对值
acos,asin,atan,cos,sin,tan
sqrt 平方根
pow(double a,double b) a的b次幂
log 自然对数以e为底
exp e为底指数
man(double a,double b)
min(double a,double b)
random()  返回0.0到1.0的随机数
long round(double a) double型的数据a转换为long型(四舍五入)
toDegrees(double angrad) 弧度->角度
toDadians(double angdeg) 角度->弧度

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

File类的常见构造方法:
 public File(String pathname)
 以pathname为路径创建File对象,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。
 public File(String parent,String child)
 以parent为父路径,child为子路径创建File对象

File的静态属性String separator存储了当前系统的路径分隔符。

File类的常用方法
1.通过File对象可以访问文件的属性;
public boolean canRead()     //能不能读
public boolean canWrite()    //能不能写
public boolean esists()      //文件是不是存在,new 出来的是内存的对象,不是硬盘上的文件
public boolean isDirectory() //是不是一个目录
public boolean isFile()      //是不是一个文件
public boolean isHidden()    //是不是隐藏的
public long lastModified()   //上次修改的时间(从某个时间到目前为止走过多少毫秒数)
public long length()         //文件内容长度
public String getName()      //文件名
public String getPath()      //路径
public String getAbsolutePath() //绝对路径

2.通过File对象创建空文件或目录(在该对象所指的文件或目录不存在的情况下)
public boolean createNewFile() throws IOException  //创建空目录
public boolean delete()   //删除一个文件
public boolean mkdir()    //创建一个路径
public boolean mkdirs()  //创建在路径中的一系列目录

5.【枚举类】

枚举类型:
 只能够取特定值的一个
 使用enum关键字
 是java.lang.Enum类型
public class TestEnum{
  public enum MyColor{red, green, blue};
 
  public static void main(String[] args){
      MyColor m = MyColor.red;
      swith(m){
 case red:
    System.out.println("red");break;
 case green:
    System.out.ptintln("green");break;     
 default:
    System.out.println("default");
      }
      System.out.println(m);
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值