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 4 years ago

      awsum dude..really awsum

    • profile image

      Wendell 4 years ago

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

    • profile image

      ricky 4 years ago

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

    • profile image

      Kishori 4 years ago

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

    • profile image

      ManiMaran 5 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 6 years ago from Manila, Philippines

      Thanks for the vote alocsin and for stopping by :)

    • alocsin profile image

      alocsin 6 years ago from Orange County, CA

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

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: "https://hubpages.com/privacy-policy#gdpr"

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)