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

## 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. Senior Member
Join Date
Jun 2008
Posts
2,571
Rep Power
11
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. Member
Join Date
Apr 2011
Posts
16
Rep Power
0
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. Senior Member
Join Date
Dec 2008
Location
Kolkata
Posts
283
Rep Power
8

## How to find a prime number

5. Senior Member
Join Date
Jun 2008
Posts
2,571
Rep Power
11
I am done answering your quiz questions.

6. Member
Join Date
Apr 2011
Posts
16
Rep Power
0
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. 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. Senior Member
Join Date
Jun 2008
Posts
2,571
Rep Power
11
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?

9. Senior Member
Join Date
Dec 2008
Location
Kolkata
Posts
283
Rep Power
8
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];
System.out.println(num+" is not a prime number");
//break;
}
else{
System.out.println(num+" is a prime number");
}

}
}
}```

10. Originally Posted by masijade
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 ;-)

11. Senior Member
Join Date
Jun 2008
Posts
2,571
Rep Power
11
WooHoo! Chaos made to order! ;-)

12. Member
Join Date
Apr 2011
Posts
16
Rep Power
0
Thanks a lot Dswastik... :)

13. Originally Posted by masijade
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

#### Posting Permissions

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