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.
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 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 ?
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?
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;
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;
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));}}
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....
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.
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);
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.
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>
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) == '#'))) {
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.
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.
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
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?
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.
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?