Meaning Of String Or Immutable Class
Aug 26, 2014this 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.
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 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.
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 have written the code to read the line from file and save first string as word and and remaining string as meaning..
E.g.: innovation a new method, idea, product, etc.
word = innovation
meaning = a new method, idea, product, etc.
In my code there is no error and have a mistake what it is means first assigning word is ok and while saving meaning ..it saves like
{
a a new a new method } like that
Java Code:
import java.io.BufferedReader;
import java.io.FileReader;
public class Dil{
public static void main(String[] arg)throws Exception
{
BufferedReader in;
[Code] ......
I am a java fresher. I want to know weather String is immutable in Java or not .....
View Replies View RelatedI 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?
while(inc < numberOfColumns){
String rslt=rs.getString(inc+1);
rowArr[inc] = rslt;
inc++;
}
program to accept string
import java.util.Scanner;
public class accept_string{
public static void main(String[] args)
[code]...
I wrote this program.actually I copy it from webpage this website.Its very cool to wrote programs in java.but I did not understand meaning of string,scanner......
what is meaning of Open Source in programming lenguages?
I want to ask this question reagrding this context that if some company or some products that says "This is open source" all codes are available Then it it mean..
1)We can reuse,re produce,distribute this code,modify and sell it for commercial purpose?
I know there is license also attach with a OpenSource.
But What exactly meaning of OpenSource?
I saw code like this.
Set<String> set = new HashSet<String>(){{ add("Hello"); }};
I can understand that it is a HashSet with one element "Hello" in it. I don't understand this syntax. Why there are double curly braces?
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?
strings are immutable in java...so can we apply toupper nd tolower function directly in a string
View Replies View RelatedThe 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] ....
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] ......
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);
[code]....
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] .....
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 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?
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]....
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] ......
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] .....
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 ....
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.