String str = "china";
String str2 = str.toUpperCase();//问:str 是什么?china
// str2 是 CHINA
str = "tit";
public class ChangeMobile {
public String change(String oldMobile) {
StringBuilder stringBuilder = new StringBuilder(oldMobile);
stringBuilder.replace(3, 7, "****");
return stringBuilder.toString();
}
/**不使用可变字符串类*/
public String change2(String oldMobile) {
String first = oldMobile.substring(0,3);
String second = oldMobile.substring(7);
return first + "****" + second;
}
public static void main(String[] args) {
ChangeMobile cm = new ChangeMobile();
System.out.println("请输入您的手机号:");
Scanner scanner = new Scanner(System.in);
String om = scanner.next();
System.out.println("隐藏后的手机号为:" + cm.change(om));
System.out.println("隐藏后的手机号为:" + cm.change2(om));
}
}
public class ReverseString {
/**输入字符串abc,输出cba*/
public String reverseStr(String oldStr) {
StringBuilder str1 = new StringBuilder(oldStr);
str1.reverse();
return str1.toString();
}
/**不使用可变字符串*/
public String reverseStr2(String oldStr) {
//abcd
String newStr = "";
for (int i = oldStr.length() - 1; i >= 0; i--) {
char c = oldStr.charAt(i);
newStr = newStr + c;
}
return newStr;
}
public static void main(String[] args) {
ReverseString rs = new ReverseString();
System.out.println(rs.reverseStr("abc"));
System.out.println(rs.reverseStr2("abcd"));
}
}
StringBuilder str1 = new StringBuilder("china");
StringBuilder str2 = new StringBuilder("china");
System.out.println(str1.equals(str2));//false
System.out.println(str1 == str2);//false
String str3 = new String("china");
String str4 = new String("china");
System.out.println(str3.equals(str4));//true
System.out.println(str3 == str4);//false
public static void main(String[] args) {
int first [] = {100,200,300,400,500};
int second [] = {1,2,3,4,5};
System.arraycopy(first, 1, second, 2, 3);
for (int x : second) {
System.out.println(x + " ");
}
}
- 在API文档里看不到Math构造方法,什么原因? 为什么里面的方法被static修饰?
- 答:没有构造方法,因为它的成员全部是静态的。
生成随机数0~1,包含0,不包含1;生成随机小数double【面试题】
//生成0-1000的随机整数
Random ran = new Random();
int x = ran.next(1000);
public class DateUtil {
/** 获得当前日期,使用Date类 */
public static String getCurrentDate() {
Date date = new Date();
//转成本地格式
return date.toLocaleString();
}
/** 使用Calendar获得日期时间*/
public static String getCurrentDateByCalendar() {
Calendar c = Calendar.getInstance();
return "" + c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + " " + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
}
public static void main(String[] args) {
System.out.println(DateUtil.getCurrentDate());
System.out.println(DateUtil.getCurrentDateByCalendar());
}
}
public class FinalizeMethod {
protected void finalize() {
System.out.println("Student对象被垃圾回收时,finalise被调用");
}
public static void main(String[] args) {
FinalizeMethod s = new FinalizeMethod();
s = null;//对象设置为null,可以进行回收
System.gc();
}
}
public class Teacher {
private String name;
private int age;
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
//重写equals方法 Ctrl+shift+s -> Generate hashCode()and equals
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Teacher other = (Teacher) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public static void main(String[] args) {
Teacher t1 = new Teacher("吴老师", 25);
Teacher t2 = new Teacher("吴老师", 25);
System.out.println(t1 == t2);//输出结果:false 地址比较两个对象两个地址指向
System.out.println(t1.equals(t2));//输出结果:true
}
}
- boolean ------ Boolean
- byte ------------Byte
- short----------- Short
- char ----------- Character
- int -------------- Integer
- double -------- Double
- float ----------- Float
- long ----------- Long
Integer x = new Integer(20);//引用类型
Integer y = new Integer("20");
int z = 20;//基本类型
System.out.println(z == x);//true,自动拆箱机制
System.out.println(x == y);//false,new两次比较地址
System.out.println(z == y);//true,自动拆箱机制
System.out.println(x.equals(y));//true
Integer m = 100;//自动装箱,100是基本类型,左侧是Integer类类型,把100封装成对象,在赋值给m;
int n = x;//自动拆箱右侧x是Integer对象
Integer mv = new Integer("23abc");//这是错误的,里边的参数必须是数字构成的
Integer mv1 = new Integer("");//错误
String str = "2000";
int num = Integer.parseInt(str);//把字符串变成数字,通过封装类完成
String str1 = "2000";
double num1 = Double.parseDouble(str1);
int grade = 4;//数字变成字符串
String gra = grade + "";
System.out.println(1 - 0.9); // 结果不是0.1,而是0.09999999999999998,二进制到十进制转换出现误差
public class Calc {
/**减法计算:依然无法精确计算*/
public double sub(double num1, double num2) {
BigDecimal number1 = new BigDecimal(num1);// API,参数是double类型
BigDecimal number2 = new BigDecimal(num2);
return number1.subtract(number2).doubleValue();
}
/** 实现精确计算*/
public double subV2(double num1, double num2) {
BigDecimal number1 = new BigDecimal(num1 + "");// API,参数是String类型
BigDecimal number2 = new BigDecimal(num2 + "");
return number1.subtract(number2).doubleValue();
}
private static final int DEF_DIV_SCALE = 3;
/**提供精确的除法运算,当发生除不尽的情况时,精确到小数点以后3位,以后的数字四舍五入*/
public static double div(double v1, double v2) {
return div(v1, v2, DEF_DIV_SCALE);
}
/**提供精确的除法运算。当发生除不尽的情况时,由scale参数指定精度,以后的数字四舍五入*/
public static double div(double v1, double v2, int scale) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static void main(String[] args) {
Calc calc = new Calc();
System.out.println(calc.subV2(1.0, 0.9));
System.out.println("小数除法精确计算结果:" + bid.div(11.3, 2.1));
}
}
public class BigIntegerDemo {
public static void main(String[] args) {
BigInteger bd1 = new BigInteger("12345556789");
BigInteger bd2 = new BigInteger("45678912345636956565656565");
System.out.println("大整数加法计算结果:" + bd2.add(bd1));
System.out.println("大整数减法计算结果:" + bd2.subtract(bd1));
System.out.println("大整数乘法计算结果:" + bd2.multiply(bd1));
System.out.println("大整数除法计算结果:" + bd2.divide(bd1)); }
}
String name = "chian";
String addr = new String("hebei")
String str1 = "hello";
String str2 = "hello";
String str3 = new String("hello");
- 首次创建Hello时,Hello被放入字符串常量池,如果有其它变量的值也是Hello的话,直接从常量池里取;已经存在的Hello,不再创建新的。也就是这个字符串Hello被共享,这也是String不可变的原因(因为被共享)。 new String(“Hello”),每new一次都有一个新的空间在堆区产生。
分析以下程序的输出结果:
String str1 = "hello";
String str2 = "hello";
String str3 = new String("hello");
String str4 = new String("hello");
System.out.println(str1 == str2);//true
System.out.println(str3 == str4);//false
System.out.println(str1 == str3);//false
System.out.println(str1.equals(str3));//true
System.out.println(str3.equals(str4));//true
StringBuilder str1 = new StringBuilder("china");
StringBuilder str2 = new StringBuilder("china");
System.out.println(str1.equals(str2));//false
System.out.println(str1 == str2);//false
String str3 = new String("china");
String str4 = new String("china");
System.out.println(str3.equals(str4));//true
System.out.println(str3 == str4);//false
- final是Java关键字,用于修饰类、属性、方法和变量
- finalize()是Object类的方法,与垃圾回收有关,在垃圾回收前被自动调用
public class StringCount {
public static void main(String[] args) {
String str1 = "ab12abaabnbab5abaxb";
String str2 = "ab";
int time = 0;
int start = 0;
while (str1.indexOf(str2, start) >= 0 && start < str1.length()) {
time++;
start = str1.indexOf(str2, start) + str2.length();
}
System.out.println("方法1:给定字符串中ab出现" + time + "次");
int count = (str1.length() - str1.replace(str2, "").length())/str2.length();
System.out.println("方法2:给定字符串中ab出现" + count + "次");
}
}
public class Code {
public static void sha(String un) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest m = MessageDigest.getInstance("SHA-1");
m.update(un.getBytes("UTF-8"));
byte[] s = m.digest();
System.out.println(Arrays.toString(s));
}
public static void md5(String un) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest m = MessageDigest.getInstance("MD5");
m.update(un.getBytes("UTF-8"));
byte[] s = m.digest();
System.out.println(Arrays.toString(s));
}
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String uname = scanner.next();
System.out.println("请输入密码:");
String upsw = scanner.next();
System.out.println("用户名的MD5编码为:");
md5(uname);
System.out.println("密码的MD5编码为:");
md5(upsw);
System.out.println("用户名的SHA-1编码为:");
sha(uname);
System.out.println("密码的SHA-1编码为:");
sha(upsw);
}
}
学号格式:170554119,05是系的编号;01-机械系,02-电子系,03-自动化系,05-计算机系,08-理学系
public class StudentID {
public void getCs(String id) {
String str = id.substring(6,8);
switch(str) {
case "01":
System.out.println("该学生为机械系。");
break;
case "02":
System.out.println("该学生为电子系。");
break;
case "03":
System.out.println("该学生为自动化系。");
break;
case "05":
System.out.println("该学生为计算机系。");
break;
case "08":
System.out.println("该学生为理学系。");
break;
default:
System.out.println("查询不到该生的有效信息!");
break;
}
}
public static void main(String[] args) {
StudentID si = new StudentID();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的学号(类似于201720050235):");
String str = scanner.next();
si.getCs(str);
}
}