String Immutable In Java?

Jun 26, 2014

I am a java fresher. I want to know weather String is immutable in Java or not .....

View Replies


ADVERTISEMENT

Why Only String Class Is Immutable

Feb 24, 2015

I 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.

View Replies View Related

Meaning Of String Or Immutable Class

Aug 26, 2014

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.

View Replies View Related

String Class Is Not Immutable - Can Reassign Another Value To Same Variable

Feb 26, 2015

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?

View Replies View Related

Create Separate String Objects As Strings Are Immutable

Jan 28, 2015

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++;
}

View Replies View Related

Strings Are Immutable In Java

Sep 21, 2014

strings are immutable in java...so can we apply toupper nd tolower function directly in a string

View Replies View Related

Can Classes Be Immutable In Some Situations But Not Others

Feb 12, 2014

I 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?

View Replies View Related

Implementation Of Immutable Queue

Nov 12, 2014

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] ....

View Replies View Related

How To Test If Class / Object Is Immutable

Jul 2, 2014

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 ?

View Replies View Related

FIFO - Implementation Of Immutable Queue

Nov 12, 2014

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] ......

View Replies View Related

Java String Cut

May 9, 2014

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.

View Replies View Related

Can Use Java Scanner With String?

Jan 18, 2010

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?

View Replies View Related

Reverse A String In Java

Oct 7, 2014

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.

View Replies View Related

Java String Tockenizer

Apr 16, 2014

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.

View Replies View Related

C Buffer To Java String

Sep 4, 2014

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] ....

View Replies View Related

Split A String In Java Using Delimiters

Aug 30, 2014

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 "|"?

View Replies View Related

Java String Regular Expression

Apr 17, 2014

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.

View Replies View Related

Splitting String In Java Error

Jul 21, 2014

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("");

View Replies View Related

Storing And Scanning String In Java?

Jun 10, 2014

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.

View Replies View Related

I/O / Streams :: Java Streaming Big String

Aug 27, 2014

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.

View Replies View Related

How To Read Integer And Then String From Java

Feb 28, 2015

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?

View Replies View Related

NumberFormat To Java String Format

Mar 6, 2014

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] .....

View Replies View Related

Java String IndexOf Parsing

Dec 24, 2014

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] ....

View Replies View Related

How To Convert Number To String In Java

Apr 8, 2013

How can I convert number to string in java. Something like

public void Discription(Number ownerName){
String.valueOf(ownerName);

View Replies View Related

String To Unicode Converter Program In Java

Mar 2, 2014

I need a program to convert any string of any language to unicode using java....

View Replies View Related

Java Class - Limiting String Length

Jul 16, 2014

I 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?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved