Java Basic Syntax
A Java program can be thought of as a collection of objects that work together by calling each other's methods. The following summarizes the concepts of classes, objects, methods, and instance variables.
- Object: An object is an instance of a class that has a state and behavior. For example, a cat is an object whose states are: color, name, and breed; behaviors are: tail wagging, eating, etc.
- Class: A class is a template that describes the behavior and state of a class of objects.
- Instance Variables : Each object has unique instance variables, and the state of the object is determined by the values of these instance variables.
First Java program
1 2 3 4 5 6 7 8 9 | public class FirstJavaProgram { /* * first Java program. * it will print the string Hello World */ public static void main(String[] args) { System.out.println("Hello World"); // print Hello World on the console } } |
Hello World
- Open Notepad and add the above code into it
- Save the file name as: FirstJavaProgram.java
- Open the cmd command window and enter the location of the target file, assuming it is Desktop:\
- Type javac FirstJavaProgram.java in the command line window and press enter to compile the code. If there are no errors in the code, the cmd command prompt goes to the next line. (Assuming the environment variables are all set).
- Then type java FirstJavaProgram and press Enter to run the program
You will see Hello World in the window
When writing Java programs, you should pay attention to the following points:
- Case Sensitivity: Java is case sensitive, which means that the identifiers Hello and hello are totally different.
- Class name's: For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should start with a capital latter, such as FirstJavaProgram.
- Method name's: All method names should start with a lowercase letter. If the method name contains several words, capitalize the first letter of each following word, such as firstJavaMethod
- Main method entry: All Java programs start execution from the main method public static void main(String[] args)
Java identifiers
Identifiers are the names that identify elements like classes, methods, interfaces, and variables in a program.
List of some valid identifiers example in Java
age, x, A, MyName, myname, my_program, _myprogram, $myprogram, $_myprogram, _5_myprogram etc
List of some invalid identifiers example in Java
age+x, 111A, My Name, my name, #my_program, 1_myprogram, &myprogram, my-program etc
Rules for defining an Identifier in Java:
- All identifiers should start with a letter (AZ or az), dollar sign ($), or underscore (_)
- The first character can be followed by any combination of letters (AZ or az), dollar signs ($), underscores (_) or numbers
- Keywords cannot be used as identifiers
- Identifiers are case sensitive
- Identifiers can’t start with digit
abstract | assert | boolean | break | byte |
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 | instanceOf | volatile | while |
Types of Variables in Java
Basically, there are three types of variables in java :
- Local Variable
- Instance Variable
- Static Variable