初学java--关键字

何为java关键字?最直接的描述就是eclipse上的代码用红色加重的单词---我们用专业术语称之为关键字!关键字是电脑语言里事先定义的,有特别意义的标识符,也就是说,每个关键字都有其特定的作用和适用范围。了解每个关键字的作用对学习一门计算机语言还是有很大的重要性滴。对于java究竟有多少个关键字呢大家说法不一,百度百科上面是45个,老师给的是48个,。也有说56个的,其实最常用的也就那么四十来个,对于让大家很纠结的十来个用得比较少,暂不讨论。

首先,让我们来看一下传说中的关键字的庐山真面目:
public  protected    private    static    final     this    class   extends  abstract  super
interface  implements  void       if      else      for     while    do       true     false
break     switch       case     default   continue  byte     char    int      short    long
float      double      boolean instanceof  catch    try     fianlly  new     import    null  
return     enum        goto       package throws  throw   assert   native  volatile 

根据每个关键字的作用我们稍微进行一下分类:
1.访问修饰符:
private    表示私有的,只能自己使用。顾名思义,私有的,也就是说不能给别人用的,在一个类里面定义的属性、访法若是用的private修饰,也就只能当前的类可以调用。例如通常定义一个类的属性的时候用private:
private 数据类型 属性名;
protected  表示受保护的,是同包下不同类可以访问。最大的范围是同一个包下的不同类之间的访问。
public     表示公有的,所有的类都可以访问。最大的范围是同一个project下的不同类之间可以访问。
2.基本数据类型:
int        整型,定义一个整形的属性时,用 int 属性名;
short      短整型
char       字符型
float      单精度浮点型
double     双精度浮点型
boolean    布尔类型
long       长整型
byte       字节型
类型              包含                默认值            大小                范围
boolean   true或false          false            1位                  - ------
char          unicode字符        \uxxxx       1 6位               u0000--\uffff
byte          有符号整数            0                8位                      -128 ___127
short          有符号整数          0                 16位                     -32768__32768
int              有符号整数          0                  32位               -214783648___2147483648
long          有符号整数            0                 64位         -9223372036854775808___9223372036854775808
float             IEEE浮点          0.0              32位     +-1.4E-45 ____+-3.4028235E+38
double          IEEE浮点          0.0             64位  -1.79769313486232E308 到 -4.94065645841247E-324
4.94065645841247E-324 到 1.79769313486232E308
3.关于类、接口、抽象类、包的关键字:
class       定义类的关键字,定义类的一般格式为:
public class 类名{}
extends     用来继承的关键字,继承类的一般格式为:
public class 子类名 extends 父类名{}
interface   定义接口的关键字,定义接口的一般格式为:
public interface 接口名{}
implements  实现接口的关键字,实现接口的一般格式为:
public class 类名 implements 接口名{}
abstract    定义抽象类和抽象方法的关键字,定义抽象类的一般方法为:
public abstract class 类名{}
static      静态的(方法、属性)最常见的就是主函数里面的 public static void main(String[] args){};static:主要是用来修饰方法和常量 以及内部类
用static定义的方法:表示该方法不需要创建对象就可以直接用类名调 :
public class test {  
 
    public static void main(String[] args) {  
        print();
    }  
//输出的方法
public static void print(){
System.out.print("*****************");
}
}

final       修饰方法的时候不能进行重写。
            修饰常量的时候不能改变常量的值
            修饰变量的时候不能改变变量的值
            修饰类的时候表示该类不可以被继承,比如以下代码:
public  final class test {  
    public static void main(String[] args) {  
        print();
    }  
//输出的方法
public static void print(){
System.out.print("*****************");
}
}

public class change extends test {

}
事实上当chang类若继承test时,电脑会报错The type change cannot subclass the final class test

package     定义包的关键字
import      引入包的关键字,引用某个包的时候,亦可以直接用“包名 对象名=new 包名();”这种格式 ,比如画板中的按钮的时候:
javax.swing.JButton Hchess=new javax.swing.JButton("直线");
4.条件和循环的关键字:
if  判断条件,如果满足择执行if内部的代码:
public  final class test {  
  //程序入口
    public static void main(String[] args) {  
        double i = 5.0;  
        double j = 1 / 4 + 3 / 4 + i + 12 / 6.0 + 3 / 4 + 1 / 4;  
       if(i ==5.0){ System.out.println(j);  
        print();
       } 
      }
   
//输出的方法
public static void print(){
System.out.print("*****************");
}
}
输出结果为:
7.0
*****************
else 一般和if在一起用,表示出了if之外的所有情况:
public  final class test {  
  //程序入口
    public static void main(String[] args) {  
        double i = 4.0;  
        double j = 1 / 4 + 3 / 4 + i + 12 / 6.0 + 3 / 4 + 1 / 4;  
       if(i ==5.0){ System.out.println(j);  
        print();
       } else{
        print();
       }
      }
   
//输出的方法
public static void print(){
System.out.print("*****************");
}
}
输出的结果为:
*****************
switch和case default 是也是条件判断的关键字switch的用法是判断case后面的表达式和switch后面的表达式是否相匹配,一旦case匹配,就会顺序执行后面的程序代码:
public class Test7 {
//程序入口
public static void main(String[] args)
{
  int i=5;
  switch(i)
  {
  case 1:
   System.out.println("one");
  case 10:
   System.out.println("ten");
  case 5:
   System.out.println("five");
  default:
   System.out.println("other");
  }
}
}

输出结果为:
five
other



for 循环的关键字,一般格式为for(){},小括号里面为循环的控制,包括初始值,循环次数等,大括号为每循环一次执行的代码:
public  final class test {  
  //程序入口
    public static void main(String[] args) {  
        double i = 4.0;  
        double j = 1 / 4 + 3 / 4 + i + 12 / 6.0 + 3 / 4 + 1 / 4;  
       if(i ==5.0){ System.out.println(j);  
        print();
       } else{
       for(int k=0;k<5;k++){
       print();
       }
      }
      }
   
//输出的方法
public static void print(){
System.out.println("*****************");
}
}
输出结果为:
*****************
*****************
*****************
*****************
*****************
while和do为另一种判断方法,while是当满足条件时执行代码,类似if:
public class Test2{
public static void main(String agrs[]) {
  int sum = 0;
  int sum1 = 0;
  int sum2 = 0;
  int i=1;
  while(i<101){
  
   sum+=i;
   i++;
  }
  System.out.println(sum);
}
}输出结果为:5050

break   如果循环中遇到了break,会结束当前循环,执行循环后边的语句,比如:
for(int k=0;k<5;k++){
       if(k==3){ 
    break;
    }else{
    System.out.println("*****************"+k);
    }
输出结果为:
*****************0
*****************1
*****************2

continue  与break同为退出循环语句,但continue为退出本次循环:
for(int k=0;k<5;k++){
       if(k==3){ 
    continue;
    }else{
    System.out.println("*****************"+k);
    }
    }
输出结果为:
*****************0
*****************1
*****************2
*****************4    
5.异常的关键字(目前不懂。。。。):
throws  throw catch    try     fianlly
6.其他常用关键字:
new关键字,用来新建一个对象的关键字,一般格式为:类名 对象名=new 类名();
this关键字,简单地说,就是表示我的,专业一点叫做当前的,在类里面,可以用this调用当前类的方法:
public class WZQUI extends javax.swing.JFrame{
//程序入口
public static void main(String [] args){
//新建一个界面对象
WZQUI wzq=new WZQUI();
wzq.showUI();
}
//显示界面的方法
private void showUI() {
this.setTitle("五子棋");
this.setSize(Config.HIGHT, Config.HIGHT);
this.setBackground(java.awt.Color.GRAY);
this.setDefaultCloseOperation(3);
this.setResizable(false);
this.setLocation(300, 30);
this.setVisible(true);
}
}这样就可以显示一个窗体!
null 空关键字,用来描述一个不确定的对象
还有保留关键字 goto 以及其他不常用的关键字,暂不总结

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值