Updated date:

Simple Java Codes and Basic Example Programs for Beginners

Here are some exercises and programs for people learning how to write in Java. You can run and test them with any Java compiler. I use NetBeans and Eclipse compilers. I left comments on some of the code for clarity and to explain what the code means. Enjoy learning Java!

Echo in Java

To output words in Java, use the pre-defined function System.out.print(). Here is the complete source code for outputting words in Java.

public class Main {
	public static void main (String[] args) {
		for (String s: args) {
			System.out.print("This is just an example...");
		}
	}
}

By running the above code, the output will be:

This is just an example...

Input and Echo Integers and Strings

The below will show you how to input integers and strings and then call them up using echo. The user input is a declaration of variables. Explanations of the lines are written as comments, after the // symbols.

import java.util.Scanner; //to be able to use scanner predefined function for input

public class Main
{
	public static void main (String[] args)
	{
		
		Scanner input = new Scanner(System.in); //input is an object, you can choose your own.
		
		int num1; //num1 is a variable of integer type
		String word1; //word1 is a variable of string type
		System.out.print("Enter a number and I will echo it later: ");

		num1=input.nextInt(); //read input for num1

		System.out.print("Enter a word and I will echo it later: ");
		
		word1 = input.next(); //read input for word1
		
		System.out.println("Here is the echo of what you have entered:");

		System.out.println("The number you have entered is: " + num1); //echoing input to num1

		System.out.println("The word you have entered is: " + word1); //echoing input to word1
	}
}

The out put of the above will be:

Enter a number and I will echo it later: 88

Enter a word and I will echo it later: hello

Here is the echo of what you have entered:

The number you have entered is 88

The word you have entered is hello

These are just two very simple programs you can do in Java.

Comments

venu on August 31, 2015:

Hi aisha, i am a beginner to java and currently I am in manual testing want to move to automation testing for writing scripts i need to learn oops concepts in java. Could you please suggest me what need to learn and how to find logic for basic programs.

VIJAYA NIRMALA on August 14, 2014:

THIS IS USEFUL FOR BEGINNERS

WE CAN UNDERSTAND THIS WITHOUT ANY TEACHERS

nithi on August 11, 2014:

Thanks for the easy bits. I'll try them out. Voting this Up and Useful.

nithi on August 11, 2014:

Thanks..it is indeed helpful for beginners like me in java!!!

abi on August 11, 2014:

thanks . its very useful to learn

aditya on April 21, 2014:

awsum dude..really awsum

Wendell on February 16, 2014:

Is the format programming of java is different on making an application for android?

ricky on January 08, 2014:

what is the code when you want to split special token..??

Kishori on January 06, 2014:

Thanks..it is indeed helpful for beginners like me in java!!!

ManiMaran on May 23, 2013:

awesome your tutorials it beginner is very use full... thank you

ann on November 17, 2012:

it is nice , because Scanner is the best codes for me ... cause im a codes programmer lover :)

Rasna Aisha (author) from Manila, Philippines on September 22, 2012:

Glad it helps RandelJoseph. :)

Thank you for letting me know it. I'll visit your blog soon

Pexx on September 16, 2012:

This have me smiling so much :)

Just started Java,found your post from another with the link to "this".

I made my first Java program because of this :) I don't mean Hello World,I mean echo's :)

Thanks!

Sub my blog: www.RandelJoseph.blog.com

Rasna Aisha (author) from Manila, Philippines on July 30, 2012:

Hello GodsonT;

Thank you for the kind comment. I hope I can upload more java tutorials too...Anyway, welcome here on hubpages :D

GodsonT from India on July 30, 2012:

Great hub for beginners in Java programming. Got to try it out. Looking for more Java tutorials.

Rasna Aisha (author) from Manila, Philippines on July 21, 2012:

Hi brix,

I uploaded new Java tutorial that might help you, this is my collection of the tutprials,

https://hubpages.com/technology/Java-Tutorial-Exam...

just be patient and continue to learn, time will come you will understand it better :) Good luck.

brix on July 10, 2012:

this java code must have a tutorial for beginners like me so that i know what to do!!!!!!!!!!!!!!!!!!!!!!!!!

Rasna Aisha (author) from Manila, Philippines on February 06, 2012:

Thanks for the vote alocsin and for stopping by :)

Aurelio Locsin from Orange County, CA on February 06, 2012:

Thanks for the easy bits. I'll try them out. Voting this Up and Useful.

Related Articles