Java Beginner's Guide [2] - Basic Syntax



Hi Everyone! Today I'm going to teach you some basic syntax (the structure of statements in a computer language) in java. So Let's start learning.. 

When it comes Java programs and their syntax, it is very important to keep in mind these following key points.
  • Case Sensitivity − Java is case sensitive, which means identifier Car and car would have different meaning in Java. 
  • Class Names − For all class names the first letter should be in Upper Case (Capital Letter). If several words are used to form a name of the class, each inner word's first letter should be in Upper Case. For an Example: class MyFirstJavaClass 
  • Method Names − All method names should start with a Lower Case letter (Simple Letter). If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case. Example: public void myMethodName() 
  • Program File Name − Name of the program file should exactly match the class name. When saving the file, you should save it using the class name (Remember Java is case sensitive) and append '.java' to the end of the name (if the file name and the class name do not match, your program will not compile). Example: Assume 'HelloWorld' is the class name. Then the file should be saved as 'HelloWorld.java
  • Java program processing starts from the main() method which is a mandatory part of every Java program. 

Java Identifiers

Names that we use for classes, variables, and methods are called identifiers. You can use any name but let's identify some important facts to remember about identifiers.
  • All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). 
  • After the first character, identifiers can have any combination of characters. 
  • A key word cannot be used as an identifier. 
  • Most importantly, identifiers are case sensitive. 

Identifiers like: name, $name, _name, __1_name, first_name are legal in Java. But 123abc, -name cannot be identified as legal identifiers. 

Java Keywords

Keywords words in a programming language may not be used as constant or variable or any other identifier names. The following list shows the keywords in Java.


Comments 

Java supports single-line and multi-line comments. All characters available inside any comment are ignored by Java compiler. Look at the below example and understand how comments work in java.



Compile & run the program. See what's happening. 


Nothing happen apart from the "Hello World" since compiler ignores anything inside the comments. 


Blank Lines

A line containing only white space, possibly with a comment, is known as a blank line, and Java totally ignores it.


Great! You have finished your tutorial on basic syntax. Now you know how syntax works in java, up to some extent. If you came up with any problem regrading this article, Please drop a comment or send an email to us. 



Previous | Next

Kalpani Ranasinghe
email: kalpanibhagya.kb@gmail.com
Facebook icon Twitter icon LinkedIn icon Google Plus icon

Comments