Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Palindrome of Number and string in same user Input.

PALINDROME  OF A NUMBER AND A STRING  IN SAME USER INPUT  :



I have gone through lots of palindrome programs but did not find any Program which give me palindrome of string and Integer in same input.
I have used   Function Overloading Concept.
Following is code snippet.


 import java.util.Scanner;  
 public class PalStringAndNumber {  
      public static void main(String args[])  
       {  
           Scanner sca =new Scanner(System.in);  
           System.out.println(" Enter Number or string to check whether it is palindrome");  
 while (sca.hasNext()){  
                String input=sca.nextLine();  
                if(!input.matches("^\\d*$")){  // check for if ( string contain numbers..?)  
                     isPalindrome(input);     // call to  isPalindrome(String str) method   
                      if(isPalindrome(input)){  
                           System.out.println("you Entered " +input + " is Palindrome ");  
                      }  
                      else{  
                           System.out.println("you Entered " +input + " is NOT Palindrome");  
                      }  
                 }  
                 if( input.matches("^\\d*$")){  
                           // System.out.println("you entered number");  
                           if(isPalindrome(input)){  
            System.out.println("you Entered " +input + " is Palindrome ");  
                           }  
                           else{  
            System.out.println("you Entered " +input + " is NOT Palindrome");  
                           }  
                          }  
                 }  
       }  
       public static boolean isPalindrome(String str) {  
            int i;  
            int n=str.length();  
            String revstr="";  
            for(i=n-1;i>=0;i--)  
            revstr=revstr+str.charAt(i);  
            if(revstr.equals(str))  
            return true;  
            else  
            return false;  
 }  
       public static boolean isPalindrome(int number) {  
            int palindrome = number; // copied number into variable  
            int reverse = 0;  
          while (palindrome != 0) {  
            int remainder = palindrome % 10;  
            reverse = reverse * 10 + remainder;  
            palindrome = palindrome / 10;  
          }  
          // if original and reverse of number is equal means  
          // number is palindrome in Java  
          if (number == reverse) {  
            return true;  
          }  
          return false;  
        }  
 } 
Output :
  Enter Number or string to check whether it is palindrome  
 adi4444ida  
 you Entered adi4444ida is Palindrome   
In code
 if( input.matches("^\\d*$")) ("^\\d*$")) is regex 
 You can read more about regex in followiong link. 

Know Regex 

 Copy this code in your IDE and play around it.
If any problem in code let me know.

Happy Coding


What is JDK ,JVM,JRE,JIT ..?

 Why it is necessory to know this terms very well ..?

            What you need to run java program on your computer . JVM..??    
           NO.

JVM is not enough even only JRE is not enough. You will need JDK [ Big Boss ] for it. But what are these terms lets  know  about JDK, JRE, JVM and JIT.
In this post we are going to explore following things :

1. Why we need to understand this..?    [As i mention above in intro .]

2. Flow Dig [ who uses whom ]

3. JDK   [shipped for free to us by Sun ]

4. JVM   [ Real game changer ]

5. JRE   [cares about device specific features ie, java runs on MS,    
                      Mac, Linus ]
5. JIT  [ speed up the process ]

6. Summary. [ Sticky notes]               


  Flow Dig :


  JDK ( Java Development Kit ) :


JDK (Java Development Kit)  it  contains 2 things :

          1.    JRE    ( To run the proggram ).

           2.  development tools like Java libraries, Java compilers, debuggers,  
                bundling  and deployment tools and API’s.  
   
          If you want to only run java program then you only require JRE.



  JVM ( Java Virtual Machine ) :


  •  Converts bytecode (i.e. .class file) to 'system specific' code. Now, since    system specific code, as the   
  •  Name suggests is dependent on OS . JVM do this by implementing JRE.( implementing....?? read JRE you ‘ll get it).

JVM is platform dependent but it makes the Java platform independent.

  JRE ( Java Runtime Environment)  :



  • JRE is not part/subset of JVM.           
  •  JRE is an implementation of JVM (e.g. Oracle, MySQL, DB2 etc. are implementations of a specification named RDBMS).
  • What actually happens here  is - when we compile a Java file, the compiler converts Java code in a byte-code. This byte-code is nothing but instructions which are understandable by an implementation of JVM (i.e. JRE)

Now  the  job of JRE ( if JDK tells him to do) is - to read byte-code, and convert it into another byte-code which is understandable by underlying OS.

  • JRE is runtime interpreter as JVM implement (uses) JRE to convert (.class) byte code to OS specific machine readable code.

  JIT ( Just in time Compiler ) :

  • JIT is the part of the Java Virtual Machine (JVM) used to speed up the execution time.
  •  JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.


 Summary :

  • If you  able to run successfully java program on your machine then you are using JRE.
  • JDK is important if you want to develop and run code (as it contain JRE and Compiler with other libraries).
  • JIT is just used to save run time.