Here are some of the rules and conventions that you need to follow while writing a code.
- You need to import a package before starting anything else.
- Package must contain at least one "Class".
- Each "Class" must contain one "Method (function) ".
- If you use more than one word jointly then start first word after the dot with a small letter while first letter of each word next to it will be capital.For example: " ItIsAnExample ".
- If you are creating an app then you must have to use "main" method.
If you haven't read our article on "Basics of Java" then i would recommend you read it first before reading it further. - A data type for a variable must be defined before using that variable.
For example " int a ; " ,this will define that "a" is a variable and it can be only an integer.
if we directly write "a" without writing any data type with that then it will display us an error. - You need to terminate each and every syntax if it is terminating . For example:
System.out.print("Hello world!");Here , ";" is used to terminate the syntax.
Example of non-terminating syntax is :
if( a>b)We can't terminate if and else conditions , because if we do so then it will not read its remaining portion .
- "Else" cannot be used without "if " but "if " can be used without "else ".
- Don't change the "casing" of words during coding .For example if you used "a" as a variable then you cannot use "A" in place of that because they both are different words in Java's point of view.
- While using "if " and "else" syntax , if you need more than one function or syntax, you will need to use curly brackets for that.
- Pre-defined words (keywords) cannot be used as identifies ( variables ).
import java.lang.*;
public class Test
{
public static void main (String[] args)
{
System.out.println("Hello world!");
System.out.print("This is our first Java program.");
}
}
The output of Code will be:
"Hello word
This is our first Java program. "
Explanation of code:
- "import java.lang.*;" .This command is importing the package.
- "public class Test". This command will create a new class with name "test".
- "public static void main(String[] args) ".This command is used to differentiate between application and applet.
- "System.out,println("Hello world!");". This command will generate an output showing "Hello world!" and then cursor will move to next line because "ln" after "print" means that after generating the display of the given string , cursor will move to new line.
- "System.out.print("This is our first Java Code.");" . This command is also used to display the output to user ,but the difference is that it doesn't move cursor to next line after displaying the string if anything is written after that syntax,it will execute that in same line.
So here was our first and simplest java program.If you like this then please share it with your friends .
Keep Visiting and Keep learning.
0 comments:
Post a Comment