注释
-
单行注释://单行注释
-
多行注释:/* 多行注释*/
-
文档注释:JavaDoc文档。采用/**回车
注意:
Locale输入为:zh_CN
Other command line arguments输入为:
-encoding UTF-8 -charset UTF-8 -windowtitle "你的文档在浏览器窗口标题栏显示的内容" -link http://docs.Oracle.com/javase/7/docs/api
结果为:
Java数据类型分为两类:
-
基本类型:
1.1.1. 整数类型:byte(1字节)、short(2字节)、int(4字节)、long(8字节)
1.1.2. 浮点类型:float(4字节)、double(8字节)
1.1.3. 字符类型:char(2字节)
1.2. boolean类型:true、false
-
引用类型:类、接口、数组
数据类型的几个注意点
-
Long类型(整数)在数字后要加个L:long num=30L
-
Ffloot类型(小数)在数字后要加个F:float num2=30.2F
-
布尔类型是:boolean,true 和false
-
字符串是String(S大写),它不是基本数据类型,而是一个类
两个转义字符
-
\t:制表符(效果是加3个空格)
-
\n:换行
类型转换(强制转换、自动转换)
低------------------------------->高
byte,short,char -> int -> long -> float -> double
-
强制转换: 从高到低,是强制类型转换,方法:(类型)变量名
-
自动转换: 从低到高
注意:强制转换的时候,可能存在内存溢出,或者精度问题。
比如:int的范围是-2147483648-2147483647;而byte范围是-128-127;
变量作用域
-
类变量 (方法外面,类的里面,比实例变量多个static关键字。类变量随着类存在,也随着类消失)
-
实例变量 (方法外面,类的里面,从属于对象)
-
局部变量 (方法里面)
public class Demo1 {
static int num1 = 1; //类变量
int num2 = 2; //实例变量(方法外面,类的里面),从属于对象
public void method(){
int num3 = 3; //局部变量(方法里面,main()或者其他像这个method()就叫方法)
}
}
注意:
-
局部变量必须初始化才能用;
-
实例变量不初始化,使用时按默认值。
2.1. 数值类型默认为:0或0.0
2.2. 布尔值默认为:false
2.3. 除了基本类型,其余都默认为:null(比如String)
-
类变量就是在实例变量的基础上,加了个static关键字。类变量随着类存在,也随着类消失。
常量
-
final 变量类型 变量名 = 值;
-
常量名一般用大写字符。
-
static final 和 final static两种写法都行。
命名规范
-
-
常量:大写字母,下划线
-
方法名:首字母小写,驼峰原则
-
类名:首字母大写,驼峰原则 (就类名是首字母大写)
运算符
-
/:除
-
%:取余(也叫模运算),11%5 = 1
-
++自增运算。注意关键字:自增。它自己会变的。
public class Demo1 {
public static void main(String[] args) {
int a = 1;
int b = a++;
int c = ++a;
System.out.println(a); //3
System.out.println(b); //1
System.out.println(c); //3
}
}
-
幂运算:Math.pow(3,2)
-
三元运算符:x ? y : z 如果x是true,则结果为y;否则结果为z
public class Demo1 {
public static void main(String[] args) {
int age1 = 10;
int age2 = 20;
String type1 = age1<18 ? "未成年":"成年";
String type2 = age2<18 ? "未成年":"成年";
System.out.println(type1); //未成年
System.out.println(type2); //成年
}
}
-
判断字符串相等:
String s = "hello";
boolean judge_F_or_T = s.equals("hello");
字符串连接符
字符串连接符+
-
前面是字符串'' '',则后面也都转化成字符串,然后拼接
-
后面是字符串'' '',不执行
public class Demo1 {
public static void main(String[] args) {
int a = 1;
int b = 2;
System.out.println(""+a+b); //12
System.out.println(a+b+""); //3
}
}
获取用户输入
采用Scanner类,获取用户输入。在java.util.Scanner中。基本语法:
Scanner s = new Scanner(System.in);
读取前,一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。
nextLine()搭配hasNextLine()使用;next()搭配hasNext()使用。
因为next()遇到空格就断了,所以next()用的少,一般nextLine()用的多。
注意:用完Scanner一定要关掉:scanner.close(); IO流不关的话,占用资源。
next()与nextLine()有区别:
-
next():
1.1. 自动忽略有效字符前的空白。
1.2. 有效字符后,遇到空格,就结束。
1.3. 搭配hasNext()用。
-
nextLine():
2.1. 以Enter为结束,即:nextLine()方法返回的是回车之前的所有字符。
2.2. 可以获得空白。
2.3. 搭配hasNextLine()用。
Example:
-
next()搭配hasNext():
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
//创建扫描器对象
Scanner scanner = new Scanner(System.in);
System.out.println("使用next()接收:");
//hasNext()结合next():
if (scanner.hasNext()){
String str = scanner.next();
System.out.println("输出内容为:"+str);
}
//IO流的类不关的话,会一直占用资源
scanner.close();
}
}输入:Wang Zan
输出:Wang
-
nextLine()搭配hasNextLine():
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
//创建扫描器对象
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine()接收:");
//hasNextLine()结合nextLine():
if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("输出内容为:"+str);
}
//IO流的类不关的话,会一直占用资源
scanner.close();
}
}输入:Wang Zan
输出:Wang Zan
方法1:println(),输出完换行
System.out.println();
方法2:print(),输出完不换行,所以加个转义字符
System.out.print("\n");
if 多选择结构
一个if或者else if 判断为true,后面的else if 和else语句都直接跳过执行。
if(布尔表达式 1){
若布尔表达式 1为true执行代码
}else if(布尔表达式 2){
若布尔表达式 2为true执行代码
}else if(布尔表达式 3){
若布尔表达式 3为true执行代码
}else{
若以上布尔表达式都不为true执行代码
}
switch 多选择结构
-
switch语句中的变量类型可以是:byte、short、int、char、String。
-
case标签必须为字面量或字符串常量。
- 不写break会有贯穿效果。
public class HelloWorld {
public static void main(String[] args) {
int num = 2;
switch (num){
case 1:
System.out.println("输出1");
break;
case 2:
System.out.println("输出2");
break;
default:
System.out.println("输出3");
}
if(num < 2){
System.out.println("num < 2");
}else if(num < 3){
System.out.println("num < 3");
}else if(num < 4){
System.out.println("num < 4");
}else{
System.out.println("num > 4");
}
}
}
输出2
循环结构(while、for、用于数组的增强型for循环)
public class HelloWorld {
public static void main(String[] args) {
int num =1 ;
while (num<4){
num++;
System.out.println(num);
}
}
}
输出:2,3,4
2. for循环
快捷键:3.for,自动生成
public class HelloWorld {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.println(i);
}
}
}
输出:0,1,2
3. 用于数组的增强型for循环
for (int x : numbers)
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4}; //定义了一个数组
//遍历数组元素
for (int x : numbers) {
System.out.println(x);
}
}
}
continue和break的区别
-
break强行中断循环,跳出循环。
-
continue强行中断本次循环,不执行循环中continue后面语句,直接进入下一次循环。
public class HelloWorld {
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
if (i == 2) {
break;
}
System.out.println(i);
}
System.out.println("=================");
for (int i = 0; i < 4; i++) {
if (i == 2) {
continue;
}
System.out.println(i);
}
}
}
0
1
=================
0
1
3
Debug