hi i have found Uri as immutable reference i dont know what it is the exact meaning of immutable reference... can anyone help me?
It's a variable that cannot be changed once set. Very useful when you have multithreaded code since being able to change a variable's value might be a source of many hard to find problems in your code.
If it's immutable, it's usually good.
A good example of an immutable class within the .NET Framework is System.String. Once you create a String object, you can’t ever change it. There’s no way around it; that’s the way the class is designed. You can create copies, and those copies can be modified forms of the original, but you simply cannot change the original instance for as long as it lives, without resorting to unsafe code. If you understand that, you’re probably starting to get the gist of where I’m going here: For a referencebased object to be passed into a method, such that the client can be guaranteed that it won’t change during the method call, it must itself be immutable.
In a world such as the CLR where objects are held by reference by default, this notion of immutability becomes very important. Let’s suppose that System.String was mutable, and let’s suppose you could write a method such as the following fictitious method:
public void PrintString( string theString )
{
// Assuming following line does not create a new
// instance of String but modifies theString
theString += ": there, I printed it!";
Console.WriteLine( theString );
}
Imagine the callers’ dismay when they get further along in the code that called this method and now their string has this extra stuff appended onto the end of it. That’s what could happen if System. String were mutable. You can see that String’s immutability exists for a reason, and maybe you should consider adding the same capability to your design.
EX: string is immutable...
if u have for ex string s =" whatever" and u output it with uppercase letter..for ex
Console.Write(s.ToUpper())the console will print u WHATEVER...but the string s will still be whatever... unlike the mutable type which will change the string from whatever to WHATEVER
"immutable" means "can't change the value"
"mutable" == "changeable"
"immutable" == "not changeable"
In java , every thing is treated as String and object , Now try to think that if have created a program of 10000 lines and in this there you have added "public" 100 times so do you think that every time this public is created in storage . else what we can do , we can created something like that when ever we find something like this we will fetch it from there there ( String pool )
Related
I was wondering what the difference between database.getReference("foo/bar/123") and database.getReference("foo").child("bar").child("123") is?
I'm assuming that the later one will load the complete "foo" object whereas database.getReference("foo/bar/123") just loads the "123" object?
Is my assumption correct or what is the correct / most efficient way to only load data of "123"?
The two are equivalent. You can inspect this manually this by printing the toString() format for both References.
References are cheap - there's nothing inefficient about either solution. Neither one has yet loaded any data. A Reference is just a pointer to a location in the database.
It should not make a difference, a reference is not actually accessed when instantiated. This is the most relevant document I can find,
https://firebase.google.com/docs/reference/node/firebase.database.Reference
The docs don't say it explicitly, but requests are only performed when using the .set() or .on() methods
I have a andoid.graphics.path already created with moveto(),lineTo(),cubeto() and close().
How do I get back all these operations ?
I mean something like the following pseodcode:
operations[] = getOperations(mypath);
String sOperation=operation[0].type; // moveto,cubeto,lineto,close
int X=operation[0].x;
int Y=operation[0].y;
I'm afraid this isn't possible. In the source of android.graphics.Path, you'll observe that the method directly invokes the native method, and doesn't provide any mechanism of retrieving the segments.
If you wish to keep the tuples, I'd suggest maintaining your own array.
The question "How to read a local (res/raw) file line by line?" deals with a similar issue but I was unable to construct a solution base on the answers provided there. I did get a very useful piece of info (the DataInputStream class which has a readLine method), and I have been researching this on the developer website and trying to make it work.
What I am trying to do is read information stored in successive lines of a text file into a string array, such that the first line is the first array element, the second line is the next array element, etc... and then this string array is going to be used to populate text fields in the next activity that is opened. This is all happening inside of a switch case (depending on the case i.e. which list item is selected, a different text file is loaded).
Here is what I have so far:
//Retrieve necessary text file for inputstream
InputStream buildinginfo = getResources().openRawResource(R.raw.testbuilding);
class DataInputStream extends FilterInputStream{
protected DataInputStream(InputStream buildinginfo) {
super(buildinginfo);
// TODO Auto-generated constructor stub
int i;
String[] building_info;
//Assign lines of text to array
for(i=0; i<5; i++){
building_info[i] = buildinginfo.readLine();
}
}
}
So far the editor is okay with it except for these errors, and I am not experienced enough to make sense of them. I understand what they are saying but not how to fix them. The errors are in the section inside the switch case where I am trying to set up the input stream and assign the values.
Most importantly, in the line where the readLine command takes place, I get:
"- The method readLine is undefined for the type DataInputStream"
"- The method readLine is undefined for the type InputStream"
This I do not understand because if I am not mistaken, it says here (http://developer.android.com/reference/java/io/DataInputStream.html) that the DataInputStream class has the readLine method available (I found out about this from the question referred to above). Obviously I have not used the DataInputStream correctly, but I can't seem to figure out how. I have looked through several questions on here and referred to the page linked above several times.
If anybody can see what I am doing wrong I would appreciate your help very much. If I am barking up the wrong tree entirely for this type of task, I apologize for wasting time, but some guidelines or a referral to an appropriate tutorial resource would be much appreciated. I have spent the last two days trying to figure out these errors.
Okay, so nobody else has helped you out here ... here goes.
Essentially you've made a wrong turn in your understanding of how to use API classes.
Your code is attempting to define the DataInputStream class. You want to use the one that is already provided by the API instead. By redefining the class you are actually masking the API class (you definitely don't want that).
So if you look at the documentation for DataInputStream you'll see a constructor. This allows you to create an instance of a class. This is what you want instead :)
InputStream buildinginfo = getResources().openRawResource(R.raw.testbuilding);
DataInputStream myDIS = new DataInputStream(buildinginfo);
//you've now got an instance of DataInputStream called myDIS
String firstString = myDIS.readLine();
//voila ... your firstString variable should contain some text
There's also an issue with you array code ... but I would not use an array in this way.
You should use ArrayList, then read the lines in a while loop so you don't need to tinker with the size.
First let's get rid of this line (I'll comment it out:
//String firstString = myDIS.readLine();
And create an instance of an ArrayList which is a template class so note the String in the angle brackets denotes what sort of elements we want in our array:
ArrayList<String> list = new ArrayList<String>();
You can use the add method to add elements to the arraylist.
We're going to do the whole thing in one go, don't worry I'll explain after ...
String myLine; //declare a variable
//now loop through and check if we have input, if so append it to list
while((myLine=myDIS.readline())!=null) list.add(myLine);
The last line is a bit chewy but when you assign the myLine variable this returns a result which you can compare with NULL ... once the last line is read, the readline function will return null. The condition of the while loop equates to false and drops out of the loop.
There is a simple helper function called toArray to turn your array list into a simple array.
String[] myStringArray = list.toArray(new String[list.size()])
You now have the desired output. I would suggest watching some videos on Android to get a general feel for the language, but I hope this helped you inderstand where the mistake was.
I am doing some debugging in my application, mainly loading custom styles from styles.xml when my custom view is given a style="#styles/CustomStyle", and attributes such as custom:attribute="custom value"
I looked into the TextView source to see how Android loads styles/attributes and I am mimicking that. However I am not being passed any of my R.styleables through some of the calls to my constructors and so I am trying to peek in there to see which resources are coming in.
I am using obtainStyledAttributes() to load these key/value pairs into a TypedArray, however I am wondering if there is an easy way to convert the R.styleable.CustomWidget_customAttribute from the int that R reads, to its referenced name.
In essence, I want LogCat to say, "We've been given R.styleable.xxx" and not "We've been given 1487214712442"
Look at this method: http://developer.android.com/reference/android/content/res/Resources.html#getResourceName(int)
Return the full name for a given resource identifier. This name is a single string of the form "package:type/entry".
You most likely are not able to do this explicitly, as all resources are stored in a generated java class with no accessible reference to the original strings.
However, your best bet is override the toString() method for the R class.
See if something like that works.
Hope this helped!
i found this problem some time ago, but i solve it using this: getString(), or this: getResources().getString()
but now, for this case, it doesn't works, i think it's because i need to get the string values on a NON ANDROID ACTIVITY CLASS. I need the resource values on a remote connection class, that doesn't extends any kind of activity or service.
how i can acces to the variables from my strings.xml on this normal class?
this is the code where i get the error (it gets an integer, and not the string value)
String a =R.string.totalpermission;
Take a look at these two answers (are the same XD):
How to obtain AssetManager without reference to Context?
How can I get a resource content from a static context?
Just an advice: try to read some basic concepts... it seems you don't understand what the R class is and how to use it. Trust me, you waste less time studying than trying to figure out how things work.
I'll add something to existing answers since I found it very useful.
To get your strings you have to use a Context. Your activity will work just great.
String string = getString(R.string.myString);
But if you have something more complex... for exemple
R.string.result -> "You %1$s %2$d cats"
String result = getString(R.string.result, killed ? "killed": "saved", count);
That would give you a result like that:
You saved 10 cats or You killed 2 cats... and so on. You can pass parameters and positional arguments in strings will get replaced by your arguments in getString.
All Android resources are referenced via a resource ID, like R.string.totalpermission. You can see those numbers in R.java (although there's no reason to ever do that).
In cases of strings, you can easily get those using Context.getString. Bonus: You can even pass optional arguments and add dynamic strings that way. You always have a context - how are you getting called? If you really don't have a context, you can create one for the package your resources are in.