- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Basic Syntax
For Java programmer, it is very important to keep in mind about the following points.
- Case Sensitivity - Java is a case sensitive language, which means that the identifier Hello, hello, HeLLo, hEllO, helLo, HELLO. All are different in Java.
- Method Names - All the method names should start with a Lower Case letter.
If several words are used to form the name of the method, then each first letter of inner word should be in Upper Case
Example: public void employeeRecords(), public void myMethodName(), public void employeeNumber() etc. - Class Names - For all class names, the first letter should be in Uppercase
If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
Examples: class MyJavaProgram, class MyThirdJavaProgram, class StudentRecords etc. - public static void main(String args[]): Java program processing starts from the method main() which is a mandatory part of every Java program.
- Program File Name - Name of a program file should exactly match the class name.
When saving the file, you should save it using the class name and add/append '.java' to end of the name.
If file name and the class name do not match, your program will not compile.
Example: Think 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java'
Java Basic Syntax Example
Following is Java's simplest program, prints Hello World on the screen.
/* Java Program Example - Java Basic Syntax */ public class JavaProgram { public static void main(String args[]) { /* print Hello World */ System.out.println("Hello World"); } }
When the above Java program is compile and executed, it will produce the following output:
Identifiers in Java
In Java, an identifiers are used to name things, such as classes, variables, and methods.
An identifier may be any sequence of uppercase and lowercase letters, number or the underscore and dollar-sign characters. As you know, Java is case-sensitive, therefore, VALUE is a different identifier than Value.
Following are the rules to declare Identifiers in Java:
- All identifiers can begin with a letter (A to Z or a to z) or dollar currency character ($) or an underscore (_).
- After the first character identifiers can have any combination of characters.
- Most importantly identifiers are case-sensitive.
- A keyword cannot be used as an identifier since it has reserved words and have some special meaning.
- Examples of illegal identifiers: 123abc, -salary etc.
- Examples of legal identifiers: AvgTemp, count, a4, this_is_ok, age, $salary, _value, __1_value, customers etc.
Whitespaces in Java
A line containing only whitespace, possibly with comment, is known as blank line, and Java compiler totally ignores it.
Comments in Java
There are the three types of comments defined by Java. You have already seen two: single-line and multiline comments. The third type is a documentation comment. This type of comment is used to produce an HTML file that documents your program. The documentation comment begins with /** and ends with */.
All characters available inside any comment are ignored by Java compiler. Her is an example.
/* Java Program Example - Java Basic Syntax */ public class JavaProgram { /* This is a simple Java program. It will print 'Hello World' on the output screen It is an example of multi-line comments or block comments. */ public static void main(String args[]) { // It is an example of single line comment /* It is also an example of single line comment. */ System.out.println("Hello World"); } }
When the above Java program is compile and run, it will produce the following output:
« Previous Tutorial Next Tutorial »