Basic structure and printing elements of Linked list in java


class Node{
     int data;
     Node next;
    
     Node(int data){
           this.data = data;
           this.next = null;
     }
}

public class LinkedList {
     Node head;
     public static void main(String[] args) {
           LinkedList list = new LinkedList();
           list.head = new Node(1);
           Node second = new Node(2);
           Node third = new Node(3);
          
           list.head.next = second;
           second.next = third;
          
           list.printList();
     }

      void printList() {
           Node node = head;
           //from start till last
           while (node!=null) {
                System.out.print(node.data);
                node = node.next;
                //to ignore last comma at last of list
                if(node!=null){
                     System.out.print(",");
                }
           }
     }

}                                                                   


Output-
1,2,3

What is String? Ways to create String? Complete Concept !!

In java, string is basically an object that represents sequence of char values.

char[] ch={'j','a','v','a','b','o','x'};  
String s=new String(ch);  

is same as

String s="javabox"

Two ways to create String object:

  1. By string literal
  2. By new keyword

1) String Literal

Java String literal is created by using double quotes. For Example:
String s="javabox";

Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn't exist in the pool, a new string instance is created and placed in the pool. For example:

String s="javabox";
String s="javabox";//will not create new instance 

Note: String objects are stored in a special memory area known as string constant pool.

Why java uses concept of string literal?
To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).


2) By new keyword

String s=new String("javabox");//creates two objects and one reference variable 
In this case, JVM will create a new string object in normal(non pool) heap memory and the literal "Welcome" will be placed in the string constant pool. The variable s will refer to the object in heap(non pool).

Program-

public class A {
public static void main(String args[]) {
String s1 = "javabox"; // creating string by java string literal
char ch[] = { 'p', 'u', 'l', 'k', 'i', 't'};
String s2 = new String(ch);  // converting char array to string
String s3 = new String("rajput"); // creating java string by new keyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}

Output-
javabox
pulkit
rajput


Program to print box pattern in java

Pattern to be printed - 

 *****
*****
*****



Program -

public class BoxPattern {

public static void main(String[] args) {

for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= 5; j++) {

System.out.print("*");

}
System.out.println();
}
}


}


Program to reverse the number

package javabox;

public class ReverseNumberExample {

    public static void main(String[] args) {

        int n = 1234, s = 0;

  while(n>0){    
   r=n%10;  //getting remainder  
   s=(s*10)+r;    
   n=n/10;    
  }    

        System.out.println("Reversed Number: " + s);
    }
}

Output - 
Reversed Number: 4321

Program to check Armstrong number

 Armstrong number is a number that is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.  Let's say, 153 = (1*1*1)+(5*5*5)+(3*3*3)  


package javabox;

class ArmstrongExample{  

  public static void main(String[] args)  {  
  
    int c=0,a,temp;  
    int n=153; //Number to check armstrong  
    temp=n;  
    while(n>0)  
    {  
    a=n%10;  
    n=n/10;  
    c=c+(a*a*a);  
    }  
    if(temp==c)  
    System.out.println("Armstrong number");   
    else  
        System.out.println("Not Armstrong number");   
   }  

}  

Output - 
Armstrong number

Program to find Factorial using Recursion

Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example: 4! = 4*3*2*1 = 24  

package javabox;

class FactorialExample{  

 static int factorial(int n){    
  if (n == 0)    
    return 1;    
  else    
    return(n * factorial(n-1));    
 }    
 public static void main(String args[]){  

  int i,fact=1;  
  int number=4;//Number to calculate factorial    
  fact = factorial(number);   
  System.out.println("Factorial of "+number+" is: "+fact);    
 }  
}  

Output - 

Factorial of 4 is: 24

Program to find Factorial using loop

Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example: 4! = 4*3*2*1 = 24  


package javabox;

class FactorialExample{  

 public static void main(String args[]){  

  int i,fact=1;  
  int number=5;//It is the number to calculate factorial    
  for(i=1;i<=number;i++){    
      fact=fact*i;    
  }    
  System.out.println("Factorial of "+number+" is: "+fact);    
 }  

}  


Output - 
Factorial of 4 is: 24

Program to check Palindrome number

palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984

package javabox;

class PalindromeExample{  

 public static void main(String args[]){
  
  int r,sum=0,temp;    
  int n=454; //Number to be checked for palindrome  
  
  temp=n;    
  while(n>0){    
   r=n%10;  //getting remainder  
   sum=(sum*10)+r;    
   n=n/10;    
  }    
  if(temp==sum)    
   System.out.println("Palindrome number ");    
  else    
   System.out.println("Not palindrome");    
}  
}  


Output - 
Palindrome number

Program to swap number with temporary variable

package javabox;

public class SwapExample {


public static void main(String a[]){

int x = 30;
int y = 40;
int temp;
System.out.println("Before swap:");
System.out.println("x value: "+x);
System.out.println("y value: "+y);
temp = x;
x = y;
y = temp;
System.out.println("After swap:");
System.out.println("x value: "+x);
System.out.println("y value: "+y);
}
}

Output -

Before swap:
x value: 30
y value: 40
After swap:
x value: 40
y value: 30

Program to swap number without temporary variable

package javabox;

public class SwapExample {

public static void main(String a[]){
int x = 30;
int y = 40;
System.out.println("Before swap:");
System.out.println("x value: "+x);
System.out.println("y value: "+y);
x = x+y;
y = x-y;
x = x-y;
System.out.println("After swap:");
System.out.println("x value: "+x);
System.out.println("y value: "+y);
}
}

Output -
Before swap:
x value: 30
y value: 40
After swap:
x value: 40
y value: 30

Program to print fibonacci series using recursion

In fibonacci seriesnext number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. 

package javabox;

public class FibonacciExample {

static int n1=0,n2=1,n3=0;    
static void printFibonacci(int count){    
    if(count>0){    
         n3 = n1 + n2;    
         n1 = n2;    
         n2 = n3;    
         System.out.print(" "+n3);   
         printFibonacci(count-1);    
     }    
}    
public static void main(String args[]){    
  int count=10;    
  System.out.print(n1+" "+n2);//printing 0 and 1    
  printFibonacci(count-2);//n-2 because 2 numbers are already printed   
}  
}

Output - 

0 1 1 2 3 5 8 13 21 34

Program to print fibonacci series without using recursion

In fibonacci seriesnext number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. 


package javabox;

public class FibonacciExample {

 public static void main(String[] args) { int n1=0,n2=1,n3,i,count=10; System.out.print(n1+" "+n2);//printing 0 and 1 for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed { n3=n1+n2; System.out.print(" "+n3); n1=n2; n2=n3; } } }

Output - 
0 1 1 2 3 5 8 13 21 34

Program to check prime number

Prime number is a number that is greater than 1 and divided by 1 or itself. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.

package javabox;

public class PrimeNumberExample {

public static void main(String[] args) {

int n=5;//Number to be checked  
boolean isPrime=true;
int i,m=0;
m=n/2; 
if(n==0||n==1){  
System.out.println(n+" is not prime number");      
}
else{  
for(i=2;i<=m;i++){      
if(n%i==0){      
System.out.println(n+" is not prime number");      
isPrime=false;      
break;      
}      
}      
if(isPrime)  { 
System.out.println(n+" is prime number");
}  
}//end of else  
}    
}

Output - 
5 is prime number

What is java? How java is platform independent? Why java is so popular and still considered as a winner in IT sector?

What is java?
Java is a hardware and software independent programming language.

Hardware independent means - it can run on any type of processor like 32 bit or 64 bit processor or any other type of processor.


Software independent means - it can run on any type of operating system like windows, unix, linux etc.




How java is hardware independent? -   when you install java in system, jdk(java development kit) get installed. 
Inside jdk, jvm (java virtual machine), jre(java run time environment), other tools and libraries are installed. 

what is jvm - It's a piece of code that converts .java file into .class file. This .class file is called as Byte code.

what is jre - It contains jvm and set of libraries that provides an environment for the execution.

This jre is responsible to provide hardware independency by providing run time environment.. 

How java is software independent? - This jvm is responsible to provide software independency. When .java file is converted to . class files. These class file can be moved to any other system and executed provided jre installed in that system. 
This is how software dependency can be achieved. 

Who created java?
James Gosling, Mike Sheridan, and Patrick Naughton started the Java language project in 1991.Java is named on the name of an island of indonesia where first coffee was produced.

Versions of java released -
  1. JDK Alpha and Beta (1995)  - first version of java released
  2. JDK 1.0 (1996)
  3. JDK 1.1 (1997)
  4. JDK 1.2 (1998)
  5. JDK 1.3 (2000)
  6. JDK 1.4 (2002)
  7. JDK 1.5 (2004)
  8. JDK  1.6 (2006)
  9. JDK  1.7 (2011)
  10. JDK  1.8 (2014)
  11. JDK 1.9 (2017)













Basic structure and printing elements of Linked list in java

class Node {      int data ;      Node next ;           Node ( int data ){            this . data = data ;            ...