Saturday, May 4, 2019

How To Reverse A String ?

Java Code Example - 1 :-

import java.io.*;
import java.util.*;

public class reverseString {
    public static void main(String[] args) {
        String input="";
        System.out.println("Enter the input string");
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            input = br.readLine();
            char[] try1= input.toCharArray();
            for (int i=try1.length-1;i>=0;i--)
            System.out.print(try1[i]);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
}}

Java Code Example - 2 :-

package coreJava;

public class StringRev2 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
    String input="AliveisAwesome";
    StringBuilder input1 = new StringBuilder();
    input1.append(input);
    input1=input1.reverse(); 
    System.out.print(input1);  
 }
}

String in Java

Java string :-

  • String is a final in java.lang package and not a keyword.
  • There are two ways to create string objects in java. One is using new operator and another one is using string literals. The objects created using new operator are stored in the heap memory and objects created using string literals are stored in string constant pool.
String s1 = new String("abc");          //Creating string object using new operator

String s2 = "abc";        //Creating string object using string literal

1. Is String immutable in java?

Yes, String class is immutable in java. Immutable means once the object is created, its value can not be changed.

2. How many objects are created using new keyword?

String str = new String("JavaHungry");

Two objects are created by the above statement. One object in the heap memory and one object in the String constant pool.

3. How to convert String to char Array?

You can convert String to char Array using toCharArray() method.

4. Are String thread-safe in java?

As we know String objects are immutable. It means they are thread-safe also.

5. Which class is final among String,StringBuilder and StringBuffer?

String class is the only class which is final.

6.  Is String primitive type or object (derived) type in java?

String is object(derived) type in java.

7. Can we use String in switch statement?

Yes, you can use String in switch statement in java 7. Prior to java 7 , you had to use if-else statements to achieve the task.

8. What is the difference between StringBuilder and StringBuffer in java?

Below are the main differences between StringBuilder and StringBuffer in java.

1. StringBuilder is not thread-safe. StringBuffer is thread-safe.
2. StringBuilder is not synchronized and StringBuffer is synchronized.
3. StringBuilder is faster while StringBuffer is slower as it is thread-safe.

9. What is the difference between below statements ?

String str = new String("abc");
String  str =  "abc";

In the first statement, String str = new String("abc");
JVM will create one object in the heap memory. Another object in the String constant pool, if the object is not present. Otherwise,if present in the String constant pool ,it will return the reference to it.

In the second statement, String str = "abc";
JVM checks for the string "abc" in the String constant pool. If the string is not present in the constant pool then it will create a new String object and stores it in pool.
If the string "abc" is found in string constant pool , then it simply creates a reference to it but does not create a new object. 

what is Java

What is Java

  • Java is a object oriented programming language.
  • It is platform independent.
  • Java is secure due to public key encryption.
  • Java is portable, robust, multi-threaded language.

History of Java :-

  • James Gosling initiated Java language project in June 1991
  • On 13 November, 2006, Sun released much of Java as free and open source software under the terms of the GNU General Public License (GPL).
  • On 8 May, 2007, Sun finished the process, making all of Java's core code free and open-source, aside from a small portion of code to which Sun did not hold the copyright.

What is object oriented programming:-

Java supports Polymorphism, Inheritance, Encapsulation, Abstraction.

Objects :- Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.

Class :- A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.

Example :-
public class Dog {
   String breed;
   int age;
   String color;

   void barking() {
   }

   void hungry() {
   }

   void sleeping() {
   }
}

Local variables − Variables defined inside methods, constructors or blocks are called local variables.

Instance variables − Instance variables are variables within a class but outside any method

Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.

what is constructors :-

  • Every class has a constructor. If we do not explicitly write a constructor for a class, the Java compiler builds a default constructor for that class.
  • Each time a new object is created, at least one constructor will be invoked. 
  • The main rule of constructors is that they should have the same name as the class.
  • A class can have more than one constructor.
public class Puppy {
   public Puppy() {
   }

   public Puppy(String name) {
      // This constructor has one parameter, name.
   }
}