ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming

Updated on October 31, 2013

Get A Website Plus a Free Domain Name in Just 1 Hour!

Bring the new technology in your hands! Share your skills, improve and impress. Get Your Own Website and a Free Domain Name Here!

Java Programming for Beginners: A Tutorial and guide of the beginners on Java Programming

Based on the comments I have got, I realized that my previous hubs are not enough to guide beginners on their learning about the language. So I decided to make another hub on what should a real beginner must do. May this hub would help the beginners on their path to Java language.

The following are the most basic that a beginner must learn

1. Data types and Declaration of variables

What are the data types on Java and how to use it on the declaration of variables?

The most commonly used data types in Java are int for integers, double for decimal numbers, char for a character, string for group of charaters and Boolean for true or false variable. Data

types are used to declare variables so that a certain a variable can be used to hold string of characters or numbers. You can declare any variable name you want, however, the variable must describe what it should meant to be. For example, num1 is a variable that will hold the first input number and num2 for the second number.

How to declare a variable? The syntax is this,

DataType variableName;

Example:

int num1;

int num2;

Java Source Code Example:

package declationofvariable;


public class Main {

       public static void main(String[] args) {
        
        int num1;
        int num2;

	double num3;
	String name;
    }

}


2. Input and Output in Java

Knowing the input and output in Java is essential on your learning about the language. Before we know about how to input in Java, we must know how to output first the characters or strings in Java.

There is two common ways on how to output in Java:

1.Using System.out.print () – For single line output.

2.Using System.out.println() – For new line output.

Java Source Code Example 1:


package javasample;


public class Main {


    public static void main(String[] args) {

        System.out.println("Hi! This Is It How It Works!!!");
        System.out.print("Is it exciting? :)");
    }

}
Java Source Code "Example1" Output
Java Source Code "Example1" Output

To input in Java you must declare an object using the Scanner pre-defined function. Object is what you call to access any function in Java. Pre-defined functions are those built in functions of the language. You can name your own Object as well as you can also make your own function.

Java Source Code Example2:


package javasample;

import java.util.Scanner;


public class Main {


    public static void main(String[] args) {

        System.out.println("This Is How to Input in an integer and string in Java");

            int num1; //declare variable to hold first input
            String string = " "; // variable for sting input

       Scanner input = new Scanner(System.in);
       System.out.print("Please Enter Your Name: ");
       string = input.next();// input for string
       System.out.print("Please Enter a Number: ");
       num1 = input.nextInt(); // input for integer
       
        System.out.println(string + " You enter a number " + num1 + " :D");

    }

}
Java Source Code "Example2" Output
Java Source Code "Example2" Output

3. Java Operators

There are 4 kinds of operators in Java, the arithmetic operators, assignment operators, comparison operators and logical operators.

Arithmetic Operators:

+ - Add
- - Subtract
* - Multiply
/ - Divide
% - Remainder for Division

Assignment Operators:

 Assignment Operators      Example	      Elaboration
 =  			     X=Y	     X is equal to Y
*= 			     X*=Y                X=X*Y
+=			     X+=Y                X=X+Y
/=			     X/=Y                X=X/Y
-=			     X-=Y		 X=X-Y
%=			     X%=Y		 X=X%Y

Comparison Operators:

== equal for comparison
!= not equal
>  greater than
<  less than
>= greater than equal
<= less than equal

Logical Operators:

&& and
!  not
|| or

4. If-else Statement

If and else statement is use to execute a code that has 2 or more conditions.

Syntax:

if(true condition here)
{
	If true Code here
}
else
{
	If false Code here
}

Java Source Code Example3:

package javasample;

import java.util.Scanner;


public class Main {


    public static void main(String[] args) {

        System.out.println("This Is How to Input an integer and string in Java");

            int num1; //declare variable to hold first input
            String string = " "; // variable for sting input

       Scanner input = new Scanner(System.in);
       System.out.print("Please Enter Your Name: ");
       string = input.next();// input for string
       System.out.print("Please Enter a Number: ");
       num1 = input.nextInt(); // input for integer
       
       if(num1 < 500)
       {
            System.out.println(string + " You enter a number that is less than 500 :D");
       }
       else
       {
            System.out.println(string + " You enter a number that is greater than 500 :D");
       }
       
    }

}

Sample Output:

Java Source Code "Example3" Output
Java Source Code "Example3" Output

5. Looping

Looping is use to execute a code multiple times. Loop can be finite or infinite. There are 3 kinds of looping in Java. The for loop, do-while loop and while loop.

For Loop Syntax:

for(initialization; condition; increment )
{
	Codes here
}

For Loop Example:

package javasample;

import java.util.Scanner;


public class Main {


    public static void main(String[] args) {

            int num1; //declare variable to hold first input
            String string = " "; // variable for sting input

       Scanner input = new Scanner(System.in);
       System.out.print("Please Enter Your Name: ");
       string = input.next();// input for string
       System.out.print("Please Enter a Number: ");
       num1 = input.nextInt(); // input for integer

       System.out.println(string + " the even numbers of number " + num1 + " are:");
       

       for(int i = 1; i <= num1; i++)
       {
           int x = 1;
           System.out.print((i+=x) + " ");
       }
       
    }

}

Sample Output:

Do-While Loop Syntax:

do
{
	
  Code to repeat multiple times till the condition is met...

}while(condition to met);

Do While Loop Example and Character Input Example:


package javasample;

import java.io.IOException;
import java.util.Scanner;


public class Main {


    public static void main(String[] args) throws IOException {

     
            int num1; //declare variable to hold first input
            String string = " "; // variable for sting input
            char ans;
           

       Scanner input = new Scanner(System.in);

       
       System.out.print("Please Enter Your Name: ");
       string = input.next();// input for string

        do
       {
       System.out.print("Please Enter a Number: ");
       num1 = input.nextInt(); // input for integer

       System.out.println(string + " the even numbers of number " + num1 + " are:");
       

       for(int i = 1; i <= num1; i++)
       {
           int x = 1;
           System.out.print((i+=x) + " ");
       }
       System.out.println();
       System.out.print("Want to enter again?[Y/N]: ");

       ans = (char) System.in.read();// to read the input character

       }while(ans == 'y' || ans == 'Y');
    }

}

Sample Output:

Do while Loop and Character Input Example
Do while Loop and Character Input Example

While Loop Syntax:

While(condition to be met)
{
	Code to be executed if the condition is me
}

6. Java Arrays

Arrays are used to hold series of variable and input. Index is what you call to the variables that the array is holding. Index starts at 0. Example: if there are 5 numbers in an array, the index count would only be 4 because the first variable will be the index index 0, the second variable would be index 1 and so on.

To declare an array in Java is like this:

DataType[] arrayname = new DataType[size of the array];

Example:

int[] integers = new int[2];

This array hold two indexes, the index 0 and the index 1.

Example for Java Array:Selection Sort

Comments

    0 of 8192 characters used
    Post Comment

    • perrya profile image

      perrya 4 years ago

      Too easy.

    • aisha91 profile image
      Author

      Rasna Aisha 4 years ago from Manila, Philippines

      That is right perrya, because you are not a beginner eh :) This is intended for those who do not know about the language and to those who have difficulties on starting out on learning java programming. Anyway thanks for stopping by. Have a happy day.

    • Alma Cabase profile image

      Alma Cabase 4 years ago from Philippines

      Of course it should be easy. It's meant for beginners in the first place. This is an important hub for promising java programmers out there. Maybe you can publish some contents about advanced java-based systems to satisfy the needs of experienced java programmers.

      This hub is really good. Keep it up!

    • aisha91 profile image
      Author

      Rasna Aisha 4 years ago from Manila, Philippines

      Thank you very much Alma for the encouraging comment. I will in the future...:)

    • profile image

      Jimmy 4 years ago

      Very well written hub ... Interesting, All the loops and branching statements at one place .... Thanks for sharing these helpful topics with us ! .... Thanks for sharing such a wonderful information ... Keep the good work up ... I am also a blogger, a writer and share for a website http://www.hubberspot.com .... Kindly have a look at it ...

      Kindly share more of such important topics over programming language .... Have a great future ahead ... God blesss ....

    • jeugenejohn profile image

      jeugenejohn 4 years ago from Kerala

      Interesting and informative hub

    • dwachira profile image

      [ Danson Wachira ] 4 years ago from Nairobi, Kenya

      Hi aisha91,

      I find a lot of students doing better in programming if they have the basics and primary guidance in programming. This article is such a beginners guide and i will recommend it to anyone aspiring to be in programming and need to kick start on introduction. Vote up and shared with followers and on Google+.

    • ib radmasters profile image

      ib radmasters 4 years ago from Southern California

      Aisha91

      I didn't read your previous hubs but it would be helpful to know what version or versions of Java are you using for the hub.

      And which is the best version of Java to eventually program applets for Android phones.

      Thanks

    • aisha91 profile image
      Author

      Rasna Aisha 4 years ago from Manila, Philippines

      hello dwachira,

      When I am a total beginner in Java, those were the things I really want to learn first and I realized that learning those 5 first is really a good foundation to learn Java better. I appreciate your comment dwachira and thank you for sharing it.

    • aisha91 profile image
      Author

      Rasna Aisha 4 years ago from Manila, Philippines

      hi ib radmasters,

      I didn't update my Netbeans IDE yet. I am using Netbeans 6.9 now. Currently, Netbeans latest version is 7.3. As for the best version of Java for Android development, I still need to research on that. Lol, I might write a hub for that. Thanks for giving me the idea. Thank you for stopping by.

    • profile image

      hardy 3 years ago

      this is interesting excuteble....nice thing which i learn from yr website thank you

    • profile image

      khirod 2 years ago

      kul !!!!!

      thanku fo the program....................needed more of it

    Click to Rate This Article