hello world and hello java
Writing your first program in java
Courtesy: http://2.bp.blogspot.com/-
Following the ritual of printing "hello world" to the system,let's get ready for our FIRST newbee java program.
Before writing the program,we must figure out which text editor you want to use.You can use the traditional Notepad or an advance free text editor especially for programmers called Notepad++.
Download link:notepad++download.
You can use IDE like netbeans or eclipse but for learning basics of java language you have to choose a text editor.
So....text editor done.
Then make a directory in a Mydocuments named java projects.
Here all your java projects resides.
So....Choosing directory done.
Then create your project "first program" inside the java projects folder. Create new file inside this folder name Hello_world.java.
NOTE:Every java file must end with extension .java(Ex.Sample.java) and there must be no spaces between the name and it must start with a character not a number.
Done...
This is a prototype of a main java program:
package name(optional);
import statements(optional);
access specifier class classname
{
public static void main(String args[])//only for main Class
{
//statements
}
}
Now the main thing...Writing the program ofcourse.
Open the hello_world.java in text editor,type these lines:
class Hello_world
{
public static void main(String args[])
{
//this is a comment which is not executed
System.out.println("hello world and hello java..");
}
}
Running java application from command prompt:open cmd(windows+r->cmd),type:"javac filename.java",hit enter(errors are shown if there is a mistake in the program otherwise),type "java filename"
Here javac is a case-insensitive command and java is a case-sensitive command.
Outpu world and hello java.t:hello
Explanation:
Line 1:keyword(reserved words for special use)class followed by the class name.Class is a user defined datatype that contains blocks,variables,methods.
Line 2:opening of bracket for class
Line 3:keyword public is access specifier.
static is a keyword
void is a return type that returns nothing
main is a method name and our program execution starts from main method
String is a class name from java.lang.array.
args is a name whose type is string.
Line 4:opening bracket for main method
Line 5:This line is not executed by the compiler.
Line 6:System is a class.
out is reference variable of print stream class
println is a method of print stream class that prints something that is passed to the system.
ln in println is used to move to the next line
println method is passed hello world and hello java.. as an argument
Line 6:closing of main method bracket
Line 7:closing of class bracket
Variables
Variables is a name memory location.A variable have type and a name.
Suppose a person that has name(eg.Syed) and a type student and the person is only be accessed by its name only.So conceptually Syed is a variable with type student.
Naming convention of a variable in java:
Courtesy: http://2.bp.blogspot.com/-
Following the ritual of printing "hello world" to the system,let's get ready for our FIRST newbee java program.
Before writing the program,we must figure out which text editor you want to use.You can use the traditional Notepad or an advance free text editor especially for programmers called Notepad++.
Download link:notepad++download.
You can use IDE like netbeans or eclipse but for learning basics of java language you have to choose a text editor.
So....text editor done.
Then make a directory in a Mydocuments named java projects.
Here all your java projects resides.
So....Choosing directory done.
Then create your project "first program" inside the java projects folder. Create new file inside this folder name Hello_world.java.
NOTE:Every java file must end with extension .java(Ex.Sample.java) and there must be no spaces between the name and it must start with a character not a number.
Done...
This is a prototype of a main java program:
package name(optional);
import statements(optional);
access specifier class classname
{
public static void main(String args[])//only for main Class
{
//statements
}
}
Now the main thing...Writing the program ofcourse.
Open the hello_world.java in text editor,type these lines:
class Hello_world
{
public static void main(String args[])
{
//this is a comment which is not executed
System.out.println("hello world and hello java..");
}
}
Running java application from command prompt:open cmd(windows+r->cmd),type:"javac filename.java",hit enter(errors are shown if there is a mistake in the program otherwise),type "java filename"
Here javac is a case-insensitive command and java is a case-sensitive command.
Outpu world and hello java.t:hello
Explanation:
Line 1:keyword(reserved words for special use)class followed by the class name.Class is a user defined datatype that contains blocks,variables,methods.
Line 2:opening of bracket for class
Line 3:keyword public is access specifier.
static is a keyword
void is a return type that returns nothing
main is a method name and our program execution starts from main method
String is a class name from java.lang.array.
args is a name whose type is string.
Line 4:opening bracket for main method
Line 5:This line is not executed by the compiler.
Line 6:System is a class.
out is reference variable of print stream class
println is a method of print stream class that prints something that is passed to the system.
ln in println is used to move to the next line
println method is passed hello world and hello java.. as an argument
Line 6:closing of main method bracket
Line 7:closing of class bracket
Variables
Variables is a name memory location.A variable have type and a name.
Suppose a person that has name(eg.Syed) and a type student and the person is only be accessed by its name only.So conceptually Syed is a variable with type student.
Naming convention of a variable in java:
- cannot start with a symbol(except "$","_")or numbers,start with an alphabet(eg.hello)you can use a "$" sign or "_" sign but it's better not to use it.
- does not contain spaces:hello world(wrong),instead use underscore hello_world(right)
- For a constant variable(discussed later) used capital letters(eg.PI).
- Variable names are case-sensitive,i.e,Syed and syed and sYed are not same.
Incorrect variable names:! Variablename, * variablename, variable name, 1variaBle_name.
Correct variable names:variable_name,$variable_name,variable_name1
Courtesy: http://4.bp.blogspot.com/
Datatypes
Datatypes are meant to identify and associated operations for handling it.In clear words datatypes are used to identify what type of data actually is,is it integer,float,boolean,double or character.
A primitive datatype or CAN be derived (or non = primitive).
Primitive datatypes are meant to operate basic operations.
Derived datatypes are extended from the primitive datatypes.
Primitive datatypes:long,int,short,byte,boolean etc.
Derived datatypes:string,array,class,interface etc.
You can't fit directly datatype smaller than the other one.
For example:a byte can't fit directly into and int.
Example program:
int big=10;
byte small=big;//an error occurred
Things can be solved by type-casting(we will discuss this later):
int big=10;
byte small=(int)big;
Object
An object is a variable of a class,you may say.Objects hadles data and its methods.Anything can be object.In real life a bike,a table,a fan can be an object ranging your imagination.
Objects are core of java programming.Objects carry two characterstics:state and behaviour.
State:In what state the object is??
Behaviour:What the object tends to do??
Consider state as the variables and the behaviour as the methods of class.
We humans learn by example:consider a bike as an object what state and behavious it has??
state:3rd gear,60kmph,5th gear
behaviour:change gear,accelerate,brake etc etc
Class
Class is an blueprint for an object.Actually,objects are of class-type.
A JAVA PROGRAM WITHOUT CLASS CANNOT BE MADE IN ANY CASE.
That's why java is a object oriented programming language.
Class contains methods and instance variables.
Operators
Operators performs certain operations on a variable and gives a result.
Tyoes of operators:
Any executable piece of code in a program is an statement.
Example:int j=10;
j=j+10;
i=j-1;
j++;
i=(i+j)/100;
//someother statements
//other statements
//so on
Datatypes
Datatypes are meant to identify and associated operations for handling it.In clear words datatypes are used to identify what type of data actually is,is it integer,float,boolean,double or character.
A primitive datatype or CAN be derived (or non = primitive).
Primitive datatypes are meant to operate basic operations.
Derived datatypes are extended from the primitive datatypes.
Primitive datatypes:long,int,short,byte,boolean etc.
Derived datatypes:string,array,class,interface etc.
You can't fit directly datatype smaller than the other one.
For example:a byte can't fit directly into and int.
Example program:
int big=10;
byte small=big;//an error occurred
Things can be solved by type-casting(we will discuss this later):
int big=10;
byte small=(int)big;
Object
An object is a variable of a class,you may say.Objects hadles data and its methods.Anything can be object.In real life a bike,a table,a fan can be an object ranging your imagination.
Objects are core of java programming.Objects carry two characterstics:state and behaviour.
State:In what state the object is??
Behaviour:What the object tends to do??
Consider state as the variables and the behaviour as the methods of class.
We humans learn by example:consider a bike as an object what state and behavious it has??
state:3rd gear,60kmph,5th gear
behaviour:change gear,accelerate,brake etc etc
Class
Class is an blueprint for an object.Actually,objects are of class-type.
A JAVA PROGRAM WITHOUT CLASS CANNOT BE MADE IN ANY CASE.
That's why java is a object oriented programming language.
Class contains methods and instance variables.
Operators
Operators performs certain operations on a variable and gives a result.
Tyoes of operators:
- Arithmetic operators
- Logical operators
- Relational operators
- Assignment operators
- Bitwise operators
Addition(+):a+b gives 5
substraction(-):a-b gives 1
substraction(-):a-b gives 1
Multiplication(*):a*b gives 6
Division(/):a/b gives 1 for integer
Modulus(%):remainder after dividing two variables:a%b gives 1
Increment(++):Increases the value of variable by 1:a++ gives 4
Decrement(--):Decreases the value of variable by 1:b-- gives 1.
Logical operators:Performs logical operations such as AND,OR,NOT,EQUALS TO,NOT EQUALS TO.
And operator(&&):true,if all the conditions are true.
OR operator(||):True,if any of the condition is true.
NOT operator(!):Reverses the input,i.e,!true gives false and !false gives true.
Relational operators:
Relational operators are used to compare between variables.
<,>,>=,<=,==,!=
Less than,greater than,greater than equals to,Less than equals to,equals to and not equals.Relational operators returns either true or false.
Assignment operators:Simplest assignment operator is "=" used for assigning the right operator value to the left one.
Example:int a=10;
int b=15;
b=a;//now the value of b is overwrited to 10.
Bitwise operators:
Performs bitwise operations,they are less used than the other operators.
Inversion(~),bitwise AND(&),bitwise or(|),bitwise exor(^),bitwise shift left(<<),shift right(>>).
StatementsLogical operators:Performs logical operations such as AND,OR,NOT,EQUALS TO,NOT EQUALS TO.
And operator(&&):true,if all the conditions are true.
OR operator(||):True,if any of the condition is true.
NOT operator(!):Reverses the input,i.e,!true gives false and !false gives true.
Relational operators:
Relational operators are used to compare between variables.
<,>,>=,<=,==,!=
Less than,greater than,greater than equals to,Less than equals to,equals to and not equals.Relational operators returns either true or false.
Assignment operators:Simplest assignment operator is "=" used for assigning the right operator value to the left one.
Example:int a=10;
int b=15;
b=a;//now the value of b is overwrited to 10.
Bitwise operators:
Performs bitwise operations,they are less used than the other operators.
Inversion(~),bitwise AND(&),bitwise or(|),bitwise exor(^),bitwise shift left(<<),shift right(>>).
Any executable piece of code in a program is an statement.
Example:int j=10;
j=j+10;
i=j-1;
j++;
i=(i+j)/100;
//someother statements
//other statements
//so on
Comments
Post a Comment