Java Basic Syntax

Java is case sensitive:
Hello and hello is not same in java

Class Name: Class name should start with capital letter at first letter eg class:Hubby{}

If several words then each inner word’s first letter should be in upper case example class: MyPuppy{}

All method name should start with a lower case letter.

If several words are used to form the name of the method, then each inner wrd’s first letter should be in upper case.

example: public void myMethodName(){}

Program file name should match the class name. if the class name is Zaki{} then the file name should be saved as Zaki.java

Java program starts processing from the main() method which is a mandatory part of every java program.

public static void main(String []args){

 

}

 

Java Identifiers:
All java components require names.Names used for classes,variables and methods are called identifiers.

In java there are several points to remember about identifiers.
They are as follows:

All identifiers should begin with a letter (A to Z or a to z), currency character($) or an underscore(_).

– After the first char identifiers can have any combination of characters.

– A keyword cannto be used as an identifier

– Identifiers are case sensitive.

example legal identifier : age,$salary,_value,_1_value
example of illegal identifiers:123abc,-salary

Java modifiers:

Like other languages,It is possible to modify classes,methods, etc. by using modifiers.There are two categories of modifiers:
Access Modifiers:default,public,protected,private
Non-access Modifiers:final,abstract,strictfp

Java Arrays:

Java Enums: In java 5.0 enums  introduced.Enums restrict a variable to have one f only a few predefined values.The values in this enumerated list are called enums. With the use of enums it is possible to reduce the number of bugs in your code.

For example, if we consider an application for a fresh juice shop,it would be possible to restrict the glass size to small,medium and large.This would make sure that it would not allow anyone to order any size other than the small,medium or large.

Example:

class FreshJuice {

	enum FreshJuiceSize{SMALL,MEDIUM,LARGE}
	FreshJuiceSize size;
}

public class FreshJuiceTest{
	
	public static void main(String []args)
	{
		FreshJuice juice=new FreshJuice();
		juice.size=FreshJuice.FreshJuiceSize.MEDIUM;
		System.out.println("Size: "+juice.size);
	}
	
}

enum can be declared as  their own or inside a class.Methods,variables,constructors can be defined inside enums as well.

Java Keywords:

The following list shows the reserved words in java.These reserved words may not be used as constant or variable or any other identifier names.

they are:

abstract,assert,boolean,break,byte,case,catch,char,class,const,continue,default,do,double,else,enum,extends,final,finally,float,for,goto,if,implements,import,instanceof,int,interface,long,native,new,package,private,protected,public,return,short,static,strictfp,super,switch,synchronized,this,throw,throws,transient,try,void,volatile,while

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *