Java Programs for Beginners | How to Start coding in JAVA?

Java Programs for Beginners | How to Start coding in JAVA?

JAVA for Beginners

I had written some basic programs that will surely help you to start coding in JAVA. Java is a simple programming language if you are having a bit of programming concept or if you had already coded in C C++ or any popular language.

If not coded in any of the languages, still it will not be difficult for you to start coding in JAVA.

Lets start with some basic programs.

For printing “Welcome to Java!” :

package chapter1;

public class Welcome { 

  public static void main(String[] args) {

    System.out.println(“Welcome to Java!”);



We just created a package named Chapter 1 and a class named Welcome. Also a public Main function also known as the driver function.

System.out.println – is used for printing just like cout in C++.


Package specifies a package name, Packname, for the class Welcome. Java Compiler compiles the source code in, generates Welcome.class, and stores Welcome.class in the Project  folder.

Reserved Words/ Key Words

  • Key words / Reserved words are those words whose meaning already defined by compiler.

  • Cannot be used as Variable Name


Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected. A public datum, method, or class can be accessed by other programs. A private datum or method cannot be accessed by other programs.


  • An instruction

  • int num;

  • System.out.println(“Hello”);


A pair of braces in a program forms a block that groups components of a program.



The class is the essential Java construct. A class is a template or blueprint for objects. To program in Java, you must understand classes and be able to write and use them.


What is System.out.println? It is a method: a collection of statements that performs a sequence of operations to display a message on the console. It can be used even without fully understanding the details of how it works. It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In this case, the argument is “Welcome to Java!” You can call the same println method with a different argument to print a different message.

Main Method

  • Public static void main(String args[])

  • Where program execution starts.

  • String args[] is string an array use for command line arguments.

Primitive Data Types and Operations

Identifiers, Variables, and Constants

Primitive Data Types

  • byte, short, int, long, float, double, char, boolean


Operators, Precedence, Associativity, Operand Evaluation Order: ++, –, *, /, %, +=, -=, *=, /=, %=, ^, &, |, +, -,


  • An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($).

  • An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit.

  • An identifier cannot be a reserved word.

  • An identifier can be of any length.


final datatype CONSTANTNAME = VALUE;  

final double PI = 3.14159;

final int SIZE = 3;

Numerical Data Types

byte            8 bits

short          16 bits

int            32 bits

long           64 bits

float          32 bits

double         64 bits


+, -, *, /, and %

5/2 yields an integer 2.

5.0/2 yields a double value 2.5

5 % 2 yields 1 (the remainder of the division)

Number Literals

  • Floating-point literals are written with a decimal point. By default, a floating-point literal is treated as a double type value. For example, 5.0 is considered a double value, not a float value. You can make a number a float by appending the letter f or F, and make a number a double by appending the letter d or D. For example, you can use 100.2f or 100.2F for a float number, and 100.2d or 100.2D for a double number.

Numeric Type Conversion

Consider the following statements:

byte i = 100;

long k = i*3+4;

double d = i*3.1+k/2;

int x = k; //(Wrong)

long k = x; //(fine,implicit casting)


To use comments in java programming, we use the following syntax:

//Single line
/*Multi line*/

Any Questions? Suggestions? Ideas? please share with us by commenting below 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s