Sunday, 3 April 2011

How to Convert Character into a String in java

import java.lang.*;
This program takes a character at the console and converts it into a string format using by the toString() method. This method converts the character into an integer type data. The character class wraps a value of the primitive type char in an object. The toString method returns a string object representing this character's values

public class CharToString{
public static void main(String args[])throws IOException{
BufferedReader read = new BufferedReader(new InputStreamReader(;
System.out.println("Enter a Character:");
String s = read.readLine();
char c = s.charAt(0);
System.out.println("Character value is " + c);
String s1 = Character.toString(c);
System.out.println("String value is " + s1);

Output of this program.

C:\corejava>java CharToString
Enter a Character:
Character value is R
String value is R

How to Compare String in java

  1. /*
  2. Java String compare example.
  3. */
  4. public class JavaStringCompareExample{
  5. public static void main(String args[]){
  6. /*
  7. Java String class defines following methods to compare Java String object.
  8. 1) int compareTo( String anotherString )
  9. compare two string based upon the unicode value of each character in the String.
  10. Returns negative int if first string is less than another
  11. Returns positive int if first string is grater than another
  12. Returns 0 if both strings are same.
  13. 2) int compareTo( Object obj )
  14. Behaves exactly like compareTo ( String anotherString) if the argument object
  15. is of type String, otherwise throws ClassCastException.
  16. 3) int compareToIgnoreCase( String anotherString )
  17. Compares two strings ignoring the character case of the given String.
  18. */
  19. String str = "Hello World";
  20. String anotherString = "hello world";
  21. Object objStr = str;
  22. /* compare two strings, case sensitive */
  23. System.out.println( str.compareTo(anotherString) );
  24. /* compare two strings, ignores character case */
  25. System.out.println( str.compareToIgnoreCase(anotherString) );
  26. /* compare string with object */
  27. System.out.println( str.compareTo(objStr) );
  28. }
  29. }
  30. /*
  31. OUTPUT of the above given Java String compare Example would be :
  32. -32
  33. 0
  34. 0
  35. */

How to Split a String in java Exmple explained

  1. *
  2. Java String split example.
  3. */
  4. public class JavaStringSplitExample{
  5. public static void main(String args[]){
  6. /*
  7. Java String class defines following methods to split Java String object.
  8. String[] split( String regularExpression )
  9. Splits the string according to given regular expression.
  10. String[] split( String reularExpression, int limit )
  11. Splits the string according to given regular expression. The number of resultant
  12. substrings by splitting the string is controlled by limit argument.
  13. */
  14. /* String to split. */
  15. String str = "one-two-three";
  16. String[] temp;
  17. /* delimiter */
  18. String delimiter = "-";
  19. /* given string will be split by the argument delimiter provided. */
  20. temp = str.split(delimiter);
  21. /* print substrings */
  22. for(int i =0; i < temp.length ; i++)
  23. System.out.println(temp[i]);
  24. /*
  25. IMPORTANT : Some special characters need to be escaped while providing them as
  26. delimiters like "." and "|".
  27. */
  28. System.out.println("");
  29. str = "one.two.three";
  30. delimiter = "\\.";
  31. temp = str.split(delimiter);
  32. for(int i =0; i < temp.length ; i++)
  33. System.out.println(temp[i]);
  34. /*
  35. Using second argument in the String.split() method, we can control the maximum
  36. number of substrings generated by splitting a string.
  37. */
  38. System.out.println("");
  39. temp = str.split(delimiter,2);
  40. for(int i =0; i < temp.length ; i++)
  41. System.out.println(temp[i]);
  42. }
  43. }
  44. /*
  45. OUTPUT of the above given Java String split Example would be :
  46. one
  47. two
  48. three
  49. one
  50. two
  51. three
  52. one
  53. two.three
  54. */

How to Convert String to Character in java Array Example

  1. /*
  2. Convert String to Character Array Example
  3. */
  4. public class StringToCharacterArrayExample {
  5. public static void main(String args[]){
  6. //declare the original String object
  7. String strOrig = "Hello World";
  8. //declare the char array
  9. char[] stringArray;
  10. //convert string into array using toCharArray() method of string class
  11. stringArray = strOrig.toCharArray();
  12. //display the array
  13. for(int index=0; index < stringArray.length; index++)
  14. System.out.print(stringArray[index]);
  15. }
  16. }

  1. /*
  2. Output of the program would be :
  3. Hello World
  4. */

What is the difference between Reference and Primitive types.

What are Primitive Types?

A primitive data type is a type where the VALUE of the variable is stored to the variable name.

What are Reference Types?

A reference type is a type where a REFERENCE of the object is stored. ‘’okay, so what’s a REFERENCE? A REFERENCE is simply a location in the computer’s memory. Just think of it as an address.’’

What’s the difference?

Look at things this way… A primitive data type is like a basket of eggs. When you place 10 eggs into a basket, nothing much that you do external of the basket can affect the number of eggs in the basket. (Here’s a bunch of code that will probably scare the heck out of you if you feel that you’re not born to be a programmer. Here’s a piece of advice. Read the code, one line at a time. If you don’t understand it, read it again. Anything behind the “//” is friendly, I’m just trying to explain how you should think about each line of code. It really isn’t difficult, I promise.)

int x=10;   //10 eggs int basket=x;   //The VALUE of x is stored into the basket x=9;    //x is changed, but the basket is remains the same. System.out.println(“x=”+x);  //see! It’s 9! System.out.println(“basket=”+basket); //see! It still prints 10! 

A reference data type is simply an address. When you use an object, the value isn’t stored, but instead an address of the object is stored. Take a home address for example, 16 Jefferson Park Avenue. It’s not possible for me to put a whole house into my basket, instead, I scribble the address down on a scrap of paper, “16 Jefferson Park Ave”. From now onwards, I whenever I want to do something to 16 Jefferson Park Ave, I simply reference the address. If I wanted to know the number of people in the house, I would need to go to the house, and count the number of people in there. This is necessary since all I have is the address, it would be impossible to determine information about the house just by looking at my own basket like before.

House h = new House(10); House jeff=h; h.setNumber(9); System.out.println(“h=”+h.getNumber()); System.out.println(“jeff=”+jeff.getNumber()); 

If you’re still stuck, try to think about the different results produced by the code. Try to resolve it within yourself why the results of both tests are different. It really is quite simple.

What do I need to know?

Now that you understand the concept (or at least I hope you do), you just need to know when something is a primitive data type, and when it’s a reference. Luckily for you, I have a complete list of primitive data types here. Anything that doesn’t fall into these data types is a reference data type. Here they are.

byte, short, int, long, float, double, boolean, char

Yes. This means String is a Reference Data Type! Okay that’s all I have for now, hope it was helpful!

Source Reference:

Write a program in C / C++ that calculate the summation of consecutive positive odd integers from 1 to n

Here is the code

#include using namespace std; int main() { int n; int sum = 0; int number; cout << "Enter the value for n: "; cin >> n; for (number=1; number<=(2*n); number+2) { sum = sum + number; } cout << "The sum of "; cout << n; cout << " is "; cout << sum << endl; return 0; }

Write a program in C to find the sum of odd numbers and even numbers from 1 to n


void main()
int n,i;
int addeven=0;
int addodd=1;

printf("\nEnter a number\n");


printf("\nThe sum of odd numbers is %d \nThe sum of even numbers is %d\n",addeven,addodd);