表达式,转意符,常见类型转换
- 表达式
- 何为表达式
- 转意符
- “/”
- 常见类型的转换
- int string date 互相转
-
何为表达式
Java表达式是变量、常量、运算符、方法调用的序列,它执行指定的计 算并返回某个确定的值。其中,运算符按操作数的数目:有
一元运算符 ++、-- 、
二元运算符 +、-、>等等 和
三元运算符 ?:,
它们分别对应一到三个操作数。表达式一般按运算符来分:
算术表达式 float x=8.3f、i++、
关系表达式 3>7、3<7、
布尔逻辑表达式 (5>4)&&true、!false、
位运算表达式 a=34^3、
赋值表达式、
条件表达式 b=100>89?a=ture:a=false
以及复合表达式。
还有一种叫“表达式语句”,就是在表达式后加上分号作为语句来使用。int i=123; -
转意符"/"
当我们要输出(" ’ /)时直接写是行不通的这时候我们就需要用到转意符( \ )
System.out.println(" " “); //这样输出“是行不通的
System.out.println(”\""); // 转意符 在需要转意的字符串前加上( \ )
-
int string date 互相转
-
int to str
int a=2;
//一
String b=Integer.toString(a);
//二
String c=String.valueOf(a);
//三
String d=""+a;
//字符串与数字相加 数字类型会自动转换为字符串类型
-
str to int
String e=“123”;
//一
int f=Integer.parseInt(e);
//二
int g=Integer.valueOf(e).intValue();
-
str to date
String string = “2016-10-24 21:59:06”;
SimpleDateFormat sdf1 = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
System.out.println(sdf1.parse(string));
//PS:这里需要用到SimpleDateFormat类进行字符串格式化
-
date to str
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
String h=sdf.format(date);
System.out.println(h);
//PS:这里需要用到SimpleDateFormat类进行字符串格式化
-