
Java Programming Patterns: Overview and Examples
Table of Content:
- Programming Style
- Example for above Style
- Example for above Style
- Different Types of printf Statement
- Style 1
- Example for Style 1
- Style 2
- Example for Style 2
- Style 3
- Example for Style 3
- How many ways can we write a java program
- 1) By changing sequence of the modifiers, method prototype is not changed.
- 2) subscript notation in java array can be used after type, before variable or after variable.
- 3) You can provide var-args support to main method by passing 3 ellipses (dots)
- 4) Having semicolon at the end of class in java is optional.
- Valid java main method signature
- Invalid java main method signature
- Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.
Programming Style
Note:Java is a freeform language. There are many ways to write a java program. We need not have to indent any lines to make the program work properly. Java system does not care wehere on the line we being typing.
Java is a freeform language. There are many ways to write a java program. We need not have to indent any lines to make the program work properly. Java system does not care wehere on the line we being typing. While this may be a licence for bad programming, We should try to use this fact to our advantages for producing readable programs. Although several alternate style are possible, we should select one and try to use it with total consistency.The modifications that can be done in a java program are given below:
For Example, the statement
System.out.println("Welcome to atnyla.com");
We can write the statement as
System.out.println ("Welcome to atnyla.com");
Example for above Style
class ProgrammingStyle{ public static void main(String args[]) { System.out.println ("Welcome to atnyla.com"); } }Output
Welcome to atnyla.com Press any key to continue . . .
Or even as
System . out . println ( "Welcome to atnyla.com" );
Example for above Style
class ProgrammingStyle{ public static void main(String args[]) { System . out . println ( "Welcome to atnyla.com" ); } }Output
Welcome to atnyla.com Press any key to continue . . .
Different Types of printf
Statement
Style 1
System.out.printf("Welcome to atnyla.com");
System.out.printf("Sum" +a+ " and " +b+ " = " +(a+b));
Example for Style 1
class ProgrammingStyle { public static void main(String args[]) { int a=2 , b=3; System.out.printf("Welcome to atnyla.com"); System.out.printf("Sum" +a+ " and " +b+ " = " +(a+b)); } }Output
Welcome to atnyla.comSum2 and 3 = 5Press any key to continue . . .
Style 2
System.out.println("Welcome to atnyla.com");
System.out.println("Sum" +a+ " and " +b+ " = " +(a+b));
Example for Style 2
class ProgrammingStyle { public static void main(String args[]) { int a=2 , b=3; System.out.println("Welcome to atnyla.com"); System.out.println("Sum" +a+ " and " +b+ " = " +(a+b)); } }Output
Welcome to atnyla.com Sum2 and 3 = 5 Press any key to continue . . .
Style 3
System.out.print("Welcome to atnyla.com");
System.out.print("Sum" +a+ "and" +b+ "=" +(a+b));
Example for Style 3
class ProgrammingStyle { public static void main(String args[]) { int a=2 , b=3; System.out.print("Welcome to atnyla.com"); System.out.print("Sum" +a+ " and " +b+ " = " +(a+b)); } }Output
Welcome to atnyla.comSum2 and 3 = 5Press any key to continue . . .
How many ways can we write a java program
1) By changing sequence of the modifiers, method prototype is not changed.
Let's see the simple code of main method.static public void main(String args[])
2) subscript notation in java array can be used after type, before variable or after variable.
Let's see the different codes to write the main method.public static void main(String[] args) public static void main(String []args) public static void main(String args[])
3) You can provide var-args support to main method by passing 3 ellipses (dots)
Let's see the simple code of using var-args in main method.public static void main(String... args)
4) Having semicolon at the end of class in java is optional.
Let's see the simple code.class A{ static public void main(String... args){ System.out.println("hello java4"); } };
Valid java main method signature
public static void main(String[] args) public static void main(String []args) public static void main(String args[]) public static void main(String... args) static public void main(String[] args) public static final void main(String[] args) final public static void main(String[] args) final strictfp public static void main(String[] args)
Invalid java main method signature
public void main(String[] args) static void main(String[] args) public void static main(String[] args) abstract public static void main(String[] args)