Results 1 to 13 of 13

Thread: Strings

  1. #1
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default Strings

    Why are called Strings called Immutable ?

    String a="Hi";
    String a=a+"Hello";

    What will be the output? and if the output is HiHello then why Strings referred to as Immutable.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because the object that "a" originally pointed to is unchanged by the above operations. A new object has been created and "a" now points to that, but "a" is not the String, is a reference to a String (and that reference can change to point to a new String), but the String itself does not.

    Edit: See this then this.
    Last edited by masijade; 04-18-2011 at 11:31 AM.

  3. #3
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    suppose I have an array 12,34,35,42,9,76,31,54,99,150
    and I have to find the element which is a prime number and print the element and index position of the array as well using break statement.

    Can you please provide me the program as I am stuck in the beginning itself i.e. finding the prime number from the array.

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I am done answering your quiz questions.

  6. #6
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    Hi Dswastik,

    I went through the link but it didnt help me much. I need to search the prime number from an array of elements 12,34,35,42,9,76,31,54,99,150. So was unable to do it. I tried little but its giving Array Index Outof Bounds...
    My coding is as follows..

    class Break
    {
    public static void main(String ar[])
    {
    int a[]={12,34,35,42,9,76,31,54,99,150};
    for(int i=0;i<=a.length;i++)
    {
    int n=a[0];
    for(int j=2;j<a[i];j++)
    {
    n=a[i]%j;
    if(n==0)
    System.out.println("Not Prime");
    break;
    }
    if(n==a[i])
    System.out.println(a[i]);
    }
    }
    }

    The first for loop is regarding the element index, I am ignoring that first I need to find out the prime number.

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    for([COLOR="blue"]int i[/COLOR]=0;i<[COLOR="Red"]=[/COLOR]a.length;i++)

    i cannot equal-to a.length because, in computers, everything starts with a zero.

    That means, if you have an array with 5 objects, the indexes would be 0,1,2,3,4 but the index i cannot equal the array length = 5. Otherwise you would get an IndexOutOfBounds exception.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by ozzyman View Post
    i cannot equal-to a.length because, in computers, everything starts with a zero.
    Everything? Try looping and indexing in MathLab with that theory. Try getString(0) on a ResultSet, which day_of_the_week does 0 equate to?

  9. #9
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    To add more, its better to write your codes in small block of functions, its always easier to debug

    Java Code:
    class Break
    {
    	private static boolean isPrimeNumber(int num){
    		int i;
    	       for (i=2; i < num ;i++ ){
    	    	  int n = num%i;
    	    	  if (n==0){
    				break;
    	    	  }
    	        }
    	        if(i == num){
    	    	  return true;
    	        }
    	        else{
    				return false;
    			}
    		}
    
           public static void main(String ar[])
           {
                   int a[]={12,34,35,42,9,76,31,54,99,150};
                   for(int i=0;i<a.length;i++)
                   {
                           int num=a[i];
                           if(!isPrimeNumber(num)){
    						   System.out.println(num+" is not a prime number");
    						   //break;
    					   }
    					   else{
    						   System.out.println(num+" is a prime number");
    					   }
    
                   }
           }
    }
    Swastik

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,452
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by masijade View Post
    Everything? Try looping and indexing in MathLab with that theory. Try getString(0) on a ResultSet, which day_of_the_week does 0 equate to?
    The almost orgasmic joy of Pascal's user definable array bounds come to mind; and, oh, the halucinating utmost extasy of Fortran/77's 'equivalent' statement ... what fun we had in those days ... *sigh*

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    WooHoo! Chaos made to order! ;-)

  12. #12
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    Thanks a lot Dswastik... :)

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,452
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by masijade View Post
    WooHoo! Chaos made to order! ;-)
    Yeah, yeah, laugh about it; but even in the book "Numerical Recipies in C" I saw them do things like this:

    Java Code:
    float farray[100];
    float* parray= &(farray[-1]);
    ... claiming that it would be so much easier to work with parray because the algorithms were so much more easily described when indexes started from one ...

    The boneheads didn't realize that the expression farray[-1] causes undefined behaviour ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Need help with Strings
    By </3java in forum New To Java
    Replies: 24
    Last Post: 02-17-2011, 05:00 PM
  2. Strings
    By zoala001 in forum Java Applets
    Replies: 9
    Last Post: 01-02-2011, 10:26 AM
  3. Binary Strings
    By Zack in forum New To Java
    Replies: 0
    Last Post: 06-25-2010, 07:15 AM
  4. Strings and Immutable
    By al_Marshy_1981 in forum New To Java
    Replies: 19
    Last Post: 06-18-2010, 07:22 AM
  5. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 05:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •