Tuesday, 16 October 2018

How to resolve import org JUnit in Eclipse

// Resolve import org JUnit in Eclipse
// EvenOdd.java
public class EvenOdd {

public boolean isEvenNumber(int number){
       
        boolean result = false;
        if(number%2 == 0){
            result = true;
        }
        return result;
    }
}

// JunitTestDemo.java
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class JunitTestDemo {

@Test
    public void testEvenOddNumber(){
EvenOdd eo = new EvenOdd();
        assertEquals("10 is a even number", true, eo.isEvenNumber(10));
    }
}
Live demo: https://www.youtube.com/watch?v=_l4-qFrVuw4

How to run java program in command prompt

// Run java program in command prompt
public class Fibonacci{
public static void main(String[] args){

int i, fibCount = 12;
int[] fib = new int[fibCount];

fib[0] = 0;
fib[1] = 1;

for(i=2; i<fibCount; i++){

fib[i] = fib[i-1]+fib[i-2];
}

System.out.print("Fibonacci series:");

for(i=0; i<fibCount; i++){

System.out.print(" "+fib[i]);
}
}
}
O/p : 0 1 1 2 3 5 8 13 21 34 55 89
Live demo: https://www.youtube.com/watch?v=COL2L-uYF7I

Friday, 31 August 2018

Basic core java programs

// Data type example

public class MyProgram {

public static void main(String[] args) {
// TODO Auto-generated method stub

int  i = 111;
double y = 11.1234;
char ch1 = '!';
long z = 22222222L;

System.out.println(i);
System.out.println(y);
System.out.println(ch1);
System.out.println(z);
}
}

/*
Output:

111
11.1234
!
22222222

*/

// Generate a random number b/w 0, 1, 2, 3, 4, 5, 6 and print 0- Sunday, 1- Monday

public class Day {

public static void main(String[] args) {
// TODO Auto-generated method stub

int num = (int) (Math.random()*7);

if(num==0){
System.out.println("Sunday\n");
}

if(num==1){
System.out.println("Monday\n");
}

if(num==2){
System.out.println("Tuesday\n");
}

if(num==3){
System.out.println("Wednesday\n");
}

if(num==4){
System.out.println("Thursday\n");
}

if(num==5){
System.out.println("Friday\n");
}

if(num==6){
System.out.println("Saturday\n");
}
}
}


/*
Output: Wednesday

*/

// Find biggest of 2 numbers by using Unary operators?

public class BiggestNumber
{
public static void main(String [] args)
{
int num1=55;
int num2=43;
System.out.println((num1>num2)?(num1):(num2));
}
}

/*
Output: 55

*/

See full: Core Java Programs and Examples

// Free fall example of switch case?

public class SwitchDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub

int i=10;

switch(i)
{
case 5: System.out.println("1");
break;

case 10: System.out.println("2");

case 11: System.out.println("3");

case 12: System.out.println("4");

case 13: System.out.println("5");

case 14: System.out.println("6");
break;

default: System.out.println("0");
break;
}
}
}

/*
Output:

2
3
4
5
6
*/

// Find a prime numbers?

public class Prime {

public static void main(String[] args) {


int num =13;
boolean  isPrime= true;
int i=2;

while(i<num-1)
{
if(num % 2==0)
{
isPrime =false;
break;
}
i++;
}
if(isPrime ==false)
{
System.out.print("Not a prime number");
}else{
System.out.print("Prime number");
}
}
}
/*
Output: Not a prime number

*/

// Second word reverse?

public class SecondWordReverse {

public static void main(String[] args) {
// TODO Auto-generated method stub

String s = "Info Tech Android Java";
int first=s.indexOf(' ');
int next=s.indexOf(' ', first+1);

System.out.println("Enter name is " +s);

System.out.print(s+ " second reverse word: ");

for(int i=next-1 ; i>first; i--)
{
System.out.print(s.charAt(i));
}
}
}

/*
Output:

Enter name is Info Tech Android Java
Info Tech Android Java second reverse word: hceT

*/

// Count words?

public class CountWords {

public static void main(String[] args) {
// TODO Auto-generated method stub

String s = "Info Technologies Bangalore India";
int count=1;

System.out.println("Enter name is " +s);

System.out.print(s+ " : ");

for(int i=0; i<=s.length()-1; i++)
{
char ch = s.charAt(i);
if(ch==' ')
{
count++;
}
}
System.out.print(count+ " words");
}
}


/*
Output:

Enter name is Info Technologies Bangalore India
Info Technologies Bangalore India : 4 words

*/

See full: Core Java Programs and Examples

// Print 8 16 10 20 12 24 14 ...10 elements using while loop?

public class SeriesDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub

int i=8;
int j=16;
int count = 0;

while (count<=9)
{
System.out.print(" "+i);
System.out.print(" "+j);

i=i+2;
j=j+4;
count++;
}
}
}

/*
Output: 8 16 10 20 12 24 14 28 16 32 18 36 20 40 22 44 24 48 26 52

*/

// Fibonacci numbers?

public class FibonacciNumber {

public static void main(String[] args) {
// TODO Auto-generated method stub

int first=0;
int second=1;
// Fibonacci series
System.out.print("// Fibonacci series\n");
System.out.print(" " +first);
System.out.print(" " +second);

int sum = first + second;

while (sum<=100)
{
System.out.print(" " +sum);
first = second;
second = sum;
sum = first + second;
}
}
}

/*
Output:

// Fibonacci series
 0 1 1 2 3 5 8 13 21 34 55 89

*/

// Armstrong number

public class ArmstrongNumber {

public static void main(String[] args) {
// TODO Auto-generated method stub

// Armstrong number

int num= 153;
int temp = num;
int sum = 0;
System.out.println("// Armstrong number\n");
while (num>0)
{
int l = num%10;
sum = sum +(l*l*l);
num = num/10;
}
if (temp == sum)
{
System.out.println("Armstrong number: "+temp);
}
else
{
System.out.println("Not armstrong number: "+temp);
}
}
}

/*
Output:

// Armstrong number

Armstrong number: 153

*/

// Infinite loop examples

public class InfiniteDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub

int i=1;

while(true){
System.out.println(" "+i);
i++;
}

}
}


/*
Output: 1 2 3...+ 2 billions ...-2 billions ..

*/

See full: Core Java Programs and Examples

Core Java Interview Questions

Q. What is Java?
A. Java is "Object oriented" programing language using which you can develop applications.

Q. What type of program we can build using Java?
A. 1. We can create website like facebook and gmail.com
2. We can create standalone application. Ex. note, microsoft word, paint
3. We can create E-commerce application like flipkart, amazon, paytm
4. We can create banking application
5. We can create mobile application

Q. What is platform?
A. Platform = OS+CPU

Q. What is JVM, JRE, JIT and JDK?
A. Java Virtual Machine (JVM) is an abstract computing machine. JVM becomes an instance of JRE at runtime of a Java program. It is widely known as a runtime interpreter.

Java Runtime Environment (JRE) along with various development tools like Java libraries, Java source compilers, Java debuggers, bundling and deployment tools.

Just In Time compiler (JIT) is runs after the program has started executing, on the fly. It has access to runtime information and makes optimizations of the code for better performance.

Java Development Kit (JDK), contains JRE along with various development tools like Java libraries, Java source compilers, Java debuggers, bundling and deployment tools.

Q. What is Java compiler?
A. In java compiler, its provide two types of code in a file, language and byte code.
Ex.

add r1 r2 r3
------------
010101010101

Q. What is JVM?
A. In which complete language and byte code in a single byte code format which is executed by processor.

Q. Which one is faster, static method or instance method?
A. Static methods are faster than instance methods, because they don't use virtual pointer concept internally.

Q. Which of the below is not a proper naming convention in java?
A. Method & class name should start with capital letter. and every word also should start with capital letters.

Q. How many times a static variable will be initialized? Assuming that we are initializing that variable out side the constructor.
A. Static variable will be created and initialized only once that is at program loading time.

Q. How many public classes are allowed in a java file?
A. You can have more than one public outer class in a java file.

Q. Explain public static void main(String args[]).
A. public static void main(String args[])

public, is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class.

static, It is a keyword in java which identifies it is class based i.e it can be accessed without creating the instance of a Class.

void, It is the return type of the method. Void defines the method which will not return any value.

main, It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.

String args[], It is the parameter passed to the main method.

See full: Core Java Interview Questions

Q. What are the core concepts of OOPS?
A. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts.

Core OOPS concepts are following

1) Class: A class is a blueprint or prototype that defines the variables and the methods (functions) common to all objects of a certain kind.

2) Object: Objects have states and behaviors. An object can be defined as an instance of a class or object is a specimen of a class.

3) Inheritance: Inheritance is a mechanism wherein a new class is derived from an existing class. classes may inherit or acquire the properties and methods of other classes.

4) Polymorphism: Polymorphism refers to the ability of a variable, object or function to take on multiple forms.

5) Abstraction: An abstraction is an act of representing essential features without including background details.

6) Encapsulation: Encapsulation is an OOP technique of wrapping the data and code.

7) Association: Association is a relationship between two objects. It defines the diversity between objects.

8) Aggregation: In this technique, all objects have their separate lifecycle.

9) Composition: A composition is a specialized form of Aggregation. It is also called "death" relationship. Child objects do not have their life-cycle so when parent object deletes all child object will also delete automatically.

Q. What is WeakHashMap?
A. WeakHashMap is an implementation of the Map interface that stores only weak references to its keys.

Storing only weak references allows a key-value pair to be garbage-collected when its key is no longer referenced outside of the WeakHashMap.

Q. Why does HashSet implementation in Sun Java use HashMap as its backing?
A. In order to reduce Duplication of code and made it Memory Efficient ,HashSet implements HashMap as its backing.

Q. What interfaces are implemented by the HashSet Class ? Which is the superclass of HashSet Class?
A. HashSet Class implements three interfaces that is  Serializable ,Cloneable and Set interfaces.

AbstractSet is the superclass of HashSet Class .

See full: Core Java Interview Questions

Q. Differences between synchronized and volatile keyword in Java?
A. Differences between synchronized and volatile keyword:

1. Synchronizes the whole of thread memory with "main" memory whereas volatile only synchronizes the value of one variable between thread memory and "main" memory.
2. Volatile is a field modifier, while synchronized modifies code blocks and methods.
3. Volatile in java is a keyword which is used in variable declaration only and cannot be used with method.
4. Volatile variables read values from main memory and not from cached data so volatile variables are not cached whereas variables defined inside synchronized block are cached.
5. Volatile variables are lock free which means that it does require any lock on variable or object whereas synchronized requires lock on method or block.
6. Volatile variable can be null whereas we will get NullPointerException in case we use null object.
7. Using synchronization thread can be blocked when waiting for other thread to release lock but not in the case of volatile keyword.

Q. How to use volatile with variable?
A. volatile Boolean flag;

Q. How to use Synchronized method and Synchronized block?
A.
synchronized void executeMethod()
{
//write code inside synchronized method
}

Synchronized block:
void executeMethod()
{
synchronized (this)
{
//write code inside synchronized block
}
}

Q. What is significance of using Volatile keyword?
A. Using volatile is yet another way (like synchronized, atomic wrapper) of making class thread safe.

Q. What is Thread safe?
A. Thread safe means that a method or class instance can be used by multiple threads at the same time without any problem.

Q. Can you again start Thread?
A. No. After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown.

Q. Enum V/s Switch?
A. An enum is a (typically small) set of specific values. A switch is a method of controlling program flow to execute certain code based on a particular value.

Enum's are type-safe can be used in switch cases.

Q. enum V/s Enum?
A.
An enum type, also called enumeration type, is a type whose fields consist of a fixed set of constants. The purpose of using enum type is to enforce type safety.

Whereas java.lang.Enum is an abstract class, it is the common base class of all Java language enumeration types. The definition of Enum is:

Q. Enum V/s Inheritance?
A. Enum can not extend any class in java, the reason is by default, Enum extends abstract base class java.lang.Enum. Since java does not support multiple inheritance for classes, Enum can not extend another class.

Q. Enum V/s Constructore?
A. enum is a keyword. Enum constructors are always private or default.

Q. Enum V/s Enumeration?
A. Enumeration is legacy Iterator and Enum is a data type.

See full: Core Java Interview Questions


Monday, 21 May 2018

Reverse element of an array

public class ArrayReverse {

public static void main(String[] args) {
// Reverse element of an array

int[] arr = new int[4];

System.out.println("// Reverse element of an array");
for(int k=0; k<arr.length; k++)
{
arr[k] = (int) (Math.random()*100);
System.out.print(" " +arr[k]);
}
System.out.println();
System.out.print("------------");

for(int i=0; i<arr.length/2; i++)
{
int temp = arr[i];
arr[i] = arr[arr.length - 1-i];
arr[arr.length - 1-i] = temp;
}
System.out.println();

for(int j=0; j<arr.length; j++)
{
System.out.print(" " +arr[j]);
}
}
}
Output:

Find smallest and largest value in a given array

public class ArrayEx {

public static void main(String[] args) {
// TODO Auto-generated method stub

// find smallest and largest value in a array
int[] arr = {11,22, 33, 44, 55};

int smallest=999;
int largest=0;

System.out.println("// find smallest and largest value in a array\n");

for(int i=0; i<arr.length; i++ )
{
// find largest values
if(largest<arr[i]){
largest = arr[i];
}
// find smallest values
if(smallest>arr[i]){
smallest = arr[i];
}
}
System.out.println(largest);
System.out.println(smallest);
}
}
Output:

Block initialization in a java

Blocks.java

public class Blocks {

public int i=10;
public static int j=20;

public Blocks(){
System.out.println("1");
}

public void f1(){
System.out.println("2");
}

{
System.out.println("IB");
}

{
System.out.println("SIB");
}
}

MyProgram.java
public class MyProgram {

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("// Block initialization in a java ");

Blocks b1 = new Blocks();

System.out.println(b1.i);
System.out.println(Blocks.j);

b1.f1();
}
}

Output:



Prime numbers given in a range

public class PrimeNumber {

public static void main(String[] args) {
// TODO Auto-generated method stub

int i =0;
       int num =0;
       //Empty String
       String  primeNumbers = "";

       for (i = 1; i <= 100; i++)       
       {    
          int counter=0;  
          for(num =i; num>=1; num--)
  {
             if(i%num==0)
     {
counter = counter + 1;
     }
  }
  if (counter ==2)
  {
     //Appended the Prime number to the String
     primeNumbers = primeNumbers + i + " ";
  }
       }
       System.out.println("Prime numbers from 1 to 100 are :");
       System.out.println(primeNumbers);
   }
}
Output:

Sunday, 20 May 2018

Armstrong number

public class ArmstrongNumber {

public static void main(String[] args) {
// TODO Auto-generated method stub

// Armstrong number

int num= 153;
int temp = num;
int sum = 0;
System.out.println("// Armstrong number\n");
while (num>0)
{
int l = num%10;
sum = sum +(l*l*l);
num = num/10;
}
if (temp == sum)
{
System.out.println("Armstrong number: "+temp);
}
else
{
System.out.println("Not armstrong number: "+temp);
}
}
}
Output:


Number series

public class Series {

public static void main(String[] args) {

// Print 8 16 10 20 12 24 14 .. 10 elements of the series

int i = 8;
int j = 16;
int count = 0;
System.out.print("// Print 8 16 10 20 12 24 14 .. 10 elements of the series\n");
while (count<=9)
{
System.out.print(" "+i);
System.out.print(" "+j);
i = i+2;
j = j+4;
count++;
}

System.out.print("\n\n");
// Print 33 37 41 .. 10 elements of the series

int k = 33;
int counter = 0;

System.out.print("// Print 33 37 41 .. 10 elements of the series\n");
while (counter<=9)
{
System.out.print(" "+k);
k = k+4;
counter++;
}
}
}
Output:


Palindrome number

public class Palindrom {

public static void main(String[] args) {
// TODO Auto-generated method stub

// Palindrome number
int num=151;
int temp=num;
int rev=0;

System.out.println("// Palindrome number");
while(num>0){
int last = num%10;
rev = (rev*10)+last;
num = num/10;
}
if(temp==rev)
{
System.out.println("Number is Palindrom number");
}
else
{
System.out.println("Not a Palindrom number");
}
}
}
Output:


Prime number

public class PrimeNumber {

public static void main(String[] args) {
// TODO Auto-generated method stub
// Prime number
int num = 13;
int i=2;
int temp=num;
boolean isPrime=true;

System.out.println("// Prime number");
while(i<num-1)
{
if(num%i==0)
{
isPrime=false;
break;
}
i++;
}
if(isPrime == false)
{
System.out.println("Number not a prime number");
}
else
{
System.out.println("Number is Prime number");
}
}
}
Output:

Fibonacci series

public class FibonacciNumber {

public static void main(String[] args) {
// TODO Auto-generated method stub

int first=0;
int second=1;
// Fibonacci series
System.out.print("// Fibonacci series\n");
System.out.print(" " +first);
System.out.print(" " +second);

int sum = first + second;

while (sum<=100)
{
System.out.print(" " +sum);
first = second;
second = sum;
sum = first + second;
}
}
}
Output:

Core Java pattern programs

import java.util.Scanner;

public class Pattern {

public static void main(String[] args) {
// TODO Auto-generated method stub

int num=0;
//Pattern example
//Taking input from keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter the row number");
num = sc.nextInt();

System.out.println("// Pattern \n");
for(int i=0; i<num;i++){
for(int j=0; j<=i;j++){
System.out.print("*");
}
System.out.print("\n");
}
}
}
Output:
import java.util.Scanner;

public class Pattern1 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int num=0;
//Pattern1 example
//Taking input from keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter the row number");
num=sc.nextInt();

System.out.println("// Pattern1 \n");
for(int i=0; i<num;i++)
{
for(int j=num; j>i;j--)
{
System.out.print("*");
}
System.out.print("\n");
}
}
}
Output:
import java.util.Scanner;

public class Pattern2 {

public static void main(String[] args) {
// TODO Auto-generated method stub

int num=0;
//Pattern2 example
//Taking input from keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter the row number");
num=sc.nextInt();

System.out.println("// Pattern2 \n");
for(int i=1; i<=num;i++)
{
for(int j=1; j<=i;j++)
{
System.out.print(j);
}
System.out.print("\n");
}

}

}
Output:

See full: Core Java Pattern Programs

import java.util.Scanner;

public class Pattern3 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int num=0;
int count = 1;
//Pattern3 example
//Taking input from keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter the row number");
num=sc.nextInt();
System.out.println("// Pattern3 \n");
for(int i=1; i<=num;i++)
{
for(int j=1; j<=i;j++)
{
System.out.print(count+" ");
count++;
}
System.out.print("\n");
}
}
}
Output:
import java.util.Scanner;

public class Pattern4 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int num=0;
//Pattern4 example
//Taking input from keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows");
num = sc.nextInt();
System.out.println("// Pattern4 \n");
for(int i=1; i<=num;i++)
{
char ch = 'a';
for(int j=1; j<=i;j++)
{
System.out.print(ch);
ch++;
}
System.out.print("\n");
}

}

}
Output:
import java.util.Scanner;

public class Pattern5 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int num=0;
char ch = 'a';
//Pattern5 example
//Taking input from keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows");
num = sc.nextInt();
System.out.println("// Pattern5 \n");
for(int i=1; i<=num;i++)
{
for(int j=1; j<=i;j++)
{
System.out.print(ch);
ch++;
}t
System.out.print("\n");
}

}

}
Output:

See full: Core Java Pattern Programs

import java.util.Scanner;

public class Pattern6 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int num=0;
//Pattern6 example
//Taking input from keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows");
num = sc.nextInt();
System.out.println("// Pattern6 \n");
for(int i=1; i<=num;i++)
{
for(int j=1; j<=num;j++)
{
if(i+j>num){
System.out.print("*");
}else
{
System.out.print(" ");
}
}
System.out.print("\n");
}
}
}
Output:
import java.util.Scanner;

public class Pattern7 {

public static void main(String[] args) {
// TODO Auto-generated method stub

int num=0;
//Pattern7 example
//Taking input from keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows");
num = sc.nextInt();

System.out.println("// Pattern7 \n");
for(int i=1; i<=num; i++)
{
for(int j=1; j<=num; j++)
{
System.out.print(i);
}
System.out.print("\n");
}
}

}
Output:
import java.util.Scanner;

public class Pattern8 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int num=0;
//Pattern8 example
//Taking input from keyboard
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows");
num = sc.nextInt();
System.out.println("// Pattern8 \n");
for(int i=1; i<=num; i++)
{
for(int j=1; j<=(num+i)-1; j++)
{
if(i+j<(num+1)){
System.out.print(" ");
}else
{
System.out.print("*");
}
}
System.out.print("\n");
}

}

}
Output:
 import java.util.Scanner;

public class Pattern9 {

public static void main(String[] args) {
// TODO Auto-generated method stub

int start=0;
int end=0;
//Pattern9 example
for(int i=1; i<=4; i++)
{
start = i;
end = 8-i;
for(int j=1; j<8; j++)
{
if(j<=start || j>=end){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.print("\n");
}

}

}
Output:




Reverse string using different way

import java.util.Scanner;

public class ReverseDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);
System.out.println("Please enter string");
String s = sc.nextLine();

// Simple use charAt method
System.out.print("// Simple use charAt method\n");
for(int i=s.length()-1; i>=0; i--){
System.out.print(s.charAt(i));
}
System.out.println("\n");

// Use charArray
System.out.print("// Use charArray\n");
char ch[] = s.toCharArray();
for(int i=ch.length-1; i>=0; i--)
{
System.out.print(ch[i]);
}
System.out.println("\n");

// Use StringBuilder
System.out.print("// Use StringBuilder and charArray\n");
StringBuilder sb = new StringBuilder();
for(int i=ch.length-1; i>=0; i--)
{
sb.append(ch[i]);
}
System.out.print(""+sb.toString());

System.out.println("\n");

// Use StringBuilder and reverse method
System.out.print("// Use StringBuilder and reverse method\n");
StringBuilder sbr = new StringBuilder(s);
String str = sbr.reverse().toString();
System.out.print(str);

System.out.println("\n");

// Reverse using recursion no inbuilt method
System.out.print("// Reverse using recursion no inbuilt method\n");
char str1[] = null;
str1 = s.toCharArray();
int size = str1.length;
reverse(str1, 0, size - 1);
System.out.print(str1);
}

public static void reverse(char str[], int stringIndex, int stringSize){

char temp;

temp = str[stringIndex];
str[stringIndex] = str[stringSize - stringIndex];
str[stringSize - stringIndex] = temp;

if (stringIndex == stringSize / 2)
{
return;
}

// Use recursion
reverse(str, stringIndex + 1, stringSize);
}
}

Output:


Saturday, 17 March 2018

Swap two numbers?

// Method 1
public class Swap
{
public static void main(String [] args)
{
int num1=42;
                int num2=22;
                int temp = 0;
                System.out.println("---Before swap numbers is");
                    System.out.println("Number1: "+num1+"Number2: "+num2);
                temp = num1;
                num1=num2;
                num2=temp;
                   System.out.println("---After swap numbers is");
System.out.println("Number1: "+num1+"Number2:

"+num2);
}
}

// Method 2 without temp
public class Swap
{
public static void main(String [] args)
{
int num1=42;
                int num2=22;
           
                num1 = num1+num2;
                num2=num1-num2;
                num1=num1- num2;
             
System.out.println("Number1: "+num1+"Number2:

"+num2);
}
}

// Method 3
public class Swap
{
public static void main(String [] args)
{
int num1=42;
                int num2=22;
           
                num1 = num1*num2;
                num2=num1/num2;
                num1=num1/ num2;
             
System.out.println("Number1: "+num1+"Number2:

"+num2);
}
}

// Method 4
public class Swap
{
public static void main(String [] args)
{
int num1=42;
                int num2=22;
           
                num1 = num1^num2;
                num2=num1^num2;
                num1=num1^num2;
             
System.out.println("Number1: "+num1+"Number2:

"+num2);
}
}


Find number is even/odd in single statement?

public class EvenOdd
{
// Find number is even/odd in single statement
        public static void main(String [] args)
{
int num=12;
System.out.println((num%2==0)?"Even":"Odd");
}
}


Find biggest of 2 numbers in single statement?

public class BiggestNumber
{
        // Find biggest of 2 numbers in single statement
public static void main(String [] args)
{
int num1=55;
int num2=43;
System.out.println((num1>num2)?(num1):(num2));
}
}


How to run java program from command prompt?

Follow below step by step process:-
1. Create a folder C:\java program.
2. Using Notepad or notepad+ text editor, create a small Java file HelloWorld.java
public class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.println("Hello, World!");
  }
}
Save your file as HelloWorld.java in java program folder.

3. Click on start and type command prompt in search and press enter. Command Prompt will open
4. Run Command Prompt 



- Type C:\> cd \java program
This makes C:\java program the current directory.
- Type C:\> \java program\dir
This directory show HelloWorld.java among the files.
- Type C:\> \java program\set path=%path%;C:\Program Files\Java\jdk1.8.0_144\bin
This tells the system where to find JDK programs. Setting path be carefull otherwise system got crash
C:\java program> javac HelloWorld.java
This runs javac.exe, the compiler. If any error please check text file and set path
-Type C:\> \java program\dir
javac has created the HelloWorld.class file.  You should see HelloWorld.java and HelloWorld.class among the files in same location.
-Type C:\java program> java HelloWorld
This runs the Java interpreter.  You should see the program output:
Hello, World!

Saturday, 10 February 2018

Java Introduction

James Gosling, Mike Sheridan, and Patrick Noughton are the actual developers of Java language project in 1991. Java language was initially called Oak and was later renamed Java, from Java coffee. Sun Microsystems released the first public implementation as java 1.0 in 1995.

Java versions:

  • JDK 1.0 (January 21, 1996)
  • JDK 1.1 (February 19, 1997)
  • J2SE 1.2 (December 8, 1998)
  • J2SE 1.3 (May 8, 2000)
  • J2SE 1.4 (February 6, 2002)
  • J2SE 5.0 (September 30, 2004)
  • Java SE 6 (December 11, 2006)
  • Java SE 7 (July 28, 2011)
  • Java SE 8 (March 18, 2014)

Java features:

  1. Java is platform independent
  2. Java language is easy to learn
  3. Java is Object oriented programming language
  4. Java can be used to build heavy enterprise applications
  5. Java is open source software.
  6. With java you can write secure and robust applications
  7. Architecture neutral
Advantage of Java:

  • Get started quickly
  • Write less code
  • Write better code
  • Develop programs more quickly
  • Avoid platform dependencies
  • Write once, run anywhere
  • Distribute software more easily
Basic questions:

Ques. What is Java?
Ans. Java is "Object oriented" programming language using which you can develop applications.

Ques. What types of program we can build using java?
Ans. 

  1. We can create website like facebook, gmail.com
  2. We can create standalone application like notepad, microsoft word, paint.
  3. We can create Ecommerce application like flipkart, amazon, paytm etc.
  4. We can create Banking application like HDFC payment, SBI payment
  5. We can create mobile application

Ques. Why Java is popular?
Ans.

  1. Platform independent
  2. Easy to learn a simple to understand
  3. Secured and robust
  4. Open source

Ques. What is meaning of  object oriented language?
Ans. Any language which supports 6 features: Class, Object, Encapsulation, Abstraction, Inheritance and Polymorphism.