Java is an Object Oriented Programming language. Java is developed by James Gosling from Sun Microsystems in the year 1991. Java is a platform independent language which means, a java program compiled on a machine can be run on any machine with a JVM installed on it.
Java programming language consists of Java compiler, Java Virtual Machine (JVM) and the java programming libraries. The java compiler converts the java code into byte code, which is understood by JVM and is interpreted into machine dependent code for execution on the machine which runs JVM. JVM is machine dependent. Each operating system got its own JVM.
Requirements to write and run a java program.
1. Java Runtime Environment (JRE)
2. Java Development Kit (JDK)
3. Editor (notepad, wordpad, notepad++, edit plus, etc) or an IDE like Eclipse or Netbeans can also be used.
Using an IDE saves time in many ways.
As of now, I am using Netbeans IDE for executing all the examples in this post.
Netbeans looks as follows:
Java Basic Syntax:
Class: In an Object Oriented Programming, a class is a construct that is used to create instances of itself. It can also be defined as a template/blue print that describes the behaviors/states that object of its type support.
Object: An object is a location in memory having a value and referenced by an identifier. Objects have states and behaviors.
Methods: A method is a behavior. (In C language it is like a function). A class can contain many methods. It is in methods where the logic are written, data is manipulated and all the actions are executed.
Instant Variables: Each object has its unique set of instant variables. An object's state is created by the values assigned to these instant variables.
First program:
Learning any programming language starts with Hello World program. So, java hello world program is coded as below
public class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
if you are using a notepad or any other editor for writing your java programs, you should compile and execute your java program form terminal or command prompt. Following are the steps to do that
1. Open command prompt
2. move to the location where your java file is located
3. if your java file is named HelloWorld.java (as it is compulsory for the public class name to match with the file name), then give "javac HelloWorld.java" with out the quotes. This gives HelloWorld.class file. If you get an error in doing this most probably class path is not set. Please take care of that. HelloWorld.class contains the byte code for the java code in HelloWorld.java.
4. Executing java code needs the .class file. It is done by entering in command prompt the following
"java HelloWorld" without the quotes.
if you are using an IDE, then it is very simple. Just right click on the file you want to execute and select run file form the right click menu. You can see the output in the output panel below your code area.
Points to remember while writing a java program
1. Java is a case sensitive programming language. Example, the identifiers 'A' and 'a' are considered as different.
2. Class names should start with upper case. If a class name has more than one word, then first letter of each word should be in upper case. Ex: HelloWorld
3. All method names should start with a lower case letter and if there are more words in a method name, then except for first word, other words should start with upper case. helloWorld()
4. Name of the program file should be same as the public class name in the java program. HelloWorld.java is file name for the program with public class HelloWorld.
5. a java program processing starts from the main() method. It is compulsory for any java program. Its syntax is public static void main(String args[]). main() method takes string arguments.
Java Keywords:
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.
Note: Single line comments and double line comments are same as in C and C++ programming.
Data types:
byte, short, int, long, float, double, boolean ,char.
Escape sequences for string and char literals:
\n -----> New line (0x0a)
\r -----> Carriage return (0x0d)
\f -----> Formfeed (0x0c)
\b -----> Backspace (0x08)
\s -----> Space (0x20)
\t -----> Tab
\" -----> Double quote
\' -----> Single quote
\\ -----> backslash
\ddd ---> Octal Character (ddd)
\uxxxx -> Hexadecimal UNICODE character (xxxx)
Operators
1. Arithmetic Operators:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement
2. Relational Operators:
== if equals, returns true
!= if not equals, returns true
> if greater than, returns true
< if less than, returns true
>= greater than or equal to
<= less than or equal to
3. Bitwise Operators:
& Binary AND
| Binary OR
^ Binary XOR
~ Binary ones complement
<< Binary Left Shift Operator
>> Binary Right Shift Operator
>>> Shift right zero full operator
4. Logical Operators:
&& Logical AND operator. If both the operands are non zero, then conditions becomes true
|| Logical OR operator. If any of the two operands are non zero, then condition becomes true
! Logical NOT operator. Used to reverse the state of its operand.
5. Assignment Operators:
= Simple assignment operator
+= Add and assignment operator
-= Subtract and assignment operator
*= Multiply and assignment operator
/= Divide and assignment operator
%= Modulus and assignment operator
6. Conditional Operator
( ? : ) :
variable x = (expression) ? value if true : value if false
Example: a = (10>20)?10:20
Decision Making in Java:
Different decision making statements include if, if-else,else if ladder, switch, for, while, do-while .
Following gives syntax for each of these decision making statements with an example for each.
a. if statement:
Syntax:
if(test_condition)
{
// true block
}
Example:
public class if_statement {
public static void main (String args[])
{
int a = 10, b = 20;
if(a
{
System.out.println("b is bigger than a");
}
if(a>b)
{
System.out.println("a is bigger than b");
}
}
}
Example:
public class if_else_statement {
public static void main (String args[])
{
int a = 10, b = 20;
if(a < b)
{
System.out.println("b is bigger than a");
}
else
{
System.out.println("a is bigger than b");
}
}
}
c. else-if ladder:
Syntax:
if(test_condition1)
{
// true block1
}
else if(test_condition2)
{
//true block 2
}
.
.
.
else if(test_condition n)
{
// true block n
}
else
{
// false block
}
Example:
public class if_else_ladder_statement {
public static void main (String args[])
{
int a = 10, b = 20, c = 30;
if(a > b && a > c)
{
System.out.println("a is biggest");
}
else if(b > a && b > c)
{
System.out.println("b is biggest");
}
else
{
System.out.println("c is biggest");
}
}
}
d. switch:
Syntax:
switch(condition)
{
case 1: // statements;
break;
case 2: // statements;
break;
case 3: // statements;
break;
...
...
case n: // statements;
break;
default: // statements;
}
Example:
public class switch_statement {
public static void main (String args[])
{
int a = 2;
switch(a)
{
case 1: System.out.println("case 1");
break;
case 2: System.out.println("case 2");
break;
case 3: System.out.println("case 3");
break;
default: System.out.println("no cases matched. default cse");
}
}
}
e. for loop:
Syntax:
for(initialization; test_condition; increment)
{
// loop body
}
Example:
public class for_loop{
public static void main (String args[])
{
for(int i=0;i < 10;i++)
{
System.out.println(i);
}
}
}
f. while loop
Syntax:
while(test_condition)
{
// loop body
}
Example:
public class while_loop {
public static void main (String args[])
{
int i=0;
while(i < 10)
{
System.out.println(i);
i++;
}
}
}
g. do-while loop:
Syntax:
do
{
// loop body
}while(test_condition);
Example:
public class if_statement {
public static void main (String args[])
{
int i=0;
do
{
System.out.println(i);
i++;
}while(i < 10);
}
}
Java programming language consists of Java compiler, Java Virtual Machine (JVM) and the java programming libraries. The java compiler converts the java code into byte code, which is understood by JVM and is interpreted into machine dependent code for execution on the machine which runs JVM. JVM is machine dependent. Each operating system got its own JVM.
Requirements to write and run a java program.
1. Java Runtime Environment (JRE)
2. Java Development Kit (JDK)
3. Editor (notepad, wordpad, notepad++, edit plus, etc) or an IDE like Eclipse or Netbeans can also be used.
Using an IDE saves time in many ways.
As of now, I am using Netbeans IDE for executing all the examples in this post.
Netbeans looks as follows:
Java Basic Syntax:
Class: In an Object Oriented Programming, a class is a construct that is used to create instances of itself. It can also be defined as a template/blue print that describes the behaviors/states that object of its type support.
Object: An object is a location in memory having a value and referenced by an identifier. Objects have states and behaviors.
Methods: A method is a behavior. (In C language it is like a function). A class can contain many methods. It is in methods where the logic are written, data is manipulated and all the actions are executed.
Instant Variables: Each object has its unique set of instant variables. An object's state is created by the values assigned to these instant variables.
First program:
Learning any programming language starts with Hello World program. So, java hello world program is coded as below
public class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
if you are using a notepad or any other editor for writing your java programs, you should compile and execute your java program form terminal or command prompt. Following are the steps to do that
1. Open command prompt
2. move to the location where your java file is located
3. if your java file is named HelloWorld.java (as it is compulsory for the public class name to match with the file name), then give "javac HelloWorld.java" with out the quotes. This gives HelloWorld.class file. If you get an error in doing this most probably class path is not set. Please take care of that. HelloWorld.class contains the byte code for the java code in HelloWorld.java.
4. Executing java code needs the .class file. It is done by entering in command prompt the following
"java HelloWorld" without the quotes.
if you are using an IDE, then it is very simple. Just right click on the file you want to execute and select run file form the right click menu. You can see the output in the output panel below your code area.
Points to remember while writing a java program
1. Java is a case sensitive programming language. Example, the identifiers 'A' and 'a' are considered as different.
2. Class names should start with upper case. If a class name has more than one word, then first letter of each word should be in upper case. Ex: HelloWorld
3. All method names should start with a lower case letter and if there are more words in a method name, then except for first word, other words should start with upper case. helloWorld()
4. Name of the program file should be same as the public class name in the java program. HelloWorld.java is file name for the program with public class HelloWorld.
5. a java program processing starts from the main() method. It is compulsory for any java program. Its syntax is public static void main(String args[]). main() method takes string arguments.
Java Keywords:
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.
Note: Single line comments and double line comments are same as in C and C++ programming.
Data types:
byte, short, int, long, float, double, boolean ,char.
Escape sequences for string and char literals:
\n -----> New line (0x0a)
\r -----> Carriage return (0x0d)
\f -----> Formfeed (0x0c)
\b -----> Backspace (0x08)
\s -----> Space (0x20)
\t -----> Tab
\" -----> Double quote
\' -----> Single quote
\\ -----> backslash
\ddd ---> Octal Character (ddd)
\uxxxx -> Hexadecimal UNICODE character (xxxx)
Operators
1. Arithmetic Operators:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement
2. Relational Operators:
== if equals, returns true
!= if not equals, returns true
> if greater than, returns true
< if less than, returns true
>= greater than or equal to
<= less than or equal to
3. Bitwise Operators:
& Binary AND
| Binary OR
^ Binary XOR
~ Binary ones complement
<< Binary Left Shift Operator
>> Binary Right Shift Operator
>>> Shift right zero full operator
4. Logical Operators:
&& Logical AND operator. If both the operands are non zero, then conditions becomes true
|| Logical OR operator. If any of the two operands are non zero, then condition becomes true
! Logical NOT operator. Used to reverse the state of its operand.
5. Assignment Operators:
= Simple assignment operator
+= Add and assignment operator
-= Subtract and assignment operator
*= Multiply and assignment operator
/= Divide and assignment operator
%= Modulus and assignment operator
6. Conditional Operator
( ? : ) :
variable x = (expression) ? value if true : value if false
Example: a = (10>20)?10:20
Decision Making in Java:
Different decision making statements include if, if-else,else if ladder, switch, for, while, do-while .
Following gives syntax for each of these decision making statements with an example for each.
a. if statement:
Syntax:
if(test_condition)
{
// true block
}
Example:
public class if_statement {
public static void main (String args[])
{
int a = 10, b = 20;
if(a
{
System.out.println("b is bigger than a");
}
if(a>b)
{
System.out.println("a is bigger than b");
}
}
}
b. if-else statement
Syntax:
if(test_condition)
{
//true block
}
else
{
//false block
}
Example:
public class if_else_statement {
public static void main (String args[])
{
int a = 10, b = 20;
if(a < b)
{
System.out.println("b is bigger than a");
}
else
{
System.out.println("a is bigger than b");
}
}
}
c. else-if ladder:
Syntax:
if(test_condition1)
{
// true block1
}
else if(test_condition2)
{
//true block 2
}
.
.
.
else if(test_condition n)
{
// true block n
}
else
{
// false block
}
Example:
public class if_else_ladder_statement {
public static void main (String args[])
{
int a = 10, b = 20, c = 30;
if(a > b && a > c)
{
System.out.println("a is biggest");
}
else if(b > a && b > c)
{
System.out.println("b is biggest");
}
else
{
System.out.println("c is biggest");
}
}
}
d. switch:
Syntax:
switch(condition)
{
case 1: // statements;
break;
case 2: // statements;
break;
case 3: // statements;
break;
...
...
case n: // statements;
break;
default: // statements;
}
Example:
public class switch_statement {
public static void main (String args[])
{
int a = 2;
switch(a)
{
case 1: System.out.println("case 1");
break;
case 2: System.out.println("case 2");
break;
case 3: System.out.println("case 3");
break;
default: System.out.println("no cases matched. default cse");
}
}
}
e. for loop:
Syntax:
for(initialization; test_condition; increment)
{
// loop body
}
Example:
public class for_loop{
public static void main (String args[])
{
for(int i=0;i < 10;i++)
{
System.out.println(i);
}
}
}
f. while loop
Syntax:
while(test_condition)
{
// loop body
}
Example:
public class while_loop {
public static void main (String args[])
{
int i=0;
while(i < 10)
{
System.out.println(i);
i++;
}
}
}
g. do-while loop:
Syntax:
do
{
// loop body
}while(test_condition);
Example:
public class if_statement {
public static void main (String args[])
{
int i=0;
do
{
System.out.println(i);
i++;
}while(i < 10);
}
}
This post briefs a very few things in java. Hope this helps. Keep visiting my blog for more topics. Thank you.