1.初体验
编写HelloWorld程序
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
2.变量类型
public class VariableDemo {
public static void main(String[] args) {
// 定义byte类型,一个字节,范围-128到127
byte b = -128;
System.out.println(b);
// 定义short类型
short s = 100;
System.out.println(s);
// 定义int
int i = 1000;
System.out.println(i);
// 定义float
float c = 3.14f;
System.out.println(c);
// 定义double
double d = 3.14;
System.out.println(d);
// 定义boolean
boolean x = true;
System.out.println(x);
}
}
3.算数运算符
package daigua.zhuwa;
public class OperatorDemo {
public static void main(String[] args) {
int a = 3;
int b = 4;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
// 有浮点数参与才会显示小数
float c = 3.0f;
System.out.println(c / b);
int d = 10;
int e = 3;
System.out.println(d % e); // 取余
// 字符参与加法运算
char f = 'A';
System.out.println(f);
System.out.println(a + f);
// 字符串跟其他数据做加法 是做字符串的拼接
System.out.println("hello" + a);
System.out.println(a + b + "hello");
// ++ -- 自增自减,单独使用时,放在前面或者后面效果一样
int g = 10;
System.out.println("g:" + g);
g++;
System.out.println("g:" + g);
++g;
System.out.println("g:" + g);
// int h = a;
// int h = a++;
int h = ++ a;
System.out.println("a:" + a);
System.out.println("h:" + h);
}
}
4.赋值运算符
package daigua.zhuwa;
/*
* 赋值运算符:
* 基本: =
* 扩展: += -= *= /=
* */
public class OperatorDemo2 {
public static void main(String[] args) {
int a = 10;
System.out.println("a:" + a);
// +=
a += 20;
System.out.println("a:" + a);
// 注意,扩展的赋值运算符隐含了强制类型转换
short s = 1;
s += 1;
System.out.println("s:" + s);
}
}
5.关系运算符
package daigua.zhuwa;
/*
* 关系运算符
* == != > >= < <=
* */
public class OperatorDemo3 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
System.out.println(a == c);
System.out.println(a == b);
System.out.println(a < b);
System.out.println(a < c);
System.out.println(a <= c);
System.out.println(a != b);
}
}
6.逻辑运算符
package daigua.zhuwa;
/*
* 逻辑运算符
* & | ^ !
* && ||
* */
public class OperatorDemo4 {
public static void main(String[] args) {
int a = 3;
int b = 4;
int c = 5;
// & 就是and,同时满足
System.out.println((a>b) & (a>c));
System.out.println((a>b) & (a<c));
System.out.println((a<b) & (a<c));
System.out.println((a<b) & (a>c));
// | 就是or,一个满足
System.out.println((a>b) | (a>c));
System.out.println((a>b) | (a<c));
System.out.println((a<b) | (a<c));
System.out.println((a<b) | (a>c));
// ^ 就是异或,不同为真
System.out.println((a>b) ^ (a>c));
System.out.println((a>b) ^ (a<c));
System.out.println((a<b) ^ (a<c));
System.out.println((a<b) ^ (a>c));
// ! 非
System.out.println(!(a>b));
System.out.println(!(a<b));
// && 跟&结果一样,区别在于有短路效应
System.out.println((a>b) && (a>c));
System.out.println((a>b) && (a<c));
System.out.println((a<b) && (a<c));
System.out.println((a<b) && (a>c));
// || 跟|结果一样,区别在于有短路效应
System.out.println((a>b) || (a>c));
System.out.println((a>b) || (a<c));
System.out.println((a<b) || (a<c));
System.out.println((a<b) || (a>c));
}
}
7.三元运算符
package daigua.zhuwa;
/*
* 三元运算符
* 关系表达式?表达式1:表达式2
* */
public class OperatorDemo5 {
public static void main(String[] args) {
int a = 1;
int b = 2;
int c = (a > b)? a:b;
System.out.println(c);
}
}
8.键盘输入
package daigua.zhuwa;
import java.util.Scanner;
/*
* 实现键盘录入,使用Scanner
* */
public class MyScanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入:");
int i = sc.nextInt();
System.out.println("结果:"+ i);
}
}