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

Basic structure and printing elements of Linked list in java

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