String a = "Hello", c = "hello";
int x = 5, y = 5;
float f = 1.0F;
1/2 = 0
分析:除法按照最大的类型显示。这里两个数都是Int类型,所以会舍弃掉小数1.0/2 = 0.5
或者 1/2.0 = 0.5
byte b1=10; b1 = b1+10;
这个就会犯大类型转小类型的错误b1 += 10;
int i=10,j=20;
boolean result = (i>10) && (++j>30);
System.out.println(result);
System.out.println(j);
abstract 返回值类型 方法名(参数)
abstract class 类名
Person p = new Person()
这种对于抽象类来说是不行的。需要先继承,在构造:class Chinese extends Person{……}
Chinese p = new Person();
abstract class Person{……}
abstract class Person{
public abstract void eat();
}
class Chinese extends Person{
public void eat(){
Sysout.out.println("使用筷子吃饭")
}
}
Chinese p = new Chinese();
p.eat();
interface 接口名称{规则属性,规则的行为}
interface USBInterface{
}
// 下面的规则符合USBInterface,行为就是powerXXX();
// 接口中的行为powerXXX是抽象的
interface USBSupply extends USBInterface{
public void powerSupply();
}
interface USBReceive extends USBInterface{
public void powerReceive();
}
// 电脑类中有两个USB接口,接口的功能是提供电源
class Computer implements USBSupply{
public USBReceive usb1;
public USBReceive usb2;
public void powerSupply(){
System.out.println("电脑提供能源");
usb1.powerReceive();
usb2.powerReceive();
}
}
// 灯
class Light implements USBReceive{
public void powerReceive(){
System.out.println("电灯接受能源");
}
}
// main中实现
Computer c = new Computer();
Light light = new Light();
c.usb1 = light;
c.powerSupply();
String[] names
new String[3]
String[] names = new String[3];
// 或者声明时,直接赋值
String[] nums = {"1","3","5"};
java.lang.String
,系统自动加载的String name = "这是字符串";
a.equals(b)
a.equalsIgnoreCase(b)
a.compareTo(b)
String s = "01234 6789";
s.substring(0,3); //截取0-2,不包含3
s.substring(6);//只传一个参数,表示截取到末尾。6-9
String s = "01234 6789";
String[] s1 = s.split(" ");//按空格来分割字符串
System.out.println(s1.length);
for(String s2:s1){
System.out.println(s2);
}
String s = " H e l l o ,mygoodfriend ";
System.out.println(s.trim());
System.out.println("!"+s.trim()+"!");
String s = "Hello World, World Li";
System.out.println(s.replace("World","Java");
String s = "Hello World, World Li";
System.out.println(s.replaceAll("World|Li","Java");
String s = "Hello mygoodfriend";
System.out.println(s.toLowerCase());
System.out.println(s.toUpperCase());
如果只想将username的第一个u变成大写,怎么操作:
String name = "username";
String s1 = name.substring(0,1);
String s2 = name.substring(1);
System.out.println(s1.toUpperCase() + s2);
String s = "Hello, my friends";
System.out.println(s.charAt(0));
String s = "Hello, my friends";
System.out.println(s.indexOf("my"));
String s = "Hello, my friends";
System.out.println(s.lastIndexOf("friends"));
String s = "Hello, my friends";
System.out.println(s.contains("mygood"));
System.out.println(s.contains("friends"));
String s = "Hello";
System.out.println(s.isEmpty());
String str = "";
System.out.println(str.isEmpty());
StringBuilder s = new StringBuilder();
for(int i=0;i<100;++i){
s.append(i);
}
System.out.println(s.toString());
String s = "";
for(int i=0;i<100;++i){
s = s+i;
}
System.out.println(s);
StringBuilder s = new StringBuilder();
s.append("abc");
StringBuilder s = new StringBuilder();
s.append("abc");
System.out.println(s.toString());
StringBuilder s = new StringBuilder();
s.append("abc");
System.out.println(s.length());
StringBuilder s = new StringBuilder();
s.append("abc");
System.out.println(s.reverse());
StringBuilder s = new StringBuilder();
s.append("012");
System.out.println(s.insert(1,"dr")); // 在下标为1的地方插入字符串dr