- 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 Program Structure
Before proceed to learn Java, it is important to understand the basic structure of a Java program.
To know about the structure of a Java Program, just look at the following simple example:
/* Java Program Example - Java Program Structure * This is a simple Java program, prints * "Hello World" on the Screen */ public class JavaProgram { /* This * is * a * multi-line * comments */ public static void main(String args[]) { // single line comments System.out.println("Hello World"); } }
When the above Java Program is compile and executed, it will produce the following output:
Let's explain the above Java Program:
- public class JavaProgram: This line of code has three parts:
- public : This is an access modifier keyword, tells the compiler to access the class. Various values of access modifiers are public, protected, private or default (no value).
- class : This keyword is used to declare the class. Name of the class (here JavaProgram) followed by this keyword.
- JavaProgram : This is the name of the class. You can give this name according to your demand/program.
- Comment Section : You can write comments in two ways:
- Single Line Comments : It start with two forward slashes i.e. // and continue to the end of the current line. Line comments do not require an ending symbol.
- Multi Line Comments : Also called as block comments start with a forward slash and an asterisk (/*) and end with an asterisk and a forward slash (*/).Block comments can also extend across as many lines as needed.
- public static void main (String args[]) : This line of code has three keywords, main, and string arguments:
- public : Access Modifier
- static : static is reserved keyword which means that a method is accessible and usable even though no objects of the class exist.
- void : This keyword declares nothing would be returned from method. Method can return any primitive or object
- main : This is the main where program starts running or from here our compiler starts checking and running our java program/code.
- Arguments : This is for the arguments purposes which will be included inside the curly bracket.
- System.out.println("Hello World"): This line of code has also four parts:
- System : It is the name of Java utility class.
- out : It is an object which belongs to System class used in sending the data which is inside the bracket to the console.
- println : It is print line which is utility method name which is used to send any String to console.
- Hello World : It is String literal set as argument to println method
Example
Let's look at some more Java program. Here is the first program:
/* Java Program Structure Example 1 */ public class JavaProgram { public static void main(String args[]) { System.out.println("Hello Compiler, This is simple Java program"); } }
When the above Java program is compile and executed, it will produce the following output:
Here is another program in Java, takes input from the user:
/* Java Program Structure Example 2 */ import java.util.Scanner; public class JavaProgram { public static void main(String args[]) { String name; Scanner scan = new Scanner(System.in); System.out.print("Enter your Name :"); name = scan.nextLine(); System.out.println("Hello " +name+ ", this is fresherearth"); } }
When the above program is compile and executed, it will produce the following output:
You can go check to many Java programs at Java Programming Examples.
« Previous Tutorial Next Tutorial »