Strings

• 04-18-2011, 11:19 AM
leepikamukharji
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.
• 04-18-2011, 11:29 AM
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.
• 04-18-2011, 11:41 AM
leepikamukharji
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.
• 04-18-2011, 12:00 PM
dswastik
How to find a prime number
• 04-18-2011, 12:05 PM
• 04-18-2011, 12:45 PM
leepikamukharji
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.
• 04-18-2011, 12:56 PM
ozzyman
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.
• 04-18-2011, 01:04 PM
Quote:

Originally Posted by ozzyman
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?
• 04-18-2011, 01:05 PM
dswastik
To add more, its better to write your codes in small block of functions, its always easier to debug

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");                                           }               }       } }```
• 04-18-2011, 01:32 PM
JosAH
Quote:

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 ;-)
• 04-18-2011, 01:34 PM
WooHoo! Chaos made to order! ;-)
• 04-18-2011, 01:37 PM
leepikamukharji
Thanks a lot Dswastik... :)
• 04-18-2011, 01:43 PM
JosAH
Quote:

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:

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