Java basic syntax

Note:

In java all identifiers are allowed only to begin with alphabets,_ or $

Any Unicode digits can be used in other positions

The normal naming conventions are outlined below

Java Keywords:

Java keywords are words that have a default or pre defined meaning . eg: int , static, abstract , final etc . These names are reserved and so we cannot create classes or variables with same name like keywords.

Java Identifiers:

Names used for classes, variables, and methods are called identifiers.

Below are the basic naming rules:

  • Name begins only with a letter (A to Z or a to z), currency character ($) or an underscore (_).
    • Examples of legal identifiers: age, $salary, _value, __1_value.
    • Examples of illegal identifiers: 123abc, -salary.
  • After the first character, identifiers can have any combination of characters.
  • A key word cannot be used as an identifier.
  • case sensitive.

Identifier Rules………..

Case Sensitivity:

Identifier Hello and hello will be considered as two different variables

Class Names:

Can start as mentioned in note but by convention, Class names starts with capital letter and each word in the class name starts with capital letter. For instance, 'ItemCartClass'

Method Names:

Can start as mentioned in note but by convention, method names starts with small letter and remaining words in the method name starts with capital letter. For instance, 'buyItemMethod' 

Program File Name:

A java file can only have one public class and the file should be saved in the same name as that of public class. For instance, 'MyFirstJavaProgram.java' if Public class  MyFirstJavaProgram  is used.



If no public class is used then you can save the file with any name,


package test;
class class11 {

public void printsome(){
 System.out.println("hi");
 }
 
 }

Can be stored with any name. But cannot be imported from another package as the default access modifier is package private.

To run a program:

public static void main(String args[]) 

Java execution starts from the main() method

package test;

public class class2{

public static void main (String args[]){

System.out.println("hi");
class11 a = new class11();
a.printsome();
}

}