package com.great.demo2;
public class StringDemo {
/*
* String:字符串
* 类型:复合引用数据类型
* 特点:
* 1.默认值:null
* 2.可以选择性使用new 即: String s = new String();
* //以下两句表达式效果一致,都可以初始化String类型
String s = "Hello";
String s1 = new String("Hello");
* 3.String类型进行参数传递时,形参改变内容,实参不变
*
*
* StringBuffer 主要是作为字符串的增删改查
*
*
*/
public void strDemo1(String s){
s = "hehe";
System.out.println("method:"+s);
}
public static void main(String[] args) {
//以下两句表达式效果一致,都可以初始化String类型
String s = "Hello你好llo";
String s1 = new String("Hello");
StringDemo sd = new StringDemo();
sd.strDemo1(s);
System.out.println("main:"+s);
System.out.println("String类型s的长度是:"+s.length()); //返回String内部的字符数
System.out.println(s.charAt(4)); //从0开始数,数到第四个为止,返回该字符
System.out.println(s.getBytes().length); //中文占2个字节,但是按照字符算算1个字符
System.out.println(s.indexOf("llo")); //如果字符串有该字符串返回第一个字符串位置,如果没有返回-1
System.out.println(s.lastIndexOf("llo"));
String a = "aBc"; //65
String b = "abc"; //97
int i = a.compareTo(b); //比较字符串大小
System.out.println(i);
String a1 = "Hello";
String b1 = "Hello";
StringDemo sd1 = new StringDemo();
StringDemo sd2 = new StringDemo();
System.out.println(a1.equals(b1)); //比较String内容是否相等
System.out.println(a1 == b1); //除了String之外变量的值是否相等用 ==
System.out.println(sd1 instanceof StringDemo); //该对象是否属于该类
String sReplace = "叶威逗逼";
sReplace = sReplace.replace("叶威", "刘彬");
sReplace = sReplace.replace("逗逼", "请客吃饭");
System.out.println(sReplace);
/*System.out.println(sd1.equals(sd2));*/
//==
StringBuffer sb = new StringBuffer("aaa"); //可以承装10个字符
System.out.println(sb.capacity());
sb.append("demo");
sb.insert(2,"demo");
System.out.println(sb);
}
}
package com.great.demo3;
public class Demo {
/*
* 封装类:
* 基本数据类型在复合引用类型中,拥有一种与之一样效果的类,这种类就是封装类
* int --- Integer
* short --- Short
* long --- Long
* byte --- Byte
* float --- Float
* double --- Double
* boolean --- Boolean
* char --- Character
*
* 封装类几乎和基本数据类型一样使用,主要是一开始时,需要复合引用类型方式的初始化
*
* 相互转换:
* 封装类 --- 基本数据类型
* 格式:
* 基本数据类型 变量名 = 封装类的对象.基本数据类型Value();
* int i1 = i.intValue();
*
*
* 基本数据类型 --- 封装类
* boolean b = false;
* Boolean b1 = new Boolean(b);
*
*
* String --- 基本数据类型
*
* 基本数据类型 变量名 = 基本数据类型对应的封装类.parse基本数据类型(String类型参数);
* String sInt = "999";
int iParse = Integer.parseInt(sInt);
System.out.println(iParse);
*
*
*
*
*
*
*/
public static void main(String[] args) {
//----------------- 封装类 --- 基本数据类型 ---------------------
Integer i = new Integer(100);//封装类初始化
System.out.println(i);
int i1 = i.intValue();//封装类转换成基本数据类型
Float f = new Float(3.14);
float f1 = f.floatValue();
//-----------------基本数据类型 --- 封装类 ---------------------
boolean b = false;
Boolean b1 = new Boolean(b);
//----------------String --- 基本数据类型 --------------------
String sInt = "999"; //是
int iParse = Integer.parseInt(sInt);
System.out.println(iParse);
//----------------基本数据类型 --- String -------------------
int a = 1;
String sa = ""+a; //直接加“”
System.out.println(sa);
String str = String.valueOf(3.1415);//用valueOf
System.out.println(str);
}
}