首页 > 技术文章 > Java初学

leviatan 2017-10-14 18:47 原文

自动类型转换

Byte → short → int long → float → double

比较运算符

为比较,=恒等

逻辑运算符

&&针对布尔值进行判断,第一个表达式为false时,不判断第二个,这种判断方式通常被称为“短路”,判断整个表达式的方式被称为“非短路”

按位运算符

按位与 &

若两个整型数a.b对应位都是1,结果位才是1,如果两个操作数精度不同,则结果的精度与精度高的操作数相同

按位或 |

如果两个操作数对应位都是0,结果才为0,否则为1,精度不同时,结果的精度与精度高的操作数相同

按位取反 ~

也称按位非,就是将操作数中0 1对调

按位异或 ^

当两个操作数二进制表示相同时结果为0,否则为1,精度不同时,结果的精度与精度高的操作数相同

移位操作

对数据按二进制位进行移位操作,分以下三种

运算符 作用
<< 左移
| 右移

| 无符号右移
适用于byte、short、char、int、long

作用

可以实现整数乘除2^n 的效果,例如:y<< 2与y*4的结果相同,y>>1的结果与y/2的结果相同,总之,一个数左移n位,就是这个数乘以2^n ,右移n位就是这个数除以2^n

三元运算符

格式

条件式?值a:值b

运算规则为条件式值为true时,整个表达式取值a,否则取值b

等价于三元运算符的if...else实现代码如下

boolean a;
if (20<45)
    a = true;
else
    a = false;

运算符优先级

通常为

  1. 增量和减量运算
  2. 算术运算
  3. 比较运算
  4. 逻辑运算
  5. 赋值运算

运算符的优先级

优先级 描述 运算符
1 括号 ()
2 正负号 + -
3 一元运算符 ++ -- !
4 乘除 * / %
5 加减 + -
6 移位运算 << >> >>>
7 比较大小 < > >= <=
8 比较是否相等 == !=
9 按位与运算 &
10 按位异或运算 ^
11 按位或运算 |
12 逻辑与运算 &&
13 逻辑或运算 ||
14 三元运算符 ? :
15 赋值运算符 =

数据类型转换

隐式类型转换

从低级类型向高级类型的转换,按精度从低到高排列的顺序为byte< short< int< long< float< double

显式类型转换

把高精度的变量的值赋给低精度的变量时,必须使用显式类型转换(强制类型转换)
语法如下

(类型名)要转换的值

实现代码

int a = (int)45.23; //45
long y = (long)456.6F;  //456
int b = (int)'d';   //100

foreach语句

语法如下

for(元素变量x :遍历对象 obj){
    引用了x的java语句;
}

示例

public static void main(String[] args) {
	int a[] = {7,10,20};
	for (int x:a){
		System.out.println(x);
	}
}

输出结果

7
10
20

String类

声明字符串

String str = [hello java]

常用构造方法

String(char a[]) 方法

用一个字符数组a创建String对象

char a[] = {'g','o','o','d'};
String s = new String(a);

等价于

String s = new String("good")

String (char a[],int offset,int length)

提取字符数组a中的一部分创建一个字符串对象,参数offset表示开始截取字符串的位置,length表示截取字符串的长度

char a[] = {'s','t','u','d','e','n','t'};
String s = new String(a,2,4);   //从第3个元素开始取4个元素

等价于

String s = new String ("uden");

String (char[] value)

分配一个新的String对象,使其表示字符数组参数中所有元素连接的结果

char a[] = {'s','t','u','d','e','n','t'};
String s = new String(a);

等价于

String s = new String("student");

引用赋值

String str1;
str1 = "We are students"

连接字符串

连接多个字符串

public static void main(String[] args) {
	String s1 = new String ("hello");
	String s2 = new String ("world");
	String s = s1 + " " + s2;
	System.out.println(s);
}

输出结果

hello world

连接其它数据类型

public static void main(String[] args) {
	int time = 4;
	float practice = 2.5f;
	System.out.println("我每天花费"+time+"小时看书,"+practice+"小时练习,共"+(practice+time)+"小时。");
}

输出结果

我每天花费4小时看书,2.5小时练习,共6.5小时。

获取字符串信息

获取字符串长度

语句如下

str.length();

实现代码

public static void main(String[] args) {
	String str = "We are students";
	int size = str.length();
	System.out.println(size);
}

输出结果

15

字符串查找

indexOf()

返回的是搜索的字符或字符串首次出现的位置

语法

str.indexOf(substr)

没有检索到字符串substr时返回-1

public static void main(String[] args) {
	String str = "We are students";
	int size = str.indexOf("s");
	System.out.println(size);
}

返回结果

7

lastIndexOf()

返回的是搜索的字符或字符最后一次出现的位置

没有检索到字符串substr时返回-1

public static void main(String[] args) {
	String str = "We are students";
	int size = str.lastIndexOf("s");
	System.out.println(size);
}

返回结果

14

获取指定索引位置的字符

语法

str.cahrAt(int index)

public static void main(String[] args) {
	String str = "hello world";
	char mychar = str.charAt(6);
	System.out.println(mychar);
}

输出结果

w

字符串操作

获取子字符串

substring (int beginIndex)

该方法返回的是从指定的索引位置开始截取直到字符串结尾的子串

语法

str.substring(int beginIndex)

public static void main(String[] args) {
	String str = "Hello World";
	String substr = str.substring(3);
	System.out.println(substr);
}

输出结果

lo World

substring (int beginIndex,endIndex)

该方法返回的是从字符串某一索引位置开始截取至某一索引位置结束的子串

语法

substring (int beginIndex,endIndex)

public static void main(String[] args) {
	String str = "Hello World";
	String substr = str.substring(3,9);
	System.out.println(substr);
}

输出结果

lo Wor

去除空格 trim()

返回字符串的副本,忽略前导空格和尾部空格

语法

str.trim()

public static void main(String[] args) {
	String str = "   Java   class   ";
	System.out.println("原字符串"+str+"原长度"+str.length());
	System.out.println("去空格后"+str.trim()+"去空格长度"+str.trim().length());
}

输出结果

原字符串   Java   class   原长度18
去空格后Java   class去空格长度12

字符串替换 replace()

语法

str.replace(char oldChar,char newChar)

public static void main(String[] args) {
	String str = "address";
	String newstr = str.replace("a","A");
	System.out.println(newstr);
}

输出结果

Address

判断字符串的开始与结尾

startsWith()

用于判断当前字符串对象的前缀是否是参数指定的字符串

语法

str.startsWith(String prefix)

startsWith()

用于判断当前字符串对象的前缀是否是以给定的字符串结束

语法

str.endWith(String suffix)

public static void main(String[] args) {
	String num1 = "22045612";
	String num2 = "21304578";
	boolean b1 = num1.startsWith("22");
	boolean b2 = num1.endsWith("78");
	boolean b3 = num2.startsWith("22");
	boolean b4 = num2.endsWith("78");
	System.out.println(b1);
	System.out.println(b2);
	System.out.println(b3);
	System.out.println(b4);
}

输出结果

true
false
false
true

判断字符串是否相等

equals()

如果两个字符串具有相同的字符和长度,返回true,区分大小写

语法

str.equals(String otherstr)

equalsIgnoreCase()

忽略大小写

语法

str.equalsIgnoreCase(String otherstr)

public static void main(String[] args) {
	String s1 = new String ("abc");
	String s2 = new String ("ABC");
	boolean b1 = s1.equals(s2);
	boolean b2 = s1.equalsIgnoreCase(s2);
	System.out.println(b1);
	System.out.println(b2);
}

输出结果

false
true

按字典顺序比较字符串compareTo()

该比较基于字符串中各个字符的Unicode值,按字典顺序将此String对象表示的字符序列与参数字符串所表示的字符序列进行比较,如果按字典顺序此Srting对象位于参数字符串之前,则比较结果为一个负整数;如果按字典顺序此String对象位于参数字符串之后,则比较结果为一个正整数,如果两个字符串相等,结果为0

语法

str.compareTo(String otherstr)

public static void main(String[] args) {
	String str1 = new String ("b");
	String str2 = new String ("a");
	String str3 = new String ("c");
	String str4 = new String ("z");
	System.out.println(str1.compareTo(str2));
	System.out.println(str1.compareTo(str3));
	System.out.println(str1.compareTo(str4));
}

输出结果

1
-1
-24

字母大小写转换

toLowerCase()

该方法将String转换为小写

语法

str.toLowerCase()

toUpperCase()

该方法将String转换为大写

语法

str.toUpperCase()

public static void main(String[] args) {
	String str = new String ("abc DEF");
	String newstr1 = str.toLowerCase();
	String newstr2 = str.toUpperCase();
	System.out.println(newstr1);
	System.out.println(newstr2);
}

输出结果

abc def
ABC DEF

字符串分割 split()

split (String sign)

该方法可根据给定的分隔符对字符串进行拆分

语法

str.split(String sign)

split (String sign,int limit)

该方法可根据给定的分隔符对字符串进行拆分,并限制拆分次数

语法

str.split (String sign,int limit)

public static void main(String[] args) {
	String str = new String ("abc,def,ghi,jkl");
	String[] newstr = str.split(",");
	System.out.println("第一次拆分");
	for (int i = 0;i < newstr.length;i++){
		System.out.println(newstr[i]);
	}
	
	String[] newstr2 = str.split(",",2);
	System.out.println("第二次拆分");
	for (int j = 0;j< newstr2.length;j++){
		System.out.println(newstr2[j]);
	}
}

输出结果

第一次拆分
abc
def
ghi
jkl
第二次拆分
abc
def,ghi,jkl

格式化字符串 format()

format (String fomat,Object...args)

语法

str.format(String format,Object...args)

format:格式字符串

args:格式字符串中由格式说明符引用的参数。如果还有格式说明符以外的参数,则忽略这些额外的参数。此参数数目可变,可以为0.

日期和时间字符串格式化

日期格式化

实例

public static void main(String[] args) {
	Date date = new Date();
	String s = String.format("%te", date);
	System.out.println(date);
	System.out.println(s);
}

输出结果

Tue Jul 11 14:33:38 CST 2017
11

常用的日期格式化转换符

转换符 说明 示例
%te 一个月中的某一天 11
%tb 指定语言环境的月份简称 Feb(英文)、二月(中文)
%tB 指定语言环境的月份全称 February(英文)、二月(中文)
%ta 指定语言环境的周几简称 Mon(英文)、星期一(中文)
%tA 指定语言环境的周几全称 Monday(英文)、星期一(中文)
%tc 包括全部日期和时间信息 星期二 七月 11 14:42:19 CST 2017
%tY 四位年份 2017
%tj 一年中的第几天 192
%tm 月份 07
%td 一个月中的第几天 11
%ty 2位年份 17

时间格式化

时间格式化转换符

转换符 说明 示例
%tH 2位数字的24小时制的小时 15
%tI 2位数字的12小时制的小时 03
%tk 2位数字的24小时制的小时 15
%tl 2位数字的12小时制的小时 3
%tM 2位数字的分钟 05
%tS 2位数字的秒数 41
%tL 3位数字的毫秒数 673
%tN 9位数字的微秒数 520000000
%tp 指定语言环境的上下午标记 下午(中文)、pm(英文)
%tz 相对于GMT RFC 82格式的数字时区偏移量 +0800
%tZ 时区缩写形式的字符串 CST
%ts 1970-01-01 00:00:00到现在经过的秒数 1499756819
%tQ 1970-01-01 00:00:00到现在经过的毫秒数 1499756833677

格式化常见的日期时间组合

常见的日期和时间

推荐阅读