• Java keywords


    Java keywords

    Java keywords are also known as reserved words. Keywords are particular words which act as a key to a code. These are predefined words by Java so it cannot be used as a variable or object name.

    List of Java Keywords

    1. abstract

       abstract class Employee{
           abstract void work();
       }aa

      abstract is used to declare abstract class. It can have abstract and non-abstract method.

    2. boolean

      boolean is used to declare a variable ad a boolean type. It can hold True and False Only.

    3. break

      break is used to break loop or switch statement. It breaks the current flow of the program.

    4. byte

      byte is used to declare a variable that can hold an 8-bit data values.

    5. case

       switch(expression){
           case value1:
               //code to be executed
               break;
           case value2:
               //code to be executed
               break;
           default:
               //code to ececuted if all cases are not matched
       }

      case is used to with the switch statements to mark blocks of test.

    6. catch

       try{
           //code that may throw an exception
       }catch(Exception_class_name ref){
           
       }

      catch is used to catch the exception generated by try statement.

    7. char

      char is used to declare a variable that can hold unsigned 16-bit Unicode characters.

    8. class

      class is used to declare a class.

    9. continue

      continue is used to continue to the loop (skips the remaining code).

    10. default

      default is used to specify the default block of code in a switch statement.

    11. do

       do{
           //code to executed
       }while(condition);

      do is used to declare a do-while loop.

    12. double

      double is used to declare a variable that hold a 64-bit floating -point numbers.

    13. else

      else is used to indicate the alternative branches in an if statement.

    14. enum

       public class EnumExample1{
           //defining the enum inside the class
           public enum Season{
               WINTER,SPRING,SUMMER,FALL
          }
           
           //main method
           public static void main(String[] args){
               //traversing the num
               for(Season s: Season.values()){
                   System.out.println(s);
              }
          }
       }

      enum (enumerate) is used to fixed set of constants.

    15. extends

       public Subclass-name extends Superclass-name{
           //methods and fields
       }

      extends is used to indicate that a class derived from another class.

    16. final

      • final variable

       public class Bike9{
           final int speedlimit = 90; //final variable
           
           public void run(){
               speedlimit = 400;
          }
           
           public static void main(String[] args){
               Bike9 obj = new Bike9();
               obj.run();
          }
       }
       C:UsersdreamDesktopMarkdownn学习Java基础	est>javac Bike9.java
       Bike9.java:5: error: cannot assign a value to final variable speedlimit
              speedlimit = 400;
              ^
       1 error
       
      • final method

      class Bike{  
        final void run(){	//final method
      	  System.out.println("running");
        }
      }  
           
      class Honda extends Bike{  
         void run(){
      	   System.out.println("running safely with 100kmph");
         }  
           
         static void main(String args[]){  
      	   Honda honda= new Honda();  
      	   honda.run();  
         }  
      }  
      C:UsersdreamDesktopMarkdownn学习Java基础	est>javac Bike.java
      Bike.java:8: error: run() in Honda cannot override run() in Bike
         void run(){
              ^
        overridden method is final
      1 error
      • final class

      final class Bike{}  
        
      class Honda1 extends Bike{  
        void run(){System.out.println("running safely with 100kmph");}  
          
        public static void main(String args[]){  
        Honda1 honda= new Honda1();  
        honda.run();  
        }  
      }  
      C:UsersdreamDesktopMarkdownn学习Java基础	est>javac Bike.java
      Bike.java:20: error: cannot inherit from final Bike
      class Honda1 extends Bike{
                           ^
      1 error

      final is used to indicate a variable, method, or class. these would be restrict to use.

    17. finally

      finally is used to indicate a block of code in a try-catch structure. the block is always executed.

    18. float

      float is used to declare a variable that can hold a 32-bit floating-point number

    19. for

      for(initialization;condition;Increment/Decrement){  
      	//statement or code to be executed  
      } 

      for is used to start a for loop

    20. if

      if is used to test condition, it executes the if block if condition is true.

    21. implements

      interface printable{  
      	void print();  
      }  
      
      class A6 implements printable{  // implements interface
          public void print(){
              System.out.println("Hello");
          }  
      
          public static void main(String args[]){  
          A6 obj = new A6();  
          obj.print();  
       	}  
      }  
      C:UsersdreamDesktopMarkdownn学习Java基础	est>java A6
      Hello

      implements is used to implement an interface.

    22. import

      import makes class and interfaces available and accessible to the current source code.

    23. instanceof

      class Simple1{
          public static void main(String[] args){
              Simple1 s = new Simple1();
              System.out.println(s instanceof Simple1);//true
          }
      }
      C:UsersdreamDesktopMarkdownn学习Java基础	est>java Simple1
      true

      instanceof is used to test whether the object is an instance of the specified class or interface

    24. int

      int is used to declare a variable that can hold a 32-bit signed integer

    25. interface

      interface is used to declare a interface. It can have only static constants and abstract method.

    26. long

      long is used to declare a variable that can hold a 64-bit signed integer.

    27. native

      native is used to specify that a method is implemented in native code using JNI(Java Native Interface)

    28. new

      new is used to create an instance of the class or array.

    29. null

      null is used to indicate that a reference does not refer to anything.

    30. package

      //save as Simple.java
      
      package mypack;
      public class Simple{
          public static void main(String[] args){
              System.out.println("Wellcome to Package");
          }
      }
      C:UsersdreamDesktopMarkdownn学习Java基础	est>javac -d . Simple.java

      image-20200403151814623

      image-20200403152017083

      C:UsersdreamDesktopMarkdownn学习Java基础	est>java mypack.Simple
      Wellcome to Package

      package is used to declare a Java package that includes the classes.

    31. private

      private is an access modifier. it is used to indicate that a method or variable may be accessed only in the class in which it is declared.

    32. protected

      protected is an access modifier. it can be assigned to variables, methods, and inner classes.

    33. public

      public is an access modifier. it is used to indicate that an item is accessible anywhere.

    34. return

      return is used to return from a method when its execution is complete.

    35. short

      short is used to declare a variable that can hold a16-bit integer.

    36. static

      static is used to indicate that a variable, method, block, or nested class belong to class area than heap memory(where store instance of class).

    37. strictfp

      strictfp class A{}	//stricfp applied on class
      strictfp interface M{}	//stricfp applied on interface
      class A{
          strictfp void m(){}	//stricfp applied on method
      }

      strictfp ensure that you will get the same result on every platform if you perform operations in the floating-point variable. it is used to applied on method, class or interface.

    38. super

      super is a reference variable which is used to refer Immediate parent class object.

    39. switch

      switch is used to indicate a switch statement that tests the quality of a variable against multiple values.

    40. synchronized

      synchronized is used to specify the critical sections or methods in multithreaded code.

    41. this

      this is used to refer the current object in a method or constructor.

    42. throw

      public class TestThrow1{  
         static void validate(int age){  
           if(age<18)  
            throw new ArithmeticException("not valid");  
           else  
            System.out.println("welcome to vote");  
         }  
         public static void main(String args[]){  
            validate(13);  
            System.out.println("rest of the code...");  
        }  
      }  

       

      throw is used to explicitly throw a custom exception.

    43. throws

      throws is used to declare an checked exception.

    44. transient

      transient is declare a variable that is transient, and will not be serialized.

    45. try

      try is used to start a block of code that will be tested for exceptions.

    46. void

      void is used to specify that a method does not have a return value.

    47. volatile

      volatile is used to indicate that a variable may change asynchronously.

    48. while

      while is used to start a while loop.

    Identifiers

    Java Identifiers are the name of class, package, constant, method, etc.

    The two key rules of all identifiers

    • The name must not contain any white space

    • The name should not start with special characters like &(ampersand), $(dollar), _(underscore).

    1. Class

    public class Employee{	// Class name
        
    }

    2. Interface

    interface Printable{ //Interface name
        
    }

    3. Method

    public class Employee{
        public void draw(){	//Method name
            
        }
    }

    4. Variable

    public class Employee{
        int id; //Vriable name
    }

    5. Package

    package com.casino;	//Package name
    public class Employee{
        
    }

    6. Constant

    public class Employee{
        static final int MIN_AGE = 18;	//Constant name
    }
     
  • 相关阅读:
    HDU4477 Cut the rope II 递推
    HDU4571 Travel in time 动态规划
    在VS2010中使用$err,hr快速查看当前GetLastError()的值
    对C语言的volatile关键字的理解
    三星S6D1121主控彩屏(240*320*18bit,262K)图形设备接口(GDI)实现
    C语言创建二叉树数据结构, 以及各种遍历
    Ubuntu下使用Dr.com宽带客户端上网的步骤
    8051单片机学习笔记/概要/总结/备忘
    联想笔记本电脑Ubuntu系统下触摸板的锁定
    [MSP430] 集成开发环境 IAR Embedded Workbench for MSP430 5.50
  • 原文地址:https://www.cnblogs.com/kshtrueheart/p/12643330.html
Copyright © 2020-2023  润新知