理论知识 |
代码演示 |
Java文件: 源文件(.java)和字节码文件(.class) 通过编写源文件—>字节码文件,然后通过JVM运行字节码文件 当创建一个java文件后,如果此类前面有public修饰的话,类名字要和文件名相同;在一个文件中可以定义多个类,但是只能有一个类前面有public修饰其余都不可以有public |
public class review{ } class A{ } class B{ } |
Main方法: Public static void main(String [] args) main 方法是无返回类型的,所以在main 方法中是不可以有return关键字的 |
public static void main(String[] args) { } |
输出语句: System.out.print(); Print()和println()的区别: 第一个不带换行,第二个有换行。 如果说用print()想达到println()的效果,可以用“\n”来实现 |
System.out.print("\n"); System.out.println(); |
输入语句: 1. 先倒入一个包 2. 定义Scanner类的对象 3. 完成从控制台的输入 |
import java.util.*; 或 import java.util.Scanner; input.next(); //从控制台输入的字符串 input.nextDouble();//从控制台输入的双精度浮点型 |
数据类型: 1. 引用数据类型: 自定义类
2. 基本数据类型: 数值型: Int double 非数值型:String char Boolean
引用数据类型和基本数据类型的区别是: 引用数据类型有方法,而基本数据类型没有方法
|
1. String str = new String(“我是一个字符串”); String str1 = “我也是一个字符串”;
Str.equals(“想要比较的字符串”) Str.equalsIgnoreCase() //比较字符串是否相当,忽略大小写 Str.substring(); //截取字符串 Str.indexOf() //索引字符第一次出现的位置 Str.lastIndexOf() //索引字符最后一次出现的位置
自定义: Class A{自定义的类} A a = new A(); a.方法(); 2.int a = 123; Double d = 12.5; Boolean b = true; Char c = ‘a’; |
类型转换: 1.显示类型转换(强制类型转换) 从大的-->小的 2.隐式类型转换(自然转换) a.两种数据类型要兼容 b.从小的à大的 |
1. double d = 12.3; Int a = (int)d;
2. int b = 13; double d = b; |
运算符: 关系运算符、赋值运算符 算术运算符、逻辑运算符 |
关系运算符 > < >= <= !=(不等于)==(是否等于) 赋值运算符 = 算术运算符 + - * / %(求余) ++(自加1) --(自减1) 逻辑运算符 &&(并且) || (或者) !(非) |
分支结构: If- else if – else 条件判断的顺序要从小到大 或是 从大到小 Switch(表达式){ //int char Case 常量: //Int char 语句; Break; Default: 语句; Break; }
区别: If-else 是区间判断 Switch 是等值判断 |
int money = 1000; if(money > 100){ System.out.println("奔驰"); }else if(money > 50){ System.out.println("宝马"); }else if(money > 10){ System.out.println("雪弗来"); }else{ System.out.println("啥也不买,继续赚钱"); } int money = 1000; //注意变量的类型 switch(money){ case 100: System.out.println("奔驰"); break; case 50 : System.out.println("宝马"); break; case 10: System.out.println("雪弗来"); break; default: System.out.println("啥也不买,继续赚钱"); } |
循环结构: For、while、do-while 区别: 变量初始化 ① While(条件判断){ ② 语句; ③ 变量更新;//避免死循环④ }
变量初始化 ① Do{ 语句;② 变量更新;//避免死循环③
}while(条件判断); //注意有分号 ④
① ② ④ For(初始化;条件判断;变量更新){ 语句; ③ }
注意: 当初始值相同,条件判断相同的情况下,while 、do-while 、for的执行次数都是相同的, 但是当初始值第一次都不满足条件时,do-while至少执行一次 |
int i = 1; while(i<=5){ System.out.println("i=" + i); i++; } int i = 1; do{ System.out.println("i=" + i); i++; }while(i<=5);
for(int i=1; i<=5; i++){ System.out.println("i=" + i); } 注意:for循环里的三个表达式都可以省略,但“;”是不可以省略的 |
跳转关键字: Break 结束循环,跳出循环结构 Continue 结束当前循环,进入下一次循环
但是: 如果在循环结构中有switch结构,那么break先跳出的是switch结构 |
for(int i=1; i<=5; i++){ if(i+1 >4){ break; } } for(int i=1; i<=5; i++){ if(i+1 < 4){ continue; } System.out.println("看看我是什么时候能输出"); }
for(int i=1; i<=7; i++){ switch(i){ case 1: case 2: case 3: System.out.println("喝粥"); break; default: System.out.println("吃咸菜"); } } |
数组: 1.定义: 1. 数据类型 [] 变量名 = new 数据类型[分配空间长度];. 2. 数据类型 变量名[] = new 数据类型[分配空间长度]; 2. 赋值: 参看右侧代码
3. 数组求长度: Length (是属性)
4. 数组元素如何遍历: 通过下标索引数组中每一个元素 但注意: 数组的下标最大值是:数组长度-1 数组下标是从 0 开始
5. 数组排序: 参看右侧代码
6. 排序 参看右侧代码 |
1. 定义: Int [] arr = new int[5]; Int arr [] = new int[5];
2. 赋值: a. Int [] arr = new int[]{1,2,3}; b. Int [] arr = {1,2,3,4} c. Int [] arr = new int[4]; Arr[0] = 2; Arr[1] = 3; …… 3. 长度 Int [] arr = new int[]{1,2,3}; Arr.length; è 3
4. int [] arr = new int[]{1,2,3,4,5}; for(int i= 0; i<arr.length; i++){ System.out.println(arr[i]); } 5.//擂台法 int [] arr = new int[]{19,21,3,14,5}; int max = arr[0]; //设置一个擂台 max for(int i= 1; i<arr.length; i++){ if(arr[i] > max){ max = arr[i]; } } System.out.println("max=" + max);
6.排序 //正序输出 int [] arr = new int[]{19,21,3,14,5}; Arrays.sort(arr); //sort中加入要排序的数组名 for(int i= 0; i<arr.length; i++){ System.out.println(arr[i]); }
//倒序输出 int [] arr = new int[]{19,21,3,14,5}; Arrays.sort(arr); //sort中加入要排序的数组名 for(int i=arr.length -1; i>=0; i--){ System.out.println(arr[i]); } |
字符串: 1.字符串定义: String str = new String(“您好:”); String str = “好好学习”
2. 字符串的方法调用 参看右侧代码 |
方法调用演示 //字符串索引 String str = "hello_world"; int position = str.indexOf("o"); //索引的是第一次出现的字符串或是字符 System.out.println(position); //索引的字符串的下标从 0 开始计算
//字符串索引 String str = "hello_world"; int position = str.lastIndexOf("o"); //索引的是最后一次出现的字符串或是字符 System.out.println(position); //索引的字符串的下标从 0 开始计算
//字符截取 String str = "hello_world"; String newStr = str.substring(3,6); //第一个参数下标从0开始计算 第二个参数下标从1开始结算 System.out.println(newStr); à lo_
String str = "hello_world"; String newStr = str.substring(3); //第一个参数下标从 0 开始计算,将后面的所有字符都取出 System.out.println(newStr);àlo_world
//如何遍历字符串中的每一个字符 String str = "hello_world"; for(int i=0; i<str.length(); i++){ System.out.print(str.charAt(i) + "\t"); } System.out.println("\n-------------"); for(int i= 0 ; i< str.length(); i++){ System.out.print(str.substring(i, i+1) + "\t"); }
//字符连接 String str1 = "hello"; String str2 = "world"; String newStr = str1.concat(str2); //用concat连接之后,生产一个新的字符串
StringBuffer sb = new StringBuffer(); sb.append("hello"); sb.append("world"); //hello 和 world 都会加入sb对象之中,并不像String 类型的concat方法
StringBuffer 类型 à string类型 调用 toString() Sb.toString() |
类和对象: 1.类和对象的关系: 类是一类具有相同属性和方法的对象的集合,我们会将这类对象里的属性和方法提取出来,写入类中。 2.如何定义一个类 参看右侧代码 3.如何定义类的属性和方法 参看右侧代码 注意: 在定义类的方法的时候,方法的返回类型有两种情况: 1.无返回类型 void 那么此时,方法中不要出现return关键字 2有返回类型 String/Int …. 方法的返回类型,取决于return后面表达式的返回类型
◆ void 和 return 不可以同时出现在方法中
★ 方法是否带有参数: 1.方法所带的参数个数可以无限个,但彼此间要用 “,” 逗号来分隔, . 2. 方法所带的参数类型和方法的返回类型无关 3.方法定义的过程中,是否带参数和方法是否有返回值或无返回值,没有固定格式,是根据具体设计代码而定
4. 方法调用 如果在两个方法在一个类中,那么方法之间可以直接调用。 如果两个方法在两个类中,那么方法之间,必须先要实例化对象才可以调用
|
定义类: Public class 类名{} 建议类名的首字母大写
定义类的属性和方法 Public class Person{ Int age; String name;
Public String eat(){ Retrun “我喜欢吃米饭”; } }
方法返回类型: public int getAge(){ return 10; //因为return后面 10 是整数类型 所以此方法的返回类型要求Int型 }
public void show(){ //此方法为无返回类型 int a = 10; int b = 19; System.out.println("无返回类型"+ (a+b)); }
方法带参数 Public void showArg(int a, Char b, String c){ System.out.print(a + b + c); }
Public String showArg(int a, Char b, String c){ Return a + b + c; }
//同一个类中的方法调用 public class review{ public int a(){ return 1; } public int b(){ a(); //直接调用本类中的a方法 return 8; } }
//不同类中的调用 public class Hello{ public int a(){ return 1; } }
class Other{ public void b(){ Hello h = new Hello(); //先实例化对象 h.a(); //对象调用方法 } } |
包: 定义包: Package 包名
但注意,包的定义一定要在第一行 |
package com.accp.dlc; import java.util.*; public class Hello{ public int a(){ return 1; } } |
JAVA 基础
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.