什么是枚举;为什么需要枚举
指由一组固定的常量组成的类型;限制赋值。将有可能的值都列举出来,赋值时只能赋列举出的值
关键字:enum
1.实现基本类型之间的转换
2.便于函数传值(泛型时不能传基本数据类型)
3.在某处用到Object 类型时,方便将基本数据类型转换
作用
1.包装类把基本类型数据转换为对象(每个基本类型在java.lang包中都有一个相应的包装类)
2.提供了一系列实用的方法
3.集合不允许存放基本数据类型数据,存放数字时,要用包装类型
基本数据类型转换为包装类
int —– Integer【特别】
char —– Character【特别】
byte —– Byte
short —– Short
long —– Long
float —– Float
double —— Double
boolean —– Boolean
构造方法
1.所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例 如:public Type(type value) ? Integer i=new Integer(1); 2.除Character类外,其他包装类可将一个字符串作为参数构造它们的实例 如:public Type(String value) 注意: 1.Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false 2..当包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译通过,运行时NumberFormatException异常
常用方法
package baoZhuangLei; 2 3 public class Demo1 { 4 public static void main(String[] args) { 5 //包装类---》基本类型*****Value(); 6 Integer i= new Integer(9); 7 int j = i.intValue(); 8 System.out.println(j); 9 10 11 Boolean b = new Boolean(true); 12 boolean b1 =b.booleanValue(); 13 System.out.println(b1); 14 15 //基本类型---》字符串(1)toString() (2) +"" 16 int num = 9; 17 String strNum=Integer.toString(num); 18 String strNum2=num+""; 19 20 //字符串--》基本类型 parse*****()Character类除外 21 String s = "89"; 22 int num2= Integer.parseInt(s); 23 boolean bNum = Boolean.parseBoolean("true"); 24 25 //valueOf():(1)如何把基本数据类型变成包装类(所有包装类都有这个方法) 26 Integer iNum = Integer.valueOf(89); 27 Character cNum = Character.valueOf(‘z‘); 28 Boolean BNum =Boolean.valueOf(true); 29 //(2)如何把字符串变成包装类(除Character外) 30 Integer iNum2 =Integer.valueOf("89"); 31 32 Boolean bNum2 = Boolean.valueOf(true); 33 34 35 //拆箱和自动装箱 36 Integer integer=5;//装箱:基本类型转换为包装类型的对现 37 int inValue =integer;//拆箱:包装类对象转换为基本类型的值 38 39 40 } 41 42 }
2.使用String对象存储字符串
3.String类位于java.lang包中,具有丰富的方法(计算字符串的长度、比较字符串、连接字符串、提取字符串)
length()方法
String类提供了length()方法,确定字符串的长度(返回字符串中的字符数)
equals()方法
String类提供了equals( )方法,比较存储在两个字符串对象的内容是否一致
equals():检查组成字符串内容的字符是否完全一致
“==”和equals()有什么区别呢?
==:判断两个字符串在内存中的地址,即判断是否是同一个字符串对象
equals():比较的是内容
字符串比较的其他方法
1.使用equalsIgnoreCase()方法
2.使用toLowerCase()方法
3.使用toUpperCase()方法
Scanner sc = new Scanner(System.in); System.out.println("欢迎进入注册系统....."); String name=""; String pwd1=""; String pwd2=""; boolean flag=false; do{ System.out.print("请输入用户名:"); name=sc.next(); System.out.print("请输入密码:"); pwd1=sc.next(); if(name.length()<3 || pwd1.length()<6){ flag=true; System.out.println("用户名长度不能小于3密码长度不能小于6"); }else{ System.out.println(pwd1.length()); System.out.print("请再次输入密码:"); pwd2=sc.next(); if(pwd1.equals(pwd2)){ flag=false; System.out.println("注册成功,请牢记用户名和密码"); }else{ flag=true; System.out.println("输入的两次密码不一致"); } } }while(flag); System.out.println("程序结束");
public static void main(String[] args) { String str1="tom"; String str2="TOM"; System.out.println(str1.equals(str2));//false //不区分大小写 System.out.println(str1.equalsIgnoreCase(str2));//true //全变为小写 System.out.println(str1.toLowerCase().equals(str2.toLowerCase()));//true //全变为大写 System.out.println(str1.toUpperCase().equals(str1.toUpperCase()));//true String s= " admin "; System.out.println(s.trim());//输出admin 去掉两端的空格 }
字符串常用提取
搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1 public int indexOf(int ch) public int indexOf(String value) 搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1 public int lastIndexOf(int ch) public int lastIndexOf(String value) 提取从位置索引开始的字符串部分 public String substring(int index) 提取beginindex和endindex之间的字符串部分 public String substring(int beginindex, int endindex) 返回一个前后不含任何空格的调用字符串的副本 public String trim()
package cn.String; import java.util.Scanner; public class StrDemo { // 字符串截取:indexOf() lastIndexOf() substring(begin) substring(begin,end) public static void main(String[] args) { // String s ="hello,my name is yangFan!,ok";//空格也算位置 // System.out.println(s.indexOf("o"));//4 // System.out.println(s.lastIndexOf("o"));//26 // // System.out.println(s.substring(4));//包含起始位置,到最后 // System.out.println(s.substring(4,5));//包含起始位置,不包含结束位置 // !!!!!String 的所有操作并不影响字符串本身,影响的是字符串的一个副本 String s = "I love you!"; System.out.println(s.substring(2, 6));// love System.out.println(s);// I love you! boolean nameFlag = false; Scanner inScanner = new Scanner(System.in); System.out.println("****************欢迎提交作业********************"); System.out.println("请输入作业名:"); String name = inScanner.next(); System.out.println("请输入邮箱:"); String email = inScanner.next(); // 检查文件名:必须以.java为后缀 int index = name.indexOf("."); if (index != -1 && index != 0 && name.substring(index).equals(".java")) { // 文件名正确 nameFlag = true; } else { System.out.println("文件名无效"); } boolean emailFlag = false; // 检查邮箱@ int index1 = email.indexOf("@"); int index2 = email.indexOf("."); if (index1 != -1 && index2 != -1 && index1 < index2) { emailFlag = true; } else { System.out.println("邮箱名无效"); } if (nameFlag && emailFlag) { System.out.println("作业提交成功!"); } else { System.out.println("作业提交失败"); } } }
对字符串频繁修改(如字符串连接)时,使用StringBuffer类可以大大提高程序执行效率
声明 StringBuffer strb = new StringBuffer(); StringBuffer strb = new StringBuffer("aaa"); 使用 sb.toString(); //转化为String类型 sb.append(" **"); //追加字符串** **sb.insert (1, "** "); //插入字符串
//对原有字符串操作 public static void main(String[] args) { /* //(1)string->stringBuffer StringBuffer s1=new StringBuffer("hello"); //追加 append s1.append("world!"); System.out.println(s1);//helloworld! //插入 insert s1.insert(1, ","); System.out.println(s1);//h,elloworld! System.out.println(s1); //(2)stringBuffer->string String s=s1.toString();*/ Scanner inputScanner=new Scanner(System.in); System.out.println("请输入一串数字"); String num=inputScanner.next(); StringBuffer numsBuffer=new StringBuffer(num); for (int i = numsBuffer.length();i>0; i=i-3) { numsBuffer.insert(i, ","); } System.out.println(numsBuffer);
经常改变内容的字符串最好不要使用
StringBuffer是可变的字符串
字符串经常改变的情况可使用StringBuffer,更高效
.abs() :获取数值的绝对值
.max() : 比较两个数字谁最大
@Test public void test2(){ int abs=Math.abs(-123); double max=Math.max(1.1, 2.2); int rand=(int) (Math.random()*10); System.out.println("abs:"+abs+" max:"+max); System.out.println(rand); }
注意:用同一个种子值来初始化两个Random 对象,然后用每个对象调用相同的方法,得到的随机数也是相同的
@Test public void test3(){ Random random=new Random(); for(int i=0;i<5;i++){ int nextInt = random.nextInt(10); System.out.print(nextInt); } }
提供操作日期和时间各组成部分的方法
Java.util.Date
java.text.SimpleDateFormat类
用于定制日期时间的格式
@Test public void test4(){ Date date=new Date(); SimpleDateFormat sad=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss "); String ss=sad.format(date); System.out.println(ss); }
抽象类,java.util.Calendar
用于设置和获取日期/时间数据的特定部分
Calendar类提供一些方法和静态字段来操作日历
@Test public void test5(){ Date date=new Date(); SimpleDateFormat sad=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss "); String ss=sad.format(date); System.out.println(ss); Calendar cal=Calendar.getInstance(); cal.set(Calendar.YEAR, 2015); cal.set(Calendar.MONTH, 4); cal.set(Calendar.DAY_OF_MONTH, 6); int week=cal.get(Calendar.WEEK_OF_YEAR); System.out.println("2015年4月6日是一年中的第"+week+"星期。"); }
原文:https://www.cnblogs.com/wufanfan/p/13023622.html