String Immutable In Java?
Jun 26, 2014I am a java fresher. I want to know weather String is immutable in Java or not .....
View RepliesI am a java fresher. I want to know weather String is immutable in Java or not .....
View RepliesI would like to understand why only String class is immutable.
1. Why String class is immutable? What is the main reason for making String class as immutable.
2. Why there is no int pool or float pool or Integer pool etc, why only String pool.
this is my code :
String name = "Mohan";
String is a immutable class and where these character stored when we assign some character. How the logics are working inside the String class.
public static void main(String[] args) {
String str1="Java";
str1="one";
System.out.println("str1="+str1);
}
String object is stored in a private final char array in String.java.
private final char value[];
The basic characteristic of a final variable is that it can initialize a value only once. By marking the variable value as final, the class String makes sure that it can’t be reassigned a value.
so the String objects can be initialized only once but the above code shows that str1 was initialized first with "Java", then it can be re-assigned value "one" bcos the output is one. If it can be re-initialized, basic characteristic of final variable is not satisified and hence how can we call String objects are immutable?
I want to clarify it whether this below code, when running this loop, will it create separate string objects as strings are immutable or else, will it keep the same reference(as each time inside loop, we use the same name 'rslt') and assign new string value for this?
while(inc < numberOfColumns){
String rslt=rs.getString(inc+1);
rowArr[inc] = rslt;
inc++;
}
strings are immutable in java...so can we apply toupper nd tolower function directly in a string
View Replies View RelatedI have some complex objects I want to design. Most of the time I would like these objects to be immutable so that other classes cannot change their values. However, I also want to create an editor to create these objects and the editor will need to change the object's values. It would also be useful to be able to set the objects' fields one at a time during serialization rather than doing everything in a huge constructor.
C provides a way to make objects unchangeable by using the 'const' keyword, but Java doesn't have that. I could also wrap my objects in other accessor objects, but then I'm duplicating a lot of code. Are there any good ways to make my objects mutable only to certain other classes?
The following codes shows an implementation of an enqueue function of a FIFO immutable queue, and the output result shows the String "c".
I don't understand why since I expected it should be null.
The head and the tail of an ImmutableQueue Object are two separate queue, and each time I call the enqueue function, it just return a new object with a new tail, however, the head is not modified except the first two times I call the function.
Therefore, I expected head.next.next should be a null element, but the result is not like that.
public class ImmutableQueue<E> {
private int size =0;
public Queue<E> head;
public Queue<E> tail;
public ImmutableQueue(){}
private ImmutableQueue(Queue<E> hd, Queue<E> tl){
head=hd;
tail=tl;
[Code] ....
I have make the immutable class as below, Now my question is how I can test if my this class/object is immutable
package com.learning;
import java.util.*;
import java.util.Map.Entry;
public final class ImmutableTest {
private final int id;
private final String name;
private final HashMap<String, String> hm ;
[Code]...
How I can Test If it is immutable class without looking ?
The following codes shows an implementation of an enqueue function of a FIFO immutable queue, and the output result shows the String "c". I don't understand why since I expected it should be null.
The head and the tail of an ImmutableQueue Object are two separate queue, and each time I call the enqueue function, it just return a new object with a new tail, however, the head is not modified except the first two times I call the function.
Therefore, I expected head.next.next should be a null element, but the result is not like that.
public class ImmutableQueue<E> {
public Queue<E> head;
public Queue<E> tail;
public ImmutableQueue(){}
private ImmutableQueue(Queue<E> hd, Queue<E> tl){
head=hd;
tail=tl;
[Code] ......
If I am given a string such as:
REG20140509-0001
I want to cut out the year/month/day, so in this case '20140509'.I know that the characters in front of the date will always be letters, not numbers, but I don't know how many. Three in the above example, but could be more or less.i do know that the date will always be followed by a dash and four digits.
I couldn't get this code working:
import java.util.*;
public class scan {
public static void main (String args[]) {
String testi;
Scanner scan = new Scanner(System.in);
[Code] .....
Did i write something wrong or can't Scanner be used with String?
Here is my code:
public class ReverseString {
public static void main(String args[]) {
//quick wasy to reverse String in Java - Use StringBuffer
String str = "The rain in Spain falls mainly on the plain";
String revString = new StringBuffer(str).reverse().toString();
[Code] .....
The requirements: The program runs but for some reason it is not meeting the requirements. use the String method toCharArray and a loop to display the letters in the string in reverse order.
import java.io.*;
import java.util.*;
import java.util.StringTokenizer;
class read
[code]...
my code compiles and prints the text contained in the file but the tockenizer isnt working.
Due to compatibility reasons, I need to be able to store a random C buffer inside of a Java String. THis means that the Java String should contain the exact same buffer information (i.e. byte sequence) as the original C buffer. How would I do that?
All the functions I found will always somehow code/decode the C buffer, and modify its content depending on the selected encoding.
I need to do this inside JNI. Following is what I have:
Java Code:
unsigned char* cBuffer=getCBuffer();
// Transfer the C buffer to s:
jstring s=NULL;
if (env->EnsureLocalCapacity(2) >= 0)
{
jbyteArray bytes = env->NewByteArray(signalLength);
if (bytes != NULL)
[Code] ....
I have a String as follows: "the|boy|is|good"
now, I want to split this string using the "|" delimeter.
I tried this :
String line = "the|boy|is|good";
line.split("|");
But it didn't split it. Is there some regex for "|"?
I have written below regex for two lines.
String LN1Pattern = "^((?=.{1,35}$)(/([A-Z]{1,5})(|(//[a-zA-Z0-9]+))))$";
System.out.println("/ABC//FGhiJkl012345".matches(LN1Pattern));
String LN2Pattern = "^(|((s+(//[a-zA-Z0-9]{1,33})){1,2}))$";
System.out.println("".matches(LN2Pattern));
s+ is a newline character.
But when I combines both as below, its not giving me expected result.
^(((?=.{1,35}$)(/([A-Z]{1,5})(|(//[a-zA-Z0-9]+))))(|((s+(//[a-zA-Z0-9]{1,33})){1,2})))$
For string "/ABC//FGhiJkl012345
//abCD01EF02" - returns False. Expected is True
I think there is some problem in lookahead placed.
I am currently trying to split the string "EAM est" between the part. I have gotten the code to work if the was a -. But I can't see why the error is occuring
I have tried
String test = "EAM-testing";
String[] parts = test.split("-");
System.out.println("parts[0] = " + parts[0]);
System.out.println("parts[1] = " + parts[1]);
String test1 = "EAM esting";
String[] parts1= test1.split("");
[Code] .....
The error occurs at line: String[] parts1= test1.split("");
I am building an app, where I have to store the data eg: "hello ! how are you" in java as a string and then use scanner to get input and check if the entered word is present in the line stored. If it is stored then the entire sentence must be displayed. eg : if the stored string is "hello how are you"
if the entered string is "how", then the entire sentence "hello how are you" should be displayed.
I have a very big string that I am returning from servlet to javascript. I am using PrintWriter.
PrintWriter out = response.getWriter();
out.print(bigString);
But as the string is very big my code is not working. My browser just hangs.
Also is it possible to stream the output? i.e instead of sending entire string, can I send small part of string at a time. So that my browser will not hang. I don't want to handle streaming manually I am just looking for an IO class which will do the streaming automatically.
I am solving a problem in which first my program will ask a number N and then N numbers form the user
suppose:
5
4 3 4 5 6
another
6
3 2 7 8 9 3
and I am using this code
inputValues=new LinkedHashMap<Integer, Integer>();
Scanner in=new Scanner(System.in);
int N=in.nextInt();
String inputString=in.nextLine();
[Code] .....
But its not working as i want . Where is fault?
In the Employee's toString, you are using the NumberFormat class to format your hourly rate and weekly pay but you are supposed to use java string formatting only (%f). You should change those to use only string formatting - no use of NumberFormat.
package coursework;
import java.text.NumberFormat;
public class Employee {
private String firstName;
private String lastName;
private int employeeId;
private double hourlyRate;
public Timecard timeCard;
[Code] .....
In the code below I am splitting the String relativeDN at the "," character. After splitting the String I need to parse the string from the "=" character using indexof and then rebuild the string. I was able to parse the first = sign then rebuild the string but it doesn't remove the subsequent = character. I am trying to parse the string using idexof for all the "=" characters and then rebuild it using StringBuilder.
public class StringTestProgram {
public static void main(String[] args) {
String relativeDN = "cn=abc,dn=xyz,ou=abc/def";
//Split String
String[] stringData = relativeDN.split(",");
[Code] ....
How can I convert number to string in java. Something like
public void Discription(Number ownerName){
String.valueOf(ownerName);
I need a program to convert any string of any language to unicode using java....
View Replies View RelatedI defined a java class thus:
class Info{
public String name;
public String version;
public String arch;
double CPUSpeed;
double ranUtil, CPUUtil;
};
But each object of such a class takes many bytes. How can I limit it to one fourth of a Kilo-Byte?