Java的关键字


1、 关键字是指在编程语言中,已经赋予了固定意义,不能随意使用的单词。
java的50个关键字:
sbstract、assert、boolean、byte、catch、char、class、dedault、do、double、else、
enum、extnds、final、finall、for、if、implements、import、instanceof、int、interface、
package、private、protected、public、return、strictfp、short、static、super、switch、
synchronized、this、throw、throws、transient、void、volatile、while
10个保留字:
break、case、const、continue、float、goto、long、native、new、try


2、访问权限
(访问权限宽松程度:   宽松到严格)
public:当前工程下的任何一个类文件中都可以访问;
protected:当前包下的任何一个类和不同包下的子类可以访问;
默认不写:当前包下的任何一个类可以访问;
private:仅仅在当前类里面可以访问。

访问权限修饰类:只能使用public和默认不写的访问你权限;
注意:一个java文件中可以有多个class类,但是只能有一个public修饰的类
 或者都用默认不写的访问权限

3、this和super
this:代表当前类的对象,不会固定某一个对象上。
  如果调用当前类的构造方法:this(参数列表);必须写在第一行

public class People {
public People(){
this("张三");
System.out.println("执行了无参的构造函数");


}

public People(String name){
this(10);
System.out.println("执行了有参构造函数");
}

public People(int age){public class People {
public People(String name){
super();//调用父类的无参的构造方法
}
public People(){

}

public String name;
public void study(){
System.out.println("人的study函数");
}
System.out.println("执行了有参构造函数");
}

public class Test {
public static void main(String[] args) {
People peo = new People();
}
}
运行结果:
执行了有参构造函数
执行了有参构造函数
执行了无参的构造函数


super:表示父类的对象,指代父类对象地址
(创建子类对象的时候,子类的构造方法会调用父类默认的无参的构造方法,创建父类对象,用于初始化父类
的属性和方法,所有继承越多的子类在创建对象时,会创建越多父类对象)
示例:
public class People {
public People(String name){
super();//调用父类的无参的构造方法
}
public People(){

}

public String name;
public void study(){
System.out.println("人的study函数");
}


public class Student extends People{

public Student(){
super();//调用父类(Perple)默认的无参的构造方法
}
public void study() {
super.study();
this.name = "王五";
super.name = "李四";
System.out.println("Student的学习方法");
}

public class Test {
public static void main(String[] args) {
Student stu = new Student();
stu.study();
System.out.println(stu.name);
}
运行结果:
人的study函数
Student的学习方法
李四

父类中name属性,子类和父类相关联,所以用父类对象对name赋值,子类的name值也会改变


4、final:最终的,表示不能再修改
可以修饰的范围:类、方法、属性、局部变量
final类:代表当前类不能被继承(不能当父类用)
final方法:代表子类不能重写父类的方法
final属性:只能被初始化一次值(常量),而且必须要初始化好
final局部变量:在当前局部变量使用的范围内,不能再被修改

5、static:静态的
可以修饰的范围:方法、属性、静态块
static关键字只能用于修饰成员变量,不能用于修饰局部变量;
(静态属性和方法:在加载类的时候加载到内存当中
静态块:在加载静态属性之后,加载静态方法之前执行)
示例1:
public static void main(String[] args) {
Person.country="Amarica";
// Person p1=new Person();
// Person p2=new Person();


System.out.println(Person.country);
}
static {System.out.println("测试类的静态代码执行了");}

}


class Person{
static String country;

static{ country="China";
System.out.println("Person类中的静态方法执行了");
System.out.println(country);}

}
运行结果:
测试类的静态代码执行了
Person类中的静态方法执行了
China
Amarica



示例2:
public class Student {
// 静态块:加载类的时候自动执行
static {
System.out.print(1);
}
public static void play() {
System.out.print(2);
}
// 代码块:创建对象的时候自动执行
{
System.out.print(3);
}
public Student() {
System.out.print(4);
}
public void study() {
System.out.print(5);
}
}
public class Test {
public static void main(String[] args) {
Student stu1 = new Student();
Student stu2 = new Student();
stu1.study();
stu1.play();
stu2.study();
stu2.play();
}


}
运行结果:134345252

6、其他关键字
break:跳出当层循环
return:结束当前方法
continue:结束本次循环,进入下一次循环

示例:
public class Test {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if(j==1){
continue;//break;//return;
}
System.out.println(j);
}
}

运行结果:
0
2
0
2

7、八大基本数据类型
java类中:引用(类)类型、基本数据类型
引用(类)类型:String \JFrame\JLabel\Math
基本数据类型:int float double char long short byte boolean


八大基本数据类型都会对应有一个类类型:
Integer、Float Double Character Long Short Byte Boolean






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值