Java基础学习之关键字

eclipse中(默认情况下):

黑色细体字: 常用方法名、常用变量名、局部变量名

红色粗体字:关键字

1、关键字定义:一些已经赋予了固定含义的单词就是关键字

2、注意:关键字,是不能被用于定义类名、变量名、方法名等

3、Java中有50个关键字

1)跟类相关的关键字:

classabstractinterfaceextendsimplements、packageimport

2)跟访问权限相关:

publicprivateprotected

3)跟方法相关:

voidreturn

4)基本数据类型:

 byte 、shortint、long、float 、double 、char 、boolean

byte(1个字节,占8位)

short(2个字节,占16位)

int(4个字节,占32位)

long(8个字节,占64位)

float(4个字节,占32位)

double(8个字节,占64位)

char(2个字节,占16位)

5)跟循环判断相关:

for 、switch、 if 、else、 break 、case、 continue、 default 、while 、do

6)异常相关:trycatchfinallythrow、throws

7)其他的:instanceof  、this 、super、 finalsynchronizedenumstatic

4、Java中数据类型:基本数据类型、引用类型

基本数据类型:只有8byte short int long float double char boolean

引用类型:也称为类类型(所有以类方式定义的类,全部都是引用类型)

JFrameStringArrayList

JFrame jf;//定义一个引用,这个引用是JFrame类型的

基本数据类型:没有方法和属性调用,只是用于存储基本变量值

引用类型,有方法和属性调用,执行复杂的操作

强调:八大基本数据类型每个类型都会对应有一个引用类型

byte -> Byte

short->Short

int  -> Integer

long -> Long

float->Float

double->Double

char -> charactor

boolean->Boolean

5、访问权限

1)修饰属性、方法和类的访问范围的一个修饰符分类:

public  protected 默认不写private

2)属性和方法的权限范围总结

public:访问权限最大,整个工程随意访问使用

     如果在不同的包中使用,只需导入包即可

protected:只能在同一个包下类使用,不同包的子类可以继承它

       A、不同包(只能继承使用)

       (1)属性:但是只能通过set和get方法来改变和得到该属性的值

       (2)方法:可以通过重写该方法,在重写的方法的第一行super.方法名();来调用父类的方法。也可以           在子类的构造方法中的第一行super.方法名();来调用父类的方法

例子如下:

package com.test.exercise1;

public class Student {
	private String name;

	public Student() {

	}

	protected void teath() {
		System.out.println("老师...");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
public Teacher() {
		super.teath();// 调用父类的teach()方法
		this.teath();// 调用子类重写的teach()方法
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.test.exercise1.Student#teath()
	 */
	protected void teath() {
		super.teath();// 调用父类的teach()方法
		System.out.println("学生...");
	}

	public static void main(String[] args) {
		Teacher tea = new Teacher();
		tea.setName("李四");// 改变继承来的protected的name属性的值为“李四”
		System.out.println(tea.getName());// 获取name的值
	}
}
结果:

       B、同包(不管继承不继承) 

       (1)属性:但是只能通过set和get方法来改变和得到该属性的值

       (2)方法:可以直接通过子类的对象调用父类的方法。也可以在子类的构造方法中的第一行super.方法名();来调用父类的方法。还可以可以通过重写该方法,在重写的方法的第一行super.方法名();来调用父类的方法  例子:

public class Student {
	private String name;

	public Student() {

	}

	protected void teath() {
		System.out.println("老师...");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
package com.test.exercise1;

public class Teacher extends Student {
	public static void main(String[] args) {
		Teacher tea = new Teacher();
		tea.setName("李四");// 改变继承来的protected的name属性的值为“李四”
		System.out.println(tea.getName());// 获取name的值
		tea.teath();// 可以直接通过对象调用teach()方法
	}
}
结果:

          

默认不写:只能在同一个包下类使用(不管继承不继承)

1)属性:都可以通过对象名直接访问

2)方法:都可以通过对象名直接访问

例子:

package com.test.exercise1;

public class Student {
	String name;

	public Student() {

	}

	protected void teath() {
		System.out.println("老师...");
	}
}
package com.test.exercise1;

public class Teacher extends Student {
	public static void main(String[] args) {
		Teacher tea = new Teacher();
		tea.name = "李四";// 改变继承来的默认不写的name属性的值为“李四”
		System.out.println(tea.name);
		tea.teath();// 可以直接通过对象调用teach()方法
	}
}

结果:


private:访问权限最小,只能在当前类自己内部使用

3)类:

只能用public默认不写方式修饰

6、final和instanceof

1)final:

(1)可以修饰:类、方法、属性、参数

   1.修饰类:不能被继承了(不能作为父类)

          不能用final修饰抽象类和接口(语法会错误)

   2.修饰方法:不能被重写了

   3.不能修饰抽象方法(即final和abstract不能同时使用)

(2)修饰属性:表示属性是常量,必须要初始化值(直接赋值和构造方法传值)

例子:

package com.test.exercise3;

public class Student {
	public final int age = 10;// 直接赋值
	public final String name;

	/*
	 * 构造函数赋值
	 */
	public Student(String name) {
		this.name = name;
	}
}
package com.test.exercise3;

public class Teacher {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		 * 每一个类都会有一个默认的无参构造方法(可以不写就有),但是重载了构造方法, 如果不写无参的构造方法,该类中就没有无参构造方法了
		 */
		Student stu = new Student("李四");
		System.out.println(stu.name);
	}

}
结果:

(3)修饰参数:代表参数在当前方法体内,不能被修改值

例子:

package com.test.exercise_1;

public class Student {
	public String sex;

	/*
	 * final修饰参数,在这个方法内不能改变这个参数的值
	 */
	public void teach(final String name) {
		System.out.println(name + "在上课...");
	}
}
package com.test.exercise_1;

public class Teacher {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Student stu = new Student();
		stu.teach("李四");// 只能通过调用来改变参数的值
	}

}

结果:



2)instanceof:判断当前对象的转型子类对象是什么类型

格式:boolean 变量名= 父类对象instanceof  子类类型名

例如:

package com.test.exercise_2;

public class Student {
	public String name;
	public String sex;

	public void study() {
		System.out.println("学生在学习...");
	}
}
package com.test.exercise_2;

public class UnStudent extends Student {
	@Override
	public void study() {
		System.out.println("大学生在上课...");
	}
}
package com.test.exercise_2;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Student stu = new Student();
		UnStudent us = new UnStudent();
		UnStudent s = new UnStudent();
		if (stu instanceof UnStudent) {
			System.out.println("我是父类的对象...");
		} else if (us instanceof UnStudent) {
			System.out.println("我是自动转型对象...");
		} else if (s instanceof UnStudent) {
			System.out.println("我是子类对象");
		}
	}

}
结果:


7、循环语句

判断语句:if判断  switch判断

循环结构:for循环、while循环、do...while循环 

do...while循环:无论什么前提下,一定会由一次循环执行

switch:只支持intString判断

8、this、super

this:当前类的对象

super:父类的对象

1)this:可以调用当前类的属性和方法(普通方法和构造方法)

1.调用普通方法:this.方法名();

2.调用构造方法:this();

注意this调用当前类构造方法

                  1)只会在构造方法中调用其他构造方法

    2)调用构造方法必须写在当前方法体的第一行 

例如:(如果调用多个构造方法可以参考如下)

package com.test.exercise_2;

public class Student {
	public String name;
	public String sex;

	public Student() {
		this("王五");
		System.out.println("我是无参的构造方法...");
	}

	public Student(String name) {
		this("李四", "女");
		this.name = name;
		System.out.println(name + "我是带一个参数的构造方法...");
	}

	public Student(String name, String sex) {
		this.sex = sex;
		System.out.println(name + sex + "我是带两个参数的构造方法...");
	}

	public void study() {
		System.out.println("学生在学习...");
	}
}
package com.test.exercise_2;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Student stu = new Student();
		stu.study();
	}

}
结果:


2)super可以调用父类的属性和方法(普通方法和构造方法)

1.调用普通方法:super.方法名();

2.调用构造方法:super();

注意

    1、一般方法重写,都会在第一行加上super.重写方法名();用于对父类方法的过程补充

    2、子类的构造方法一定会调用父类的构造方法用于初始化父类对象,并继承父类对象的属性和方法

    3、调用父类构造方法必须写在当前方法体的第一行

例子:

package com.test.exercise_2;

public class Student {
	public String name;
	public String sex;

	public Student() {
		this("王五");
		System.out.println("我是无参的构造方法...");
	}

	public Student(String name) {
		this("李四", "女");
		this.name = name;
		System.out.println(name + "我是带一个参数的构造方法...");
	}

	public Student(String name, String sex) {
		this.sex = sex;
		System.out.println(name + sex + "我是带两个参数的构造方法...");
	}

	public void study() {
		System.out.println("学生在学习...");
	}
}
package com.test.exercise_2;

public class UnStudent extends Student {
	public UnStudent() {

	}

	@Override
	public void study() {
		super.study();// 调用父类的方法
		System.out.println("大学生在上课...");
	}
}
package com.test.exercise_2;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		UnStudent stu = new UnStudent();// 调用了父类的无参构造方法
		stu.study();
	}

}
结果:


9、static

static静态的

修饰:类(内部类)、方法、属性、静态块

注意:静态修饰的属性和方法,初始化的时间和同步

     成员(普通)属性和方法:初始化时间和对象同步

  (1)静态块:static{}

  (2)非静态代码块:{}


A、类加载时,静态的所有东西被放到static静态空间中,静态块直接执行

B、静态的属性和方法可以直接通过类名调用

C、定义引用时,引用放到栈中

D、创建对象时,非静态的所有东西放到堆空间中,非静态块直接执行,然后构造方法执行

E、通过对象调用属性和方法

F、静态的东西只有一份,只有在类加载时的那一份,不管是类名调用还是不同的类创建的对象调用,其实调用的都是同一个空间里的同一个东西,所以最后一次调用属性(方法)改变的值是最终的值

执行顺序(与代码顺序无关):静态块>非静态块>构造方法>其他

静态的东西跟类同步,非静态的跟对象同步

例子:

package com.test.exercise2;

public class Student {
	public static String name;

	public static void play() {
		System.out.println("我是静态方法");
	}

	static {
		System.out.println("我是静态块");
	}

	public Student() {
		System.out.println("我是构造方法");
	}

	{
		System.out.println("我是非静态块");
	}

	public void study() {
		System.out.println("我是普通方法");
	}
}

package com.test.exercise2;

public class Teacher {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Student stu1 = new Student();
		Student stu2 = new Student();
		stu1.study();

		Student.name = "李四";
		stu1.name = "张三";
		stu2.name = "王五";

		System.out.println(Student.name);
		System.out.println(stu1.name);
		System.out.println(stu2.name);
		Student.play();
	}

}

结果:


10、return、break、continue的区别
1)return:退出当前方法

例子:

package com.test.exercise_5;

public class ReturnTest {
	public static void main(String[] args) {
		new ReturnTest().returnTest();
		System.out.println("run");
	}

	public void returnTest() {
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				System.out.println(j);
				return;// 当i等于0时,执行第二层循环,遇到return,不管后面的代码如何,都不执行,直接退出这个returnTest()方法
			}
			System.out.println(i);
		}
	}
}
结果:



2)break:退出离它最近的一层循环

例子:

package com.test.exercise_5;

public class BreakTest {
	public static void main(String[] args) {
		new BreakTest().breakTest();
		System.out.println("run");
	}

	public void breakTest() {
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				System.out.println(j);
				break;// 当执行第二层循环,遇到break,退出离它最近的for循环,继续执行
			}
			System.out.println(i);
		}
	}
}
结果:


3)continue:退出当前次循环,开始下一次循环

例子:

package com.test.exercise_5;

public class ContinueTest {
	public static void main(String[] args) {
		new ContinueTest().continueTest();
		System.out.println("run");
	}

	public void continueTest() {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				if (j == 2) {
					continue;// 当执行第二层循环,遇到continue,退出j=2的这次循环,继续下一次循环执行
				}
				System.out.println(j);
			}
			System.out.println("running");
		}
	}
}
结果:


11、|、||、&、&&的区别

1)|:非中断或

(boolean)|(boolean)|(boolean)|...

它会计算出所有的boolean值

例子:

package com.test.exercise_5;

public class Exercise {
	public static boolean One() {
		System.out.println("run...1");
		return false;
	}

	public static boolean Two() {
		System.out.println("run...2");
		return true;
	}

	public static boolean Three() {
		System.out.println("run...3");
		return true;
	}

	public static boolean Fore() {
		System.out.println("run...4");
		return true;
	}

	public static void main(String[] args) {
		Exercise e = new Exercise();
		if (e.One() | e.Two() | e.Three() | e.Fore()) {
			System.out.println("run...");
		}
	}
}
结果:



2)||:中断或

(boolean)||(boolean)||(boolean)||...

从左往右依次计算,直到遇到第一个为true就停止计算,返回true

package com.test.exercise_5;

public class Exercise {
	public static boolean One() {
		System.out.println("run...1");
		return false;
	}

	public static boolean Two() {
		System.out.println("run...2");
		return true;
	}

	public static boolean Three() {
		System.out.println("run...3");
		return true;
	}

	public static boolean Fore() {
		System.out.println("run...4");
		return true;
	}

	public static void main(String[] args) {
		Exercise e = new Exercise();
		if (e.One() || e.Two() || e.Three() || e.Fore()) {
			System.out.println("run...");
		}
	}
}


3)&:非中断且

(boolean)&(boolean)&(boolean)&...

它会计算出所有的boolean值

package com.test.exercise_5;

public class Exercise {
	public static boolean One() {
		System.out.println("run...1");
		return false;
	}

	public static boolean Two() {
		System.out.println("run...2");
		return true;
	}

	public static boolean Three() {
		System.out.println("run...3");
		return true;
	}

	public static boolean Fore() {
		System.out.println("run...4");
		return true;
	}

	public static void main(String[] args) {
		Exercise e = new Exercise();
		if (e.One() & e.Two() & e.Three() & e.Fore()) {
			System.out.println("run...");
		}
	}
}

结果:


4)&&:中断且

从左往右依次计算,直到遇到第一个为false就停止计算,返回false

package com.test.exercise_5;

public class Exercise {
	public static boolean One() {
		System.out.println("run...1");
		return false;
	}

	public static boolean Two() {
		System.out.println("run...2");
		return true;
	}

	public static boolean Three() {
		System.out.println("run...3");
		return true;
	}

	public static boolean Fore() {
		System.out.println("run...4");
		return true;
	}

	public static void main(String[] args) {
		Exercise e = new Exercise();
		if (e.One() && e.Two() && e.Three() && e.Fore()) {
			System.out.println("run...");
		}
	}
}
结果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值