ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

Simple Java Codes and Basic Example Programs for Beginners

Updated on November 13, 2014

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

    0 of 8192 characters used
    Post Comment

    • profile image

      venu 2 years ago

      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.

    • profile image

      SOWMYA 2 years ago

      IT IS USEFUL TO LEARN JAVA

    • profile image

      VIJAYA NIRMALA 3 years ago

      THIS IS USEFUL FOR BEGINNERS

      WE CAN UNDERSTAND THIS WITHOUT ANY TEACHERS

    • profile image

      nithi 3 years ago

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

    • profile image

      nithi 3 years ago

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

    • profile image

      abi 3 years ago

      thanks . its very useful to learn

    • profile image

      aditya 3 years ago

      awsum dude..really awsum

    • profile image

      Wendell 3 years ago

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

    • profile image

      ricky 3 years ago

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

    • profile image

      Kishori 3 years ago

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

    • profile image

      ManiMaran 4 years ago

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

    • profile image

      ann 5 years ago

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

    • aisha91 profile image
      Author

      Rasna Aisha 5 years ago from Manila, Philippines

      Glad it helps RandelJoseph. :)

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

    • profile image

      Pexx 5 years ago

      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

    • aisha91 profile image
      Author

      Rasna Aisha 5 years ago from Manila, Philippines

      Hello GodsonT;

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

    • GodsonT profile image

      GodsonT 5 years ago from India

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

    • aisha91 profile image
      Author

      Rasna Aisha 5 years ago from Manila, Philippines

      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.

    • profile image

      brix 5 years ago

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

    • aisha91 profile image
      Author

      Rasna Aisha 5 years ago from Manila, Philippines

      Thanks for the vote alocsin and for stopping by :)

    • alocsin profile image

      alocsin 5 years ago from Orange County, CA

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