Categories
java

Ternary Operator in java with example

Ternary Operator

In this example we will learn to use ternary operator in java with example

  • Ternary Operator will be used instead of if else 
  • We can use this ternary operator to do null check
  • we can use this to check numeric operations

Syntax

(conditon) ? true : false ;

Example with String

public class TernaryOperatorExample {

	public static void main(String[] args) {
		String s = null;
		System.out.println((s == null) ? "String s is null" : "String s is not null");
	}
}

Output

String s is not null

Example with numeric operations

public class TernaryOperatorWithNumeric {

	public static void main(String[] args) {
		int i = 10;
		System.out.println((i > 10) ? true : false);
	}
}

Output

false

Github link

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

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

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 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

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