Simple Java Programs for Beginners Example: If you’re looking to get into coding, learning Java is a great place to start. It’s a stable and popular programming language that can be used in many different industries. And once you know how to code in Java, it becomes easier to reuse that code for other purposes.
In this article, we will explore some of the Simple Java Programs for understanding the fundamentals of basic Java programming. Java is a versatile language that can be used for building a variety of applications. In order to code in Java, everything must be contained within a class. Once you have created your class, you can save it with a .java extension.
Coding-related questions are common in interviews, so it is beneficial to have a strong foundation in the basics of Java programming. In order to increase your coding skills or knowledge, you can check this Simple Java Program for Beginners with Examples and output.
Post Type: | Java Programs For Beginners |
Published On: | www.softwaretestingo.com |
Applicable For: | Freshers & Experience |
Get Updates: | Join Our Telegram Group |
Basic Simple Java Programs
Basic Simple Java Programs
How to Write Text File Using Java Selenium With Example Program?
package com.selenium.mix; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class WriteTextFile { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File f=new File("G:\\Git_Base_Folder\\softwaretestingblog\\Selenium\\src\\com\\selenium\\mix\\Selenium Text"); FileWriter fw=new FileWriter(f); BufferedWriter bw=new BufferedWriter(fw); bw.write("Welcome to My Testing Blof"); bw.newLine(); bw.write("www.softwareteastingblog.in"); bw.close(); } }
Write a Program To Find out Parent Reference Implementation.
package com.java.Softwaretestingblog; class ab { public void a() { System.out.println("Inside Method A"); } } public class ParentReference extends ab { public void a() { System.out.println("Inside Overriding Method A"); } public void b() { System.out.println("Inside Method B"); } @SuppressWarnings("unused") public static void main(String[] args) { //ab obj=new ab(); //obj.a(); ab abc=new ParentReference(); abc.a(); ParentReference abx=new ParentReference(); //abx.a(); //abx.b(); //ParentReference aby=new ab(); } }
Execution: The Parent Reference Final output link
Output:
Inside Overriding Method A
How to Remove Spaces From Sentence In Java With Example?
package com.java.Softwaretestingblog; public class RemoveSpaceInASentence { public static void main(String[] args) { // Remove Spaces Java Program String str = "For More Testing Interview Questions Visit Software Testing Blog "; System.out.println("Entered String:- "+str); //1. Using replaceAll() Method String strWithoutSpace = str.replaceAll("\\s", ""); System.out.println("Remove Space With Using Replaceall Method:- "+strWithoutSpace); //Output : ForMoreTestingInterviewQuestionsVisitSoftwareTestingBlog //2. Without Using replaceAll() Method char[] strArray = str.toCharArray(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < strArray.length; i++) { if( (strArray[i] != ' ') && (strArray[i] != '\t') ) { sb.append(strArray[i]); } } System.out.println("After Remove The Space From The Sentence:- "+sb); //Output : CoreJavajspservletsjdbcstrutshibernatespring } }
Output:
Entered String:- For More Testing Interview Questions Visit Software Testing Blog Remove Space With Using Replaceall Method:- ForMoreTestingInterviewQuestionsVisitSoftwareTestingBlog After Remove The Space From The Sentence:- ForMoreTestingInterviewQuestionsVisitSoftwareTestingBlog
Write a Program to Find out Widening In Java Example Program?
package com.java.Softwaretestingblog; public class WideningExample { public static void main(String[] args) { // TODO Auto-generated method stub //Conversion lower ro higher is called wideining //byte->short->int->float->long->double int i = 100; //automatic type conversion long l = i; //automatic type conversion float f = l; System.out.println("Int value "+i); System.out.println("Long value "+l); System.out.println("Float value "+f); } }
Output:
Int value 100 Long value 100 Float value 100.0
Non-Repeated & First Repeated Character Using HashMap
How to Find First Repeated & Non-Repeated Character In Java?
package com.softwaretestingblog.programs; import java.util.HashMap; import java.util.Scanner; public class Non_Repeated_Character { static void firstRepeatedNonRepeatedChar(String inputString) { //Creating a HashMap containing char as a key and occurrences as a value HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>(); //Converting inputString to char array char[] strArray = inputString.toCharArray(); //Checking each char of strArray for (char c : strArray) { if(charCountMap.containsKey(c)) { //If char is present in charCountMap, incrementing it's count by 1 charCountMap.put(c, charCountMap.get(c)+1); } else { //If char is not present in charCountMap, //adding this char in charCountMap with 1 as it's value charCountMap.put(c, 1); } } //checking for first non-repeated character for (char c : strArray) { if (charCountMap.get(c) == 1) { System.out.println("First Non-Repeated Character In '"+inputString+"' is '"+c+"'"); break; } } //checking for first repeated character for (char c : strArray) { if (charCountMap.get(c) > 1) { System.out.println("First Repeated Character In '"+inputString+"' is '"+c+"'"); break; } } } public static void main(String[] args) { @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); System.out.println("Enter the string :"); String input = sc.next(); firstRepeatedNonRepeatedChar(input); } }
Write a Program to Find out ReflectionAPI Java Example Program.
package com.java.Softwaretestingblog; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; class Axy { @SuppressWarnings("unused") private void cube(String n) { System.out.println(n); } } public class Reflection_APIEx { public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // TODO Auto-generated method stub @SuppressWarnings("rawtypes") Class c=Axy.class; Object obj=c.newInstance(); @SuppressWarnings("unchecked") Method m=c.getDeclaredMethod("cube",new Class[]{String.class}); m.setAccessible(true); m.invoke(obj,"SoftwareTestingBlog"); } }
Output:
SoftwareTestingBlog
Simple Java Program Object With Example?
Simple Java Program Object With Example?
package com.java.Softwaretestingblog; class top { int b=20; void m1() { System.out.println("Inside Super Class"); } } public class DifferentialObject { public static void main(String[] args) { // TODO Auto-generated method stub new top(); new top().b=30; //System.out.println(b); //Differential Objects Can't be Outside top obj=new top(); obj.b=30; System.out.println("The Value Of B:- "+obj.b); } }
Output:
The Value Of B:- 30
How to Find Pair Elements In an Array In Java With Example Program?
package com.softwaretestingblog.programs; public class PairOfElementsInArray { static void findThePairs(int inputArray[], int inputNumber) { System.out.println("Pairs of elements whose sum is "+inputNumber+" are : "); for (int i = 0; i < inputArray.length; i++) { for (int j = i+1; j < inputArray.length; j++) { if(inputArray[i]+inputArray[j] == inputNumber) { System.out.println(inputArray[i]+" + "+inputArray[j]+" = "+inputNumber); } } } } public static void main(String[] args) { findThePairs(new int[] {4, 6, 5, -10, 8, 5, 20}, 10); /*findThePairs(new int[] {4, -5, 9, 11, 25, 13, 12, 8}, 20); findThePairs(new int[] {12, 13, 40, 15, 8, 10, -15}, 25); findThePairs(new int[] {12, 23, 125, 41, -75, 38, 27, 11}, 50);*/ } }
Output:
Pairs of elements whose sum is 10 are : 4 + 6 = 10 5 + 5 = 10 -10 + 20 = 10
Write a Program to Find Distinct Element Using POJO [Getter & Setter] Class Java?
package com.java.Softwaretestingblog; import java.util.HashSet; public class FindDistinctElement { public static void main(String[] args) { // TODO Auto-generated method stub HashSet<Price> lhm = new HashSet<Price>(); lhm.add(new Price("Banana", 20)); lhm.add(new Price("Apple", 40)); lhm.add(new Price("Orange", 30)); for(Price pr:lhm){ System.out.println(pr); } Price duplicate = new Price("Banana", 20); System.out.println("inserting duplicate object..."); lhm.add(duplicate); System.out.println("After insertion:"); for(Price pr:lhm){ System.out.println(pr); } } } class Price{ private String item; private int price; public Price(String itm, int pr){ this.item = itm; this.price = pr; } public int hashCode(){ System.out.println("In hashcode"); int hashcode = 0; hashcode = price*20; hashcode += item.hashCode(); return hashcode; } public boolean equals(Object obj){ System.out.println("In equals"); if (obj instanceof Price) { Price pp = (Price) obj; return (pp.item.equals(this.item) && pp.price == this.price); } else { return false; } } public String getItem() { return item; } public void setItem(String item) { this.item = item; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String toString(){ return "item: "+item+" price: "+price; } }
Output:
In hashcode In hashcode In hashcode item: Apple price: 40 item: Orange price: 30 item: Banana price: 20 inserting duplicate object... In hashcode In equals After insertion: item: Apple price: 40 item: Orange price: 30 item: Banana price: 20
Write a Program to Find Out Sum Of Digits In Java Program?
package com.java.Softwaretestingblog; import java.util.Scanner; public class SumOfDigits { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.println("Enter the number"); int sum=0,n,a; n=in.nextInt(); while(n>0) { a=n%10; n=n/10; sum=sum+a; } System.out.println("Sum of digits=" +sum); } }
Output:
Enter the number: 1254 Sum of digits=12
How To Open Exe Files In Java Example Program?
package com.java.interviewFAQ; import java.io.IOException; public class RunExeFiles { public static void main(String[] args) throws IOException { Runtime.getRuntime().exec("C:\\Windows\\notepad.exe"); } }
Conclusion:
This Java interview questions and answers tutorial will help you crack your next Java interview. We’ve covered Simple Java Programs questions and answers that are frequently asked by interviewer. With this guide, you’ll be ready to tackle any question thrown your way.
If you’re struggling with these java programs, please leave a comment below. In addition to this article on Simple Java Programs for Beginners, if you come across any other interview questions, please let us know in the comment section and we’ll update the article accordingly.