Strings Are Immutable In Java
Sep 21, 2014strings are immutable in java...so can we apply toupper nd tolower function directly in a string
View Repliesstrings are immutable in java...so can we apply toupper nd tolower function directly in a string
View RepliesI 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++;
}
I am a java fresher. I want to know weather String is immutable in Java or not .....
View Replies View RelatedI 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.
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?
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] ....
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.
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] ......
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 am having an array of strings and i want to find out whether these strings contained in the array contain a similar character or not.For example i am having following strings in the array of string:
aadafbd
dsfgdfbvc
sdfgyub
fhjgbjhjd
my program should provide following result: 3 because i have 3 characters which are similar in all the strings of the array(f,b,d).
covers switch statements and if/else statements. Java doesn't like the Strings for some reason. My instructor does her strings just like this and it works for her. I can figure out the rest of the program if I can only get around the: "java error35: sSymbol variable might not have been initialized.
import java.util.*;
public class RockPaperScissors
{
public static void main(String[] args) {
//generate outcome
int symbol = (int)(Math.random() * 4);
String sSymbol;
[code]....
I can sort strings in a collection by uppercase and then lowercase though I was wondering if there is any way of doing it in reverse, sorting by lowercase then by uppercase.
View Replies View RelatedI want get a specific document from data set if a given string matches in that document.
If given string is 'what is java' and,
doc1='... what is full form of java ...'
doc2='... is java what ...'
doc3='... what is java ...'
then the output should contains doc1, and doc3.
I'm trying like this.
for(String key_word: key.split("W+")) {
for (String _word : line1.split("W+")) {
if(word.toLowerCase().equals(key_word.toLowerCase())) {
key_final=key_final+" "+key_word;
}
}
}
I'm getting wrong output containing doc1, doc2 and doc3.But I want only the exact match. Here each document is containing some paragraphs.
It works when i do this ,
String a = "*";
for(int i = 0; i++; i<10){
System.out.println(a);
a = a +"*";
}
but it doesn't work the other way,
String a = "***************";
for(int i = 0; i++; i<10){
System.out.println(a);
a = a - "*";
}
I searched about this online and read strings are unmutable in java but what does that exactly mean ? If the string cannot be changed how i am able to add the characters but not remove them.
Am i doing this right?
public class TwoStrings
{
public static String duplicate (String s) {
String t = s + s;
return t;
[Code] ....
I am testing some stuff here is my code:
public static void main(String[] args) {
String[] a = new String[5];
//String input = JOptionPane.showInputDialog(null, "enter input");
Scanner scanner = new Scanner(System.in);
[Code] ....
I am trying to type in 5 strings from java console and print them all out. I am using a "/" as a delimiter. My problem is that it does not print any output after I type strings separated by "/" like this: hello/gea/cae/eaf/aer and press enter. It works if I use JOptionPane but from the console its not working. I want to use the console instead of JOptionPane for this one.
I am advised to use a while loop the Scanner method hasNextLine() and in the while loop body, call the Scanner method nextLine(), and add the returned String to the ArrayList of Strings. what I have gotten so far:
Scanner input = new Scanner(new File(""));
while(input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
Its supposed to notify the user if they have a palindrome, but keeps returning true even when I type, "hello".
import java.util.Scanner;
public class PalinDrome {
public static void main(String[] args) {
String line;
Scanner input = new Scanner(System.in);
System.out.print("Please enter a word ");
[code]....
I know that in c++ in order to read a string like
PHP Code:
. you need to use
getline(cin,string variable)
. In java, I have noticed that using
String name=keyboard.nextLine()
automatically reads the whole line...Suppose I wanted to read the first name only, how would I be able to accomplish that?
example input (#federer #great #game )->
output : [federer] [great][game], [federer, game],[federer,great], [game,great], [federer, game,great]
I need my code to print out the top three most common IP Addresses in the ArrayList. Right now I have it printing out the top IP Address. I am a little confused as to how to get it to print out three. Every time I change something, It doesn't give me the correct results
My Code:
public class Log_File_Analysis {
private static ArrayList<String> ipAddress = new ArrayList<>();
private static String temp , mostCommon;
static int max = 0;
static int num = 0;
[Code] .....
Below is the snippet of code
public static void main(String[] args) throws Exception {
String s = "oldString";
reverse(s);
System.out.println(s); // oldString
}
public static void modifyString(String s) {
s = "newString";
System.out.println(s); // newString
}
I thought the first print statement would print "newString" as String is an object, and when we pass objects between methods, changing state of the object in any method reflects across the methods.
I am asking the user a question and already have a correct answer. The answer is a string but is more than one word.
When the user types in the correct answer, it still comes up as wrong but I only have the input.next method.
Quote
Why are rabbits ears long?
United states of america
Incorrect!
So the while loop I am trying to use is:
while( type != "EXIT" ) {
type = input.next();
}
The problem is that typing in EXIT doesn't end the loop like I want it to. I also tried input.nextLine(); but neither of them work. The loop is being used to fill an ArrayList so the number of elements can change based on how many they want. What am I doing wrong, or what alternatives do I have?
Seems I need to use !type.equals("EXIT")
I have to write a java programm,where i have given string. Output should be like that:
1. print only once what characters are apearinng in string, and the last index of it
2. print how many characters are in the string
I have to do it only with loops,no special classes
So far:
public static void main( String[] args ) {
String besedilo = "Testiranje";
besedilo = besedilo.toLowerCase();
for (int i=0; i<besedilo.length();i++)
[code]...