Categories
java

How to install Java in Windows


Prerequisites

  • Windows 10 Operating System
  • Download Access
  • Oracle account
  • Administrator access on windows

Step 1

Download JDK from Oracle website
URL : https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Open Above URL and navigate to Java SE Development kit

Step 2

Accept license agreement
Click on the jdk-8u231-windows-64.exe

 

Step3

Login into oracle,if you don’t have oracle account signup and download the file

Step 4

Once you downloaded the file, right click and select run as administrator

Step 5

Click Next to initialize your installation

Step 6

If you want to change the JDK installation path click on change button( here we didn’t changed the path
Click Next button to proceed installation

Step 7

If you want to change JRE path click on change button (Here we are not changing the path)
Click Next button to proceed

Step 8

That’s it, we are done with the installation
click on close button to complete your installation

 

That’s it !!

Categories
java oops

Overriding concept in java with example

Overriding in java with example

In this tutorial, we will learn Overriding concept in java with example

Overriding is a feature that allows a child class to override a method which is present in parent class
We should have same method signature of parent class

Example

<span class="token keyword">class</span> <span class="token class-name">SuperClass</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token class-name">String</span> printText<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Super Class: "</span> <span class="token operator">+</span> printText<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">ChildClass</span> <span class="token keyword">extends</span> <span class="token class-name">SuperClass</span> <span class="token punctuation">{</span>

	<span class="token annotation punctuation">@Override</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token class-name">String</span> printText<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Child Class : "</span> <span class="token operator">+</span> printText<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OverridingExample</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">ChildClass</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ChildClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		obj<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"GeeksCoding"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

Output

Child Class : GeeksCoding

Download

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/OverridingExample.java

Categories
java oops

Method Overloading in Java with example

Method Overloading in Java with example

In this tutorial we will learn Method Overloading in Java with example.

Overloading is an example of polymorphism (OOPS concept) , Where we should have more than one method with same name but should have different input parameters or different datatype.

Below we have two methods with same name but different input parameters

<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> x <span class="token operator">+</span> y<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">,</span> <span class="token keyword">int</span> z<span class="token punctuation">)</span> <span class="token punctuation">{</span>
 <span class="token keyword">return</span> x <span class="token operator">+</span> y <span class="token operator">+</span> z<span class="token punctuation">;</span>
 <span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Maths</span> <span class="token punctuation">{</span>

	<span class="token comment">/**
	 * Method with 2 input parameters
	 * 
	 * @param x
	 * @param y
	 * @return
	 */</span>
	<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> x <span class="token operator">+</span> y<span class="token punctuation">;</span>

	<span class="token punctuation">}</span>

	<span class="token comment">/**
	 * method with 3 input parameters with same data type
	 * 
	 * @param x
	 * @param y
	 * @param z
	 * @return
	 */</span>
	<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">,</span> <span class="token keyword">int</span> z<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> x <span class="token operator">+</span> y <span class="token operator">+</span> z<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token comment">/**
	 * Method with 3 input parameters with different data type
	 * 
	 * @param x
	 * @param y
	 * @param z
	 * @return
	 */</span>
	<span class="token keyword">public</span> <span class="token keyword">float</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">,</span> <span class="token keyword">float</span> z<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> x <span class="token operator">+</span> y <span class="token operator">+</span> z<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Maths</span> math <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Maths</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Creating object</span>
		<span class="token keyword">int</span> sum <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// calling sum method with 2 input parameters</span>
		<span class="token keyword">int</span> threeParameter <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Calling sum method with 3 parameters</span>
		<span class="token keyword">float</span> floatSum <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">10.6F</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Calling sum method with different data types</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Result of 2 Parameters "</span> <span class="token operator">+</span> sum<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Result of 3 Parameters "</span> <span class="token operator">+</span> threeParameter<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Result of 3 Parameters with different data types "</span> <span class="token operator">+</span> floatSum<span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token punctuation">}</span>

<span class="token punctuation">}</span>

Output

Result of 2 Parameters 20
Result of 3 Parameters 30
Result of 3 Parameters with different data types 40.6

Download

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/Maths.java

Categories
java

Print a String in java with example

Print a String in java

In this tutorial, we will learn to print a String in java with example


Syntax

System.out.println("");

Example

public class PrintExample {

  public static void main(String[] args) {
    String printingText = "Hello World";
    // Below line will print above string variable
    System.out.println(printingText);
  }
}

Output

Hello World

Reference

https://docs.oracle.com/javase/tutorial/getStarted/application/index.html

Download

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/PrintExample.java

Categories
java

Datatypes in java

Datatypes in java

In this post, we will learn about Datatypes in java and it’s default value

Below are the list of primitive data types

  • int (int i=1;)
  • float (float f=0.10;)
  • char (char c =’d’;)
  • byte (byte by=1;)
  • short (short s =0;)
  • long (long l=3L;)
  • double (double d=10;)
  • boolean (boolean b=false;)
DatatypeDefaultValue
int 0
float 0.0f
char ‘\u0000’
byte 0
short 0
long 0L
double 0.0d
boolean false

Reference

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Categories
java String

Convert String to lowercase using java

Convert String to lowercase using java

In this example we will learn to convert a String to lowercase using java

Syntax

<span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

Example

public class StringLowerCase {

  public static void main(String[] args) {
    String s="A guy from New WORLD ";
    // Below line will convert your string to lower case
    String lowerCase=s.toLowerCase();
    System.out.println(lowerCase);
  }
}

Output

a guy from new world

Reference

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toLowerCase()

Categories
collections java

Check key exists in Hashmap Java

Check key exists in Hashmap Java

In below example we are going to Check key exists in Hashmap java

Syntax

<span class="token function">containsKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

Example

import java.util.HashMap;
import java.util.Map;

public class KeyExistsHashMapExample {

  public static void main(String[] args) {
    Map<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("1", "Car");
    hashMap.put("2", "Bus");
    hashMap.put("3", "Train");
    boolean isKeyAvaillable = hashMap.containsKey("1");
    if(isKeyAvaillable) {
      System.out.println("The value is "+hashMap.get("1"));
    }
    else {
      System.out.println("Key is not availlable");
    }
  }
}

Output

The value is Car

Reference

https://beginnersbug.com/hashmap-in-java/
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#containsKey-java.lang.Object-

Download

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/KeyExistsHashMapExample.java

Categories
java

Helloworld in java

Helloworld in java

In this example we will learn how to print helloworld
Helloworld is the first program in all language by programmers

Prerequisites

  • JDK
  • Eclipse

Example

public class HelloWorld {

	public static void main(String[] args) {
		System.out.println("Helloworld");
	}

}

Output

Helloworld

In above example we printed Helloworld using java
Since Java is a OOPS programming language, so here we bounded all the coding inside class and method

class,main is a keyword in java
System.out.println is used to print a string like printf in c++ language

Download

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/HelloWorld.java

Categories
Interview java

Print non repeated characters in java

Print non repeated characters in java

In this example we will print non repeated characters from a string using java 
This is mostly asked in the java programming interview 

Example

import java.util.ArrayList;

public class NonRepeatedCharcters {

  public static void main(String[] args) {
    String s = "BeginnersBug";
    String nonRepeatedChars = "";
    ArrayList arrayList = new ArrayList();
    char[] charArray = s.toCharArray();

    for (int i = 0; i < charArray.length; i++) {
      // Checking that character already exists on array list
      boolean contains = arrayList.contains(charArray[i]);
      if (!contains) {        
        arrayList.add(charArray[i]);
        nonRepeatedChars += charArray[i];
      }
    }
    System.out.println(nonRepeatedChars);
  }
}

Output

Beginrsu

Array List Reference

 https://beginnersbug.com/arraylist-in-java/

Download Source Code

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/NonRepeatedCharcters.java

Categories
Interview java

Print String reverse using java with example

Print String reverse in java

Print String in reverse using java with example. This is most asked qustion in java interviews In below example we will see that.

Example

public class StringReverseExample {

  public static void main(String[] args) {
    String s = "java";
    int stringLength = s.length();
    // In Below for loop starting will be length-1
    // The end will 0 the index
    for (int i = stringLength - 1; i >= 0; i--) {
      // we used charAt method to get value from the string
      // instead of println we used print to print on same line
      System.out.print(s.charAt(i));
    }
  }
}

Output

avaj

String reverse without for loop

public class StringReverse {

	public static void main(String[] args) {
		String s = "Java";
		// Below we used StringBuilder to print string in reverse
		StringBuilder builder = new StringBuilder(s);
		System.out.println(builder.reverse());
	}

}

Output

avaJ

To know more about StringBuilder, please refer below link
https://beginnersbug.com/stringbuilder-java/

Download

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/StringReverseExample.java