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
ADVERTISEMENT
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
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
Jun 26, 2014
I am a java fresher. I want to know weather String is immutable in Java or not .....
View Replies
View Related
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
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
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
Sep 21, 2014
strings are immutable in java...so can we apply toupper nd tolower function directly in a string
View Replies
View Related
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
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
Dec 16, 2014
i am trying to write a class method which will take in a string and returns a string which is the reversed version of that string. it compiles fine but when i try to run it it states Main method not found in class StringReverse,please define the main method as public static void main(String[]args). I am new to java and cannot figure out
import javax.swing.JOptionPane;
public class StringReverse {
public String reverseString(String str){
JOptionPane.showInputDialog(null,"Please enter word");
char c = str.charAt(str.length()-1);
if(str.length() == 1) return Character.toString(c);
return c + reverseString(str.substring(0,str.length()-1));}}
View Replies
View Related
Mar 31, 2014
I want to write an app that declares a class Person(String name, int age), and an Account class, Account(int code, double balance).But, additionally, every Person has at most 3 accounts, and each account has a Peron associated with it.
my code so far...
public class Person {
private String name;
private int age;
private Account[] accounts;
private int numOfAccounts;
public Person(String name,int age){
this.name=name;
[code]....
My problem is:When I make data input for a person, and additionally I want to read data for the account(s) that this rerson has, what code should I write to create a new Account object as account[numOfAccounts].And, what is the code to assign an owner to a new created Account object?
There exists a relationship between the two classes, but I cannot find the way to implement this relation....
View Replies
View Related
Feb 12, 2015
I am a beginner at Java programming. how to implement my own String class, but I have to provide my own implementation for the following methods:
public MyString1(char[ ] chars)
public char charAt(int index)
public int length( )
ublic MyString1 substring(int begin, int end)
public MyString1 toLowerCase( )
[code]....
I have looked through the API, but I don't really understand where to start.
View Replies
View Related
Oct 22, 2014
why my sub class object just gives me a blank when it comes to the String. It works just fine for the super class but when I get to the sub class the program just gives me a blank. I won't let me input anything for the String. On line 24 of the client I attempt to input a new String but it doesn't ever let me enter one so then any call to getName is just a blank.
I have altered my super and sub class as well as the client to try to get it to work. I tried a local variable in the client, I tried using protected in the super class, I tried a handful of other things.
import java.util.*;
public class TryingItOutClient {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
[code]....
View Replies
View Related
Apr 9, 2014
What does the line Console[] conList mean? How am I going to access the string values in class consolelist?
class ConsoleInfo
{
private String conTitle;
private double conPrice;
private int conQty;
private String conPic;
[Code] .....
View Replies
View Related
May 18, 2013
I have a little a problem with String object in this class ....
public class Personne {
private String nom;
private String prenom;
private int age;
public Personne(){
this(null, null, 0);
[Code] ....
When i call the class personne with the Personne() i get these errors in compiling-time :
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(Unknown Source)
at Personne.setNom(Personne.java:18)
at Personne.<init>(Personne.java:12)
at Personne.<init>(Personne.java:8)
at Main.main(Main.java:4) // The line wich i inisialize my object in my main method.
View Replies
View Related
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
Apr 26, 2015
I've tried implementing the compareTo method in several ways and no luck.. I keep getting errors and now it just says bad operand type for binary operator with the ">" symbol and also the less than. I'm attempting to give an implementation for the compareTo method so it compares the value of the requestDate instance variable of the two objects. if the calling object of request is greater then I have to return ""1" if it's smaller then returns "-1" and if they are the same then returns value of "0"
package librarysystem_phase2;
import java.io.Serializable;
/**
* This class represents a request a member makes to checkout or download an item from the library.
*/
public class Request implements Serializable, Comparable<Request>
[code]....
View Replies
View Related
Apr 5, 2014
import java.util.*;
public class PhoneNumber {
private int areacode;
private int number;
private int ext;
PhoneNumber() {
[Code] .....
I just can't seem to get it completely correct.
PhoneNumber.java:35: error: incompatible types: int cannot be converted to String
areacode = Integer.parseInt(str[0]);
^
PhoneNumber.java:38: error: cannot find symbol
for(i = 0; i < number.length; i++){
^
symbol: variable length
[Code] ......
View Replies
View Related
Dec 9, 2014
The following code is located in my main class called InjectionFix. I have another class where I need to call or initiate the string in the code below. How can I achieve this. I tried to create an Object of the class but I cant do this because my other class doesnt contain a main method.How can I get the other class to initiate the code below which is loacted in my main class.
public static String escapeDN(String name) {
StringBuilder sb = new StringBuilder();
// space or # character at the beginning of a string
if ((name.length() > 0) &&
((name.charAt(0) == ' ') ||
(name.charAt(0) == '#'))) {
[Code] .....
View Replies
View Related
Feb 27, 2015
The String class stores the characters of the string internally as a private char[] and calling someString.length() results in getting the length field from the character array. I am looking to get the details on how the length is implemented. I understand it is a field, but in the original question I provide sample code and really want to know if/how the resulting byte code may differ when compiled, perhaps I am just not seeing the simple answer through my confusion.
Link ....
View Replies
View Related
Feb 10, 2015
There are two versions
1. The words remain in their places but the letters are reversed. Eg I love you becomes Ievol uoy
2. The words are also reversed. Eg I love you becomes uoy evol IWrite a program that use the java String class methods.
View Replies
View Related
Mar 12, 2014
I've a vertical-bar-delimited file where most elements contain text, some contain whitespace, and some are empty. Examples:
62RG|fe|Pencil Financial Group, LLC||doug@pencil.com|||85637889|Cross, Ben|bcross@godaddy.net|Bernard|Cross|Ben||315 One Tree Hill Terrace|Lafayette|LA
62RG|fe|Pencil Financial Group, LLC||tracy@pencil.com|||13658734|Dustin Cardwright|dcart@motorola.com|Dustin|Cartwright|||| |LA
which I parse and store with
String str_arry = innline.split( "|", 17);
lisst.add( new Contact( str_arry));
and my Contact class has the constructor
public Contact( String[] str_arry) {
for( int ii = 0 ; ii < str_arry.length ; ii++ ) {
if( str_arry[ii].matches("^s+$")) {
str_arry[ii] = null;
System.out.println("hit a null");
[Code]...
I expect the for-loop in the constructor to find any elements containing whitespace characters and set them to null for subsequent assignment.And when the code runs I do see some hit-statements pop up, so the detecting part is working.
But when I then process the list and access a Contact object and test fields for nulls I don't find any ie
if( aContactObj.getfFCity() == null) System.out.println("city is null");
never prints when it should.
What's the trick? Or is my approach wrong and if so what should it be?
View Replies
View Related
Aug 26, 2014
I'm new to java. I have a Product class with getters and setters.
E.g. setProdType & getProdType
I want to store the values from a file into that
StringTokenizer token = new StringTokenizer(line,"**");
while(token.hasMoreElements()) {
int p.setProdType = Integer.parseInt(token.nextElement().toString());
}
View Replies
View Related
Apr 8, 2014
I have a school assignment that involves me sorting an array of objects based on one of the class String variables. I am using as the title says a simple selection sort method. The problem I'm having is that when I run the program in debug mode, it never seems to enter the if statement in the inner loop. I would like to say I've tried a number of things to figure it out, but honestly I'm just stumped as to why it's not working.
Here is the code:
public static void sortTransactions(Transaction[] oTransaction){// This is the sorting method, obviously it's not done so it currently just prints to screen.
System.out.println("Successful call to sortTransaction()");
String min = "";
int curInd = 0;
Transaction[] temp = new Transaction[1];
[Code] ....
The output when I check to see if the array is sorted verifies that the array never does get sorted.
View Replies
View Related
May 19, 2014
I am trying to declare fields as protected String custom.field.1096; in my java class but it does not allow me. Can I not declare the field as above? Is there any workaround to achieve this?
View Replies
View Related