Java中的一些常用类的方法

Math方法:

package com.jzlg.lanqiao;

import javax.swing.border.MatteBorder;

public class Sum {

 public static void main(String[] args) {
  //Math类常用方法
  //返回绝对值
  System.out.println(Math.abs(-12));
  //返回两个参数的教大值
  System.out.println(Math.max(3, 5));
  //返回两个参数的最小值
  System.out.println(Math.min(3, 5));
  //返回0.0~1.0之间的double类型的随机数,包括0.0,但不包括1.0
  System.out.println((int)(Math.random()*100));
     //返回四舍五入的整数值
  System.out.println(Math.round(3.6));
  //平方根函数
  System.out.println(Math.sqrt(4));
  //幂运算
  System.out.println(Math.pow(2, 3));
  
  
 }

}

 

StringBuffer类中的方法:

package com.jzlg.lanqiao;

public class Buffer {
 public static void main(String args[])
 {
  StringBuffer stringBuffer=new StringBuffer("Mstanford");
  //将字符串序列反转输出
  stringBuffer.reverse();
  //在指定位置插入字符
    stringBuffer.setCharAt(4, 't');
  //移除指定的字符
  stringBuffer.deleteCharAt(2);
  //将StringBuffer装换为String类型
  String str=stringBuffer.toString();
  System.out.println(stringBuffer);
  //Integer类的操作
  Integer integer=new Integer(100);
  //将Inter类型转换为基本类型int
  int a=integer.intValue();
 }

}

 

String类中的方法

package com.jzlg.lanqiao;

public class Str {
 public static void main(String agrs[]){
  int a=2012;
  int b=2013;
 String str="Mstanford";
 //返回指定位置以后的字符串
  System.out.println(str.substring(2));
  //返回指定范围的新字符串
  System.out.println(str.substring(0, 2));
  //将整形转换为字符串
  String a1=String.valueOf(a);
  String b1=String.valueOf(b);
  System.out.println(a1+b1);
  //比较两个字符串大小,下面返回-1
  String str1="a";
  String str2="b";
  System.out.println(str1.compareTo(str2));
  //返回指定位置的char值
 char str3=str.charAt(2);
         //查看字母的ASCII码值,将char转换成int
        int  B ='a';
        int con=str3;
        //将小写字母转换为大写
        char con1=(char) (B-32);
        System.out.println(con1);
        System.out.println(B);
        //测试字符串是否以指定的后缀结束,返回boolean类型
        System.out.println(str.endsWith("d"));
        //返回指定字符的下标
        System.out.println(str.indexOf("a"));
         //清除字符串左右两端的空格
        System.out.println(str);
        System.out.println(str.trim());
  
  
  
 }


}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值