Categories
java

Switch Case in Java with Example

 

Switch Case

  • Switch Case in Java with Example is most used condition based approach in java.
  • It is similar from other programming language like sql, C,C++. 

Syntax


		switch (key) {
		case value:
			break;

		default:
			break;
		}
  • Switch Case will print matching conditions 
  • If none of the condition matches default block will execute 
  • break statement is necessary here 
  • case statement are case sensitive 

Example


public class SwitchCaseExample {

	public static void main(String[] args) {
		try {
			String language = "python";

			switch (language) {
			case "JAVA":
				System.out.println("Java is choosed");
				break;
			case "C++":
				System.out.println("C++ is choosed");
			case "python":
				System.out.println("Python is choosed");
				break;
			default:
				System.out.println("default");
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

Output

Python is choosed

Github Link

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

Categories
java

How to set java home in windows

Prerequisites

Step 1

Copy jdk home path
example: C:\Program Files\Java\jdk1.8.0_171

Step 2

Open My computer

Step 3

Right Click This PC

Step 4

Click on Properties

Step 5

Click on Advanced system settings

Step 6

Click on the Environment variables

Step 7

Click New on the System variables column

Step 8

Enter Variable name as JAVA_HOME
Enter Variable value as C:\Program Files\Java\jdk1.8.0_171
click ok
Note :
Variable name should be CAPS
Variable value should be as your java installed path
don’t copy until bin folder

Step 9

After Click OK
we can see the JAVA_HOME variable in environment variables

Step 10

now we need to need to add this JAVA_HOME to Path variable
Search for Path variable in same system variables window

Step 11

After selecting Path variable
Click on Edit button

Step 12

Edit environment variable dialog box will appear
Click on the New button

Step 13

Text box will appear on the list
add %JAVA_HOME%\bin in the text box

Step 14

Click OK after adding %JAVA_HOME%\bin
Now close all the windows

Step 15

We can verify by below steps
open the command prompt
type java -version

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