- 一、static关键字
- 1.修饰成员变量 静态属性
- 案例
- 内存分析
- 2.修饰成员方法 静态方法
- 案例
- 3.修饰代码块 静态代码块
- 案例
- 二、异常
- 1.Java中的异常
- 2.Throwable类
- 3.错误和异常
- 4.异常
- 4.1异常的捕捉
- 案例
- 4.2异常的抛出
- 案例
- 5.自定义异常
- 案例
- 三、String类
- 1.String类型的两种的声明方式
- 1.1案例
- 1.2内存解析
- 2.字符串的比较
- 案例
一、static关键字
注意:static和类的实例化对象无关!
1.修饰成员变量 静态属性语法格式:
static 数据类型 变量名;
//调用格式 类.静态属性;
注意事项:
1.使用static修饰的变量叫静态变量。
2.代码中对象还没有创建的时候,如果加载了类,static修饰的属性已经存在了,和对象没有关系。
案例代码如下(示例):
package com.qfedu.a_static;
class Person {
String name;
int age;
static String country;
}
public class Demo {
public static void main(String[] args) {
Person sb = new Person();
sb.name = "张三";
sb.age = 23;
Person.country = "中国";
//The static field Person.country
//should be accessed in a static way
System.out.println(sb.country);//中国
System.out.println(Person.country);//in a static way
sb.country = "PRC";
System.out.println(Person.country);//PRC
}
}
内存分析
2.修饰成员方法 静态方法
语法格式:
public static 返回值 方法的名字 (参数列表) {
}
调用静态方法:
类.方法名字();案例
代码如下(示例):
package com.qfedu.a_static;
class Dog {
public void eat () {
System.out.println("普通的成员方法");
}
public static void sleep () {
System.out.println("睡吧不用看家了");
}
}
public class Demo {
public static void main(String[] args) {
Dog.sleep();
//Dog.eat();只能拿对象来调用这个方法eat
Demo3.test();
}
public static void test () {
System.out.println("嘻嘻");
}
}
3.修饰代码块 静态代码块
语法格式:
static {
语句体
}
只要这个类加载,那么静态代码块一定会执行。
执行顺序: 静态代码块->构造代码块->构造方法。
案例代码如下(示例):
package com.qfedu.a_static;
class Cat {
public Cat () {
System.out.println("无参构造方法");
}
{
System.out.println("构造代码块");
}
static {
System.out.println("静态的代码块");
}
}
public class Demo {
public static void main(String[] args) {
Cat cat = new Cat();
}
}
二、异常
1.Java中的异常
程序在自上而下执行的时候,发生了不可预期的事件,这个事件阻止程序的运行,这就是异常。
常见的异常如数组下标越界,类转换异常等。
Java给咱们封装很多的异常类,并且提供很优秀的异常处理机制。
2.Throwable类构造方法:
| 无参构造 | Throwable()构造一个新的可抛出的null作为其详细信息。 |
|---|---|
| 有参构造 | Throwable(String message)构造一个具有指定的详细消息的新的throwable。 |
方法:
| String | getMessage()返回此throwable的详细消息字符串。 |
|---|---|
| void | printStackTrace()将此throwable和其追溯打印到标准错误流。 |
| String | toString()返回此可抛出的简短描述。 |
Throwable类下面有两个子类,一个叫Error类,一个叫Exception类。
Error:是代表JVM本身的错误,程序员是通过代码解决不了的。
Exception:异常,代表程序在运行过程中,发生了不可预期的事件。此时可以使用Java提供的解决方案,将异常捕捉出来,让程序继续执行下去。
异常分为两种:
1.编译时异常
FileNotFoundException(找不到文件异常)
SQLException(SQL语句异常)
ClassNotFoundException(找不到类异常)
InterruptException
2.运行时异常
ArrayIndexOutOfBoundException(数组下标越界异常)
ClassCastException(类转换异常)
NullPointerException(空指针异常)
4.异常代码有可能会出现异常,Java给咱们提供了两种解决方案:
1.异常的捕捉
2.异常的抛出
4.1异常的捕捉在程序运行过程中,代码难免有可能会遇到异常。如果没有异常,代码正常执行,如果有异常,就捕捉异常。
语法格式:
try {
有可能出现异常的代码
} catch (异常对象) {
针对于异常的处理方案
}
案例
代码如下(示例):
package com.qfedu.c_Exception;
public class Demo {
public static void main(String[] args) {
test(3, 0);
}
public static void test (int a, int b) {
int ret = 0;
try {
//有可能个出现异常的代码
ret = a / b;
} catch (ArithmeticException e) {
System.out.println("123");
//打印错误信息
System.out.println(e.getMessage());
}
//即使代码有异常,通过捕捉以后。是不会影响程序的接着执行的代码的
System.out.println(ret);
System.out.println( 4 + 4);
}
}
4.2异常的抛出
如果使用异常抛出的话,一旦发生异常,从出现异常的地方会终止代码。
使用两个关键字:
throw: 是抛出异常的动作,给jvm一个异常的对象。
throws:在方法的声明处书写,告知当前调用者此处有异常的抛出。
案例代码如下(示例):
package com.qfedu.c_Exception;
import java.io.FileNotFoundException;
public class Demo {
public static void main(String[] args) throws Exception{
test(0);
Thread.sleep(1000);
}
public static void test (int a) throws FileNotFoundException{
if (a == 0) {
//编译时异常
throw new FileNotFoundException();
}
System.out.println("jvm xixi");
}
}
5.自定义异常
Java给咱们提供了很多的异常对象,但有的时候还是满足不了现实生活的需求,此时我们可以自定义一个异常对象。
案例代码如下(示例):
package com.qfedu.b_exception;
class SingleManException extends Exception {
public SingleManException () {}
public SingleManException(String message) {
super(message);//调用父类的有参构造
}
}
public class Demo {
public static void main(String[] args) {
try {
buy(false);
} catch (SingleManException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
public static void buy (boolean isSingle) throws SingleManException{
//true 是单身 false 不是单身
if (isSingle) {
throw new SingleManException("单身不能进店购买");
}
System.out.println("情侣买一送一,限制性别");
}
}
三、String类
1.String类型的两种的声明方式
1.1案例
代码如下(示例):
package com.qfedu.c_string;
public class Demo {
public static void main(String[] args) {
//声明一个字符串类型的数据
//第一种声明方式
//str1在栈区
//"今天吃顿好的" 字符串常量池
String str1 = "今天吃顿好的";
System.out.println(str1);
//第二种声明方式
//str2 在栈区
//"今天吃顿好的" 在堆区
String str2 = new String("今天吃顿好的");
System.out.println(str2);
}
}
1.2内存解析
2.字符串的比较
==:比较的是内存的地址和内容。
equals:先比较内存地址,再比较内容也返回true。
我们在开发中通常使用equals方法。
案例代码如下(示例):
package com.qfedu.c_string;
public class Demo {
public static void main(String[] args) {
//在字符串常量池
String str1 = "abc";
String str2 = "abc";
String str3 = new String("abc");
String str4 = new String("abc");
//== 比较的是内存的地址和内容
System.out.println(str1 == str2);//true
System.out.println(str1 == str3);//false
System.out.println(str3 == str4);//false
//以后用开发使用==合适不? 不合适因为咱们咱们只关注结果
//值是否一样,不要去管内存地址
//equals
System.out.println(str1.equals(str2));//true
System.out.println(str2.equals(str3));//true
System.out.println(str3.equals(str4));//true
}
}



