Basic Syntax

If you don’t have Java  installed and need to setup the environment visit this link :  http://wp.me/p49tJ1-2

Object: An object defines behaviors and states of a class. For example we take class Dog we know that the dog has states like it has a color or name and it also have Behaviors for example barking, running e.t.c. More about Objects at :http://wp.me/P49tJ1-1w

Class:                                                                                                                                                                                               A class is a block  which contain Methods/Behaviors and Fields/States which  are  associated with the object of its type. class can be made by using “class” keyword followed by the name of the class i.e Class1. More about Classes at :http://wp.me/P49tJ1-1w

Method:                                                                                                             Method of a class defines the class’s specific behavior  . A class can have as many methods as required.  More About Methods at :

Here is a simple java Program:

class MyFirst{

public static void main(String args[]){

System.out.println(“Hello CodeHeaven”);  // prints text on screen

}

}

Compiling and Running Java Code:

  • Open notepad and copy and paste the above code.
  • Save as MyFirst.java
  • Open Command Prompt and go to the directory where you saved the MyFirst.java file.
  • Type ‘javac MyFirst.java’ if there are no errors your code is ready to run
  • Type ‘java MyFirst’

Output:

Hello CodeHeaven

RULES:

  • Execution of a java program starts from the main function (public static void main(String arguments[])) .
  • Java is case sensitive language for example ‘Hello’ and ‘hello’ are two different things in java(HTML is not case Sensitive).
  • Name of the class in java should start with a uppercase  Letter.
  • Name of a Method should start with a lowercase letter following letters may be uppercase or lowercase.

RULES for Identifiers Name:

  •  Identifier can Consist of letters(A-Z and a-z) numbers and characters such as $(DolllorSign) and _(Underscores).
  •  Identifier must start with a letter or an underscore
  •  Java keywords can not be used as Identifiers

Legal Identifiers : _name ,a345,D

Illegal Identifiers : 123a, switch

Modifiers in JAVA:    Java has two type of Modifiers:

  • Access Modifiers i.e public, protected, private.
  • Non-Access Modifiers final, static and abstract.

 COMMENTS in java :

Java accepts C style comments which is /* comments   */

for single line comments java uses  ‘//‘ before the line

for example // this is a comment.

JAVA Keywords:

Java has 50 keywords they have  a unique meaning in java and they can not be used as Identifiers

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s