Writing first java program
|
public class first
{
public static void main(String str[])
{
System.out.println("this is my first java program");
}
}
|
The output of
the above example is
this is my first java program.
how to run the program?
I will explain how to run thE program step by step.
step 1 create a new folder called myprogram and save the first.java(the file name same as class name) on that folder.
step 2 complile the program using javac.exe file.type the following program in the command prompt
c:\myprogram>javac first.java
step 3 to run the program type the following command on the command prompt
c:\>java first
thats it this is your first java program congrates!!!!!!!!!!!
comments:
comments are statements which are ignored by compiler
/* this is an example of mulitple line comment */
single line comment // this is single line comment
identifiers
identifiers are the name given to variable,methods,classes,packages and interfaces.
the following are the valid variable .
myvariable
Myvariable
MYVARIABLE
_MYVA
x
Java keywords
Keywords are reserved words that are predefined by the language and cannot be used as identifier. Keywords are reserved for their intended use and cannot be used by the programmer for variable or method names. Keywords are listed below
|
| abstract | default | goto | null | synchronized | | boolean | do | if | package | this | | break | double | implements | private | throw | | byte | else | import | protected | throws | | case | extends | instanceof | public | transient | | catch | final | int | return | true | | char | finally | interface | short | try | | class | float | long | static | void | | continue
| for | native | super | volatile | | const | false | new | switch | while |
|
data types in java
data types in java are categorized into two parts
- primitive data type
- Reference data type
In java you will be using 8 data types called PRIMITIVE DATA TYPES.These are 8 types are byte,short,int,long,char,float,double and boolean.
Java Operators
OPERATORS ARE THE SYMBOL THAT MAKES IT POSSIBLE TO USE ON fOR MORE VALUES TO PRODUCE A NEW VALUE. A VALUE THAT IS OPERATED ON BY AN OPERATOR IS AN OPEREND.
Different operators used in Java.
Arithmetic Operators
|
| Operator |
Description |
Example |
Result |
| + |
Addition |
x=2 x+2 |
4 |
| - |
Subtraction |
x=2 5-x |
3 |
| * |
Multiplication |
x=4 x*5 |
20 |
| / |
Division |
15/5 5/2 |
3 2.5 |
| % |
Modulus (division remainder) |
5%2 10%8 10%2 |
1 2 0 |
| ++ |
Increment |
x=5 x++ |
x=6 |
| -- |
Decrement |
x=5 x-- |
x=4 |
Assignment Operators
| Operator |
Example |
Is The Same As |
| = |
x=y |
x=y |
| += |
x+=y |
x=x+y |
| -= |
x-=y |
x=x-y |
| *= |
x*=y |
x=x*y |
| /= |
x/=y |
x=x/y |
| %= |
x%=y |
x=x%y |
Comparison Operators
| Operator |
Description |
Example |
| == |
is equal to |
5==8 returns false |
| != |
is not equal |
5!=8 returns true |
| > |
is greater than |
5>8 returns false |
| < |
is less than |
5<8 returns true |
| >= |
is greater than or equal to |
5>=8 returns false |
| <= |
is less than or equal to |
5<=8 returns true |
Logical Operators
| Operator |
Description |
Example |
| && |
and |
x=6 y=3
(x < 10 && y > 1) returns true |
| || |
or |
x=6 y=3
(x==5 || y==5) returns false |
| ! |
not |
x=6 y=3
!(x==y) returns true | |
conditional statement
conditional statements allow execution of selected statements or portion of the program,based on the value of some expressions.
if statement
Almost all computer programs need to make decisions. They test a condition and operate differently based on the outcome of the test. All programming language have some form of an if statements that tests conditions. The basic syntax of if statement is
If (Boolean expression)
Statement or block
the arguments to a conditional statement like if must be a boolean value
consider the example
|
class ifexample
{
public static void main (String args[])
{
int a=9;
int b=8;
if(a>b)
{
System.out.println("a is greater than b");
}
}
|
The output will be as follows
a is greater than b
|
if else statement |
if(a>b)
System.out.println("a is greater than b");
else
System.out.println("b is greater than a"); |
switch statementswitch statements are shortened for certain kinds of if statements.the example is as follows
|
switch(x)
{
case 0:do something;
break;
case 1: do something;
break;
case 2: do something;
break;
case 3: do something;
break;
default: do something else;
}
|
Looping Statement
There might be a situation where you want to execute a statement or a block repeatedly. Take a simple example; you want to display integers starting 1 to 10. You can simply write 10 println statements as given below
System.out.println(1); System.out.println(2); System.out.println(3); System.out.println(4); System.out.println(5); System.out.println(6); System.out.println(7); System.out.println(8); System.out.println(9); System.out.println(10);
But imagine if you want to display numbers form 1 to 100 or 1000. For this you need loops or looping statements. Following are the looping statement used in java.
while Statement
general syntax of the while loop is
while (boolean condition)
statement or block
consider the example
|
public class stars
{
public static void main(String [] s)
{
int a=1;
while(a<10)
{ System.out.println(a);
a++;
}
}
}
|