第五章 适用对象

第一节 字符类型
单个的字符:char
public class Main {
        public static void main(String[] args ) {
               // TODO Auto-generated method stub
              Scanner in = new Scanner(System. in );
               char c = 'A' ;
               char d = 'D' ;
               c ++;
              System. out .println( d - c );
       }
}
输出3

字符【char】和整数【int】可以互相转换

大小写转换
1.大写-小写
public class Main {
        public static void main(String[] args ) {
               // TODO Auto-generated method stub
              Scanner in = new Scanner(System. in );
               char c = 'A' ;
               char d =( char )( c + 'a' - 'A' );
               c ++;
              System. out .println( d );
       }
}
2.小写到大写
public class Main {
        public static void main(String[] args ) {
               // TODO Auto-generated method stub
              Scanner in = new Scanner(System. in );
               char c = 'a' ;
               char d =( char )( c + 'A' - 'a' );
               c ++;
              System.out.println( d );
       }
}

第二节包裹类型
基础类型:boolean     char     int     double
包裹类型:Boolean     Character     Interger     Double
5.2.1可以和基础类型一样赋值eg.int i=10; Integer k=10;
5.2.2包裹类型和.配合可以起到奇效。
Integer k =10;
System. out .println(Integer. MAX_VALUE );
可以找到他的最大值或者最小值( Integer. MIN_VALUE)。
System. out .println(Character.toLowerCase( 'A' ));可以输出小写的a。

第三节字符串变量
String s;
String s;是一个类,String的变量是对象的管理者而非所有者。
就像数组变量是数组的管理者而非所有者一样

new=创建
String s=new String(“a string”);
创建了一个String的对象
用“a String”初始化这个对象
创建管理这个对象的变量s
让s管理这个对象
String s="hello";

字符串连接
“hello”+“world”-->"helloword"


输入字符串
in.next();读入一个单词,单词的标志是空格
in.nextLine();读入一整行

比较字符串
1.==判断是不是同一个东西
2.用equal判断内容是不是一样

字符串操作
1.equal       System. out .println( s .equals( "bye" ));
2.comparto     System. out .println( "abc" .compareTo( "abcd" ));比较的是编码的大小
3.length()      System. out .println( "abcd" .length());"abcd"的长度
4. "abcd".charAt       System. out .println( "abc" .charAt(0)); 不能用for-each来遍历,只能有for循环。
5. "01234567abc" .substring(2)      System. out .println( "01234567abc" .substring(2)); System. out .println( "01234567abc" .substring(2,4));显示 234567abc; 23
5.index     System. out .println( "01234567abc" .indexOf( 'A' )); System. out .println( "01234567abc" .indexOf( 'a' )); System. out .println( "01234567abc" .indexOf( "abc" ));
显示-1、8和8吗,表示A不存在;a在第8位;abc在第8位。

第四节 Math类
1.math.abs求绝对值     System. out .println(Math.abs(-10));
2.math.round求四舍五入    System. out .println(Math.round(10.564));
3.math.random() 求随机值     System. out .println(Math.random());
4.math.pow求幂次方     System. out .println(Math.pow(2,3));



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值